From 61ef427b4fe826cf600d4c3c7be896e88c39a8f1 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 15 Apr 2022 10:33:34 +0200 Subject: [PATCH] feat(api): added chart to avg_first_paint metric --- api/chalicelib/core/dashboard.py | 24 +++++++++++++++++--- ee/api/chalicelib/core/dashboard.py | 35 +++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/api/chalicelib/core/dashboard.py b/api/chalicelib/core/dashboard.py index ff3489432..0c143f98a 100644 --- a/api/chalicelib/core/dashboard.py +++ b/api/chalicelib/core/dashboard.py @@ -2693,11 +2693,15 @@ def __get_user_activity_avg_session_duration(cur, project_id, startTimestamp, en def get_top_metrics_avg_response_time(project_id, startTimestamp=TimeUTC.now(delta_days=-1), - endTimestamp=TimeUTC.now(), value=None, **args): + endTimestamp=TimeUTC.now(), value=None,density=20, **args): + step_size = __get_step_size(startTimestamp, endTimestamp, density, factor=1) pg_sub_query = __get_constraints(project_id=project_id, data=args) + pg_sub_query_chart = __get_constraints(project_id=project_id, time_constraint=True, + chart=True, data=args) if value is not None: pg_sub_query.append("pages.path = %(value)s") + pg_sub_query_chart.append("pages.path = %(value)s") with pg_client.PostgresClient() as cur: pg_query = f"""SELECT COALESCE(AVG(pages.response_time), 0) AS value FROM events.pages @@ -2706,11 +2710,25 @@ def get_top_metrics_avg_response_time(project_id, startTimestamp=TimeUTC.now(del AND pages.timestamp >= %(startTimestamp)s AND pages.timestamp < %(endTimestamp)s AND pages.response_time > 0;""" - cur.execute(cur.mogrify(pg_query, {"project_id": project_id, + params={"step_size":step_size,"project_id": project_id, "startTimestamp": startTimestamp, "endTimestamp": endTimestamp, - "value": value, **__get_constraint_values(args)})) + "value": value, **__get_constraint_values(args)} + cur.execute(cur.mogrify(pg_query, params)) row = cur.fetchone() + pg_query = f"""SELECT generated_timestamp AS timestamp, + COALESCE(AVG(pages.response_time),0) AS value + FROM generate_series(%(startTimestamp)s, %(endTimestamp)s, %(step_size)s) AS generated_timestamp + LEFT JOIN LATERAL ( + SELECT first_paint_time + FROM events.pages INNER JOIN public.sessions USING (session_id) + WHERE {" AND ".join(pg_sub_query_chart)} AND pages.response_time > 0 + ) AS pages ON (TRUE) + GROUP BY generated_timestamp + ORDER BY generated_timestamp ASC;""" + cur.execute(cur.mogrify(pg_query, params)) + rows = cur.fetchall() + row["chart"] = helper.list_to_camel_case(rows) row["unit"] = schemas.TemplatePredefinedUnits.millisecond return helper.dict_to_camel_case(row) diff --git a/ee/api/chalicelib/core/dashboard.py b/ee/api/chalicelib/core/dashboard.py index f3daa6e67..c9a33f489 100644 --- a/ee/api/chalicelib/core/dashboard.py +++ b/ee/api/chalicelib/core/dashboard.py @@ -2544,23 +2544,40 @@ def __get_user_activity_avg_session_duration(cur, project_id, startTimestamp, en def get_top_metrics_avg_response_time(project_id, startTimestamp=TimeUTC.now(delta_days=-1), - endTimestamp=TimeUTC.now(), value=None, **args): - ch_sub_query = __get_basic_constraints(table_name="pages", data=args) + endTimestamp=TimeUTC.now(), value=None, density=20, **args): + step_size = __get_step_size(endTimestamp=endTimestamp, startTimestamp=startTimestamp, density=density) + ch_sub_query_chart = __get_basic_constraints(table_name="pages", round_start=True, data=args) meta_condition = __get_meta_constraint(args) + ch_sub_query_chart += meta_condition + ch_sub_query = __get_basic_constraints(table_name="pages", data=args) ch_sub_query += meta_condition if value is not None: ch_sub_query.append("pages.url_path = %(value)s") + ch_sub_query_chart.append("pages.url_path = %(value)s") with ch_client.ClickHouseClient() as ch: ch_query = f"""SELECT COALESCE(AVG(pages.response_time),0) AS value FROM pages {"INNER JOIN sessions_metadata USING(session_id)" if len(meta_condition) > 0 else ""} WHERE {" AND ".join(ch_sub_query)} AND isNotNull(pages.response_time) AND pages.response_time>0;""" - rows = ch.execute(query=ch_query, - params={"project_id": project_id, - "startTimestamp": startTimestamp, - "endTimestamp": endTimestamp, - "value": value, **__get_constraint_values(args)}) - return helper.dict_to_camel_case(rows[0]) + params = {"step_size": step_size, "project_id": project_id, + "startTimestamp": startTimestamp, + "endTimestamp": endTimestamp, + "value": value, **__get_constraint_values(args)} + rows = ch.execute(query=ch_query, params=params) + results = rows[0] + ch_query = f"""SELECT toUnixTimestamp(toStartOfInterval(pages.datetime, INTERVAL %(step_size)s second ))*1000 AS timestamp, + COUNT(pages.response_time) AS value + FROM pages {"INNER JOIN sessions_metadata USING(session_id)" if len(meta_condition) > 0 else ""} + WHERE {" AND ".join(ch_sub_query_chart)} AND isNotNull(pages.response_time) AND pages.response_time>0 + GROUP BY timestamp + ORDER BY timestamp;""" + rows = ch.execute(query=ch_query, params={**params, **__get_constraint_values(args)}) + rows = __complete_missing_steps(rows=rows, start_time=startTimestamp, + end_time=endTimestamp, + density=density, neutral={"value": 0}) + results["chart"] = rows + results["unit"] = schemas.TemplatePredefinedUnits.millisecond + return helper.dict_to_camel_case(results) def get_top_metrics_count_requests(project_id, startTimestamp=TimeUTC.now(delta_days=-1), @@ -2596,7 +2613,7 @@ def get_top_metrics_count_requests(project_id, startTimestamp=TimeUTC.now(delta_ end_time=endTimestamp, density=density, neutral={"value": 0}) result["chart"] = rows - result["unit"] = schemas.TemplatePredefinedUnits.count + result["unit"] = schemas.TemplatePredefinedUnits.count return helper.dict_to_camel_case(result)