def groupby(tsastat, index_keys): """ group tsastat by index_keys, which are a subset of the original index_keys the grouping functions are predefined, it makes no sense to make this variable parameters: tsastat <TimeseriesArrayStats> index_keys <tuple> returns: <TimeseriesArrayStats> """ group_funcs = { "sum" : lambda a, b: a + b, "avg" : lambda a, b: (a + b) / 2, "min" : min, "max" : max, "count" : lambda a, b: a + b, "std" : lambda a, b: (a + b) / 2, "median" : lambda a, b: (a + b) / 2, "mean" : lambda a, b: (a + b) / 2, "last" : lambda a, b: (a + b) / 2, "first" : lambda a, b: (a + b) / 2, } try: assert all(index_key in tsastat.index_keys for index_key in index_keys) except AssertionError: logging.error("All given index_keys have to be in tsastat.index_keys") return # intermediate data data = {} for key in tsastat.keys(): key_dict = dict(zip(tsastat.index_keys, key)) group_key = tuple((key_dict[key] for key in index_keys)) if group_key not in data: data[group_key] = tsastat[key].stats else: # there is something to group for value_key in tsastat[key].keys(): for stat_func, value in tsastat[key][value_key].items(): # group values by function grouped_value = group_funcs[stat_func](value, data[group_key][value_key][stat_func]) # store data[group_key][value_key][stat_func] = grouped_value # get to same format as TimeseriesArrayStats.to_json returns outdata = [tsastat.index_keys, tsastat.value_keys, ] outdata.append([(key, json.dumps(value)) for key, value in data.items()]) # use TimeseriesArrayStats.from_json to get to TimeseriesArrayStats # object new_tsastat = TimeseriesArrayStats.from_json(json.dumps(outdata)) return new_tsastat
def get_tsastats(self, project, tablename, datestring): """ get TimeseriesStatsArray object for this particular project/tablename/datestring combination parameters: project <str> tablename <str> datestring <str> returns: <TimeseriesStatsArray> """ uri_params = { "project" : project, "tablename" : tablename, "datestring" : datestring, } query_params = {} data = self.__get_raw_data("get_tsastats", uri_params, query_params) tsastats = TimeseriesArrayStats.from_json(data) return tsastats