diff --git a/emission/net/api/metrics.py b/emission/net/api/metrics.py index a427d9c31..eeb80a358 100644 --- a/emission/net/api/metrics.py +++ b/emission/net/api/metrics.py @@ -12,7 +12,7 @@ import emission.analysis.result.metrics.simple_metrics as earms import emission.storage.decorations.analysis_timeseries_queries as esda import emission.storage.decorations.local_date_queries as esdl -import emission.storage.timeseries.tcquery as esttc +import emission.storage.timeseries.timequery as esttq import emcommon.metrics.metrics_summaries as emcms @@ -25,14 +25,9 @@ def summarize_by_local_date(user_id, start_ld, end_ld, freq_name, metric_list, i return _call_group_fn(earmt.group_by_local_date, user_id, start_ld, end_ld, local_freq, metric_list, include_aggregate) -def summarize_by_yyyy_mm_dd(user_id, start_ymd, end_ymd, freq, metric_list, include_agg, app_config): - time_query = esttc.TimeComponentQuery( - "data.start_local_dt", - esdl.yyyy_mm_dd_to_local_date(start_ymd), - esdl.yyyy_mm_dd_to_local_date(end_ymd) - ) +def summarize_by_yyyy_mm_dd(user_id, start_ymd, end_ymd, freq, metric_list, include_agg, app_config): + time_query = esttq.TimeQuery("data.start_fmt_time", start_ymd, end_ymd) trips = esda.get_entries(esda.COMPOSITE_TRIP_KEY, None, time_query) - print('found ' + str([e for e in trips])) return emcms.generate_summaries(metric_list, trips, app_config) def _call_group_fn(group_fn, user_id, start_time, end_time, freq, metric_list, include_aggregate): diff --git a/emission/storage/timeseries/timequery.py b/emission/storage/timeseries/timequery.py index 950e0adb0..1c86be78a 100644 --- a/emission/storage/timeseries/timequery.py +++ b/emission/storage/timeseries/timequery.py @@ -8,19 +8,26 @@ from builtins import object class TimeQuery(object): """ - Object that encapsulates a query for a particular time (read_ts, write_ts, or processed_ts) + Object that encapsulates a query for a range of time [start_time, end_time] + Can query by Unix timestamps with a '*_ts' time_type (like "metadata.write_ts", "data.ts", or "data.start_ts") + e.g. TimeQuery("metadata.write_ts", 1234567890, 1234567900) + Or, can query by ISO datetime strings with a '*_fmt_time' time_type (like "data.fmt_time" or "data.start_fmt_time") + This is useful for querying based on the local date/time at which data was collected + e.g. TimeQuery("data.fmt_time", "2024-06-03T08:00", "2024-06-03T16:59") """ - def __init__(self, timeType, startTs, endTs): - self.timeType = timeType - self.startTs = startTs - self.endTs = endTs + def __init__(self, time_type, start_time, end_time): + self.time_type = time_type + self.start_time = start_time + # if end_time is an ISO string, append 'Z' to make the end range inclusive + # (because Z is greater than any other character that can appear in an ISO string) + self.end_time = end_time + 'Z' if isinstance(end_time, str) else end_time def get_query(self): - time_key = self.timeType - ret_query = {time_key : {"$lte": self.endTs}} - if (self.startTs is not None): - ret_query[time_key].update({"$gte": self.startTs}) + time_key = self.time_type + ret_query = {time_key : {"$lte": self.end_time}} + if (self.start_time is not None): + ret_query[time_key].update({"$gte": self.start_time}) return ret_query def __repr__(self): - return f"TimeQuery {self.timeType} with range [{self.startTs}, {self.endTs})" + return f"TimeQuery {self.time_type} with range [{self.start_time}, {self.end_time})"