feat(api): dashboard include series with templates

This commit is contained in:
Taha Yassine Kraiem 2022-04-07 13:28:01 +02:00
parent 06b8366fab
commit f09a251c44
2 changed files with 14 additions and 4 deletions

View file

@ -14,8 +14,15 @@ CATEGORY_DESCRIPTION = {
def get_templates(project_id, user_id):
with pg_client.PostgresClient() as cur:
pg_query = cur.mogrify(f"""SELECT category, jsonb_agg(metrics ORDER BY name) AS widgets
FROM metrics
WHERE deleted_at IS NULL AND (project_id ISNULL OR (project_id = %(project_id)s AND (is_public OR user_id= %(userId)s)))
FROM (SELECT *
FROM metrics LEFT JOIN LATERAL (SELECT COALESCE(jsonb_agg(metric_series.* ORDER BY index), '[]'::jsonb) AS series
FROM metric_series
WHERE metric_series.metric_id = metrics.metric_id
AND metric_series.deleted_at ISNULL
) AS metric_series ON (TRUE)
WHERE deleted_at IS NULL
AND (project_id ISNULL OR (project_id = %(project_id)s AND (is_public OR user_id= %(userId)s)))
) AS metrics
GROUP BY category
ORDER BY category;""", {"project_id": project_id, "userId": user_id})
cur.execute(pg_query)

View file

@ -88,7 +88,7 @@ class TimeUTC:
return datetime.utcfromtimestamp(ts // 1000).strftime(fmt)
@staticmethod
def human_to_timestamp(ts, pattern):
def human_to_timestamp(ts, pattern="%Y-%m-%dT%H:%M:%S.%f"):
return int(datetime.strptime(ts, pattern).timestamp() * 1000)
@staticmethod
@ -96,7 +96,10 @@ class TimeUTC:
if date is None:
return None
if isinstance(date, str):
return TimeUTC.human_to_timestamp(date, "%Y-%m-%dT%H:%M:%S.%f")
fp = date.find(".")
if fp > 0:
date += '0' * (6 - len(date[fp + 1:]))
date = datetime.fromisoformat(date)
return int(datetime.timestamp(date) * 1000)
@staticmethod