From 1b748293d50193818d2ebf3fbf745225184b9670 Mon Sep 17 00:00:00 2001 From: Kraiem Taha Yassine Date: Thu, 14 Nov 2024 17:57:05 +0100 Subject: [PATCH] Dev (#2747) * fix(chalice): fixed Math-operators validation refactor(chalice): search for sessions that have events for heatmaps * refactor(chalice): search for sessions that have at least 1 location event for heatmaps * fix(chalice): fixed Math-operators validation refactor(chalice): search for sessions that have events for heatmaps * refactor(chalice): search for sessions that have at least 1 location event for heatmaps * feat(chalice): autocomplete return top 10 with stats * fix(chalice): fixed autocomplete top 10 meta-filters * feat(chalice): new webVital speed index by location --- .../core/custom_metrics_predefined.py | 23 ++++++++------- api/chalicelib/core/metrics.py | 28 +++++++++++++++++++ api/schemas/schemas.py | 3 +- ee/api/chalicelib/core/metrics.py | 27 ++++++++++++++++++ 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/api/chalicelib/core/custom_metrics_predefined.py b/api/chalicelib/core/custom_metrics_predefined.py index 6baf7896c..36c061717 100644 --- a/api/chalicelib/core/custom_metrics_predefined.py +++ b/api/chalicelib/core/custom_metrics_predefined.py @@ -8,15 +8,18 @@ logger = logging.getLogger(__name__) def get_metric(key: Union[schemas.MetricOfWebVitals, schemas.MetricOfErrors], project_id: int, data: dict): - supported = {schemas.MetricOfWebVitals.COUNT_SESSIONS: metrics.get_processed_sessions, - schemas.MetricOfWebVitals.AVG_VISITED_PAGES: metrics.get_user_activity_avg_visited_pages, - schemas.MetricOfWebVitals.COUNT_REQUESTS: metrics.get_top_metrics_count_requests, - schemas.MetricOfErrors.IMPACTED_SESSIONS_BY_JS_ERRORS: metrics.get_impacted_sessions_by_js_errors, - schemas.MetricOfErrors.DOMAINS_ERRORS_4XX: metrics.get_domains_errors_4xx, - schemas.MetricOfErrors.DOMAINS_ERRORS_5XX: metrics.get_domains_errors_5xx, - schemas.MetricOfErrors.ERRORS_PER_DOMAINS: metrics.get_errors_per_domains, - schemas.MetricOfErrors.ERRORS_PER_TYPE: metrics.get_errors_per_type, - schemas.MetricOfErrors.RESOURCES_BY_PARTY: metrics.get_resources_by_party, - schemas.MetricOfWebVitals.COUNT_USERS: metrics.get_unique_users, } + supported = { + schemas.MetricOfWebVitals.COUNT_SESSIONS: metrics.get_processed_sessions, + schemas.MetricOfWebVitals.AVG_VISITED_PAGES: metrics.get_user_activity_avg_visited_pages, + schemas.MetricOfWebVitals.COUNT_REQUESTS: metrics.get_top_metrics_count_requests, + schemas.MetricOfErrors.IMPACTED_SESSIONS_BY_JS_ERRORS: metrics.get_impacted_sessions_by_js_errors, + schemas.MetricOfErrors.DOMAINS_ERRORS_4XX: metrics.get_domains_errors_4xx, + schemas.MetricOfErrors.DOMAINS_ERRORS_5XX: metrics.get_domains_errors_5xx, + schemas.MetricOfErrors.ERRORS_PER_DOMAINS: metrics.get_errors_per_domains, + schemas.MetricOfErrors.ERRORS_PER_TYPE: metrics.get_errors_per_type, + schemas.MetricOfErrors.RESOURCES_BY_PARTY: metrics.get_resources_by_party, + schemas.MetricOfWebVitals.COUNT_USERS: metrics.get_unique_users, + schemas.MetricOfWebVitals.SPEED_LOCATION: metrics.get_speed_index_location, + } return supported.get(key, lambda *args: None)(project_id=project_id, **data) diff --git a/api/chalicelib/core/metrics.py b/api/chalicelib/core/metrics.py index 0ce787757..f71528855 100644 --- a/api/chalicelib/core/metrics.py +++ b/api/chalicelib/core/metrics.py @@ -594,3 +594,31 @@ def get_unique_users(project_id, startTimestamp=TimeUTC.now(delta_days=-1), results["progress"] = helper.__progress(old_val=count, new_val=results["value"]) results["unit"] = schemas.TemplatePredefinedUnits.COUNT return results + + +def get_speed_index_location(project_id, startTimestamp=TimeUTC.now(delta_days=-1), + endTimestamp=TimeUTC.now(), **args): + pg_sub_query = __get_constraints(project_id=project_id, data=args) + pg_sub_query.append("pages.speed_index IS NOT NULL") + pg_sub_query.append("pages.speed_index>0") + + with pg_client.PostgresClient() as cur: + pg_query = f"""SELECT sessions.user_country, AVG(pages.speed_index) AS value + FROM events.pages INNER JOIN public.sessions USING (session_id) + WHERE {" AND ".join(pg_sub_query)} + GROUP BY sessions.user_country + ORDER BY value, sessions.user_country;""" + params = {"project_id": project_id, + "startTimestamp": startTimestamp, + "endTimestamp": endTimestamp, **__get_constraint_values(args)} + cur.execute(cur.mogrify(pg_query, params)) + rows = cur.fetchall() + if len(rows) > 0: + pg_query = f"""SELECT AVG(pages.speed_index) AS avg + FROM events.pages INNER JOIN public.sessions USING (session_id) + WHERE {" AND ".join(pg_sub_query)};""" + cur.execute(cur.mogrify(pg_query, params)) + avg = cur.fetchone()["avg"] + else: + avg = 0 + return {"value": avg, "chart": helper.list_to_camel_case(rows), "unit": schemas.TemplatePredefinedUnits.MILLISECOND} diff --git a/api/schemas/schemas.py b/api/schemas/schemas.py index 805da7d85..617f8b5ea 100644 --- a/api/schemas/schemas.py +++ b/api/schemas/schemas.py @@ -933,6 +933,7 @@ class MetricOfWebVitals(str, Enum): COUNT_REQUESTS = "countRequests" COUNT_SESSIONS = "countSessions" COUNT_USERS = "userCount" + SPEED_LOCATION = "speedLocation" class MetricOfTable(str, Enum): @@ -940,8 +941,6 @@ class MetricOfTable(str, Enum): USER_BROWSER = FilterType.USER_BROWSER.value USER_DEVICE = FilterType.USER_DEVICE.value USER_COUNTRY = FilterType.USER_COUNTRY.value - # user_city = FilterType.user_city.value - # user_state = FilterType.user_state.value USER_ID = FilterType.USER_ID.value ISSUES = FilterType.ISSUE.value VISITED_URL = "location" diff --git a/ee/api/chalicelib/core/metrics.py b/ee/api/chalicelib/core/metrics.py index a0529fabc..13fb95fa4 100644 --- a/ee/api/chalicelib/core/metrics.py +++ b/ee/api/chalicelib/core/metrics.py @@ -585,3 +585,30 @@ def get_unique_users(project_id, startTimestamp=TimeUTC.now(delta_days=-1), results["progress"] = helper.__progress(old_val=count, new_val=results["value"]) results["unit"] = schemas.TemplatePredefinedUnits.COUNT return results + + +def get_speed_index_location(project_id, startTimestamp=TimeUTC.now(delta_days=-1), + endTimestamp=TimeUTC.now(), **args): + ch_sub_query = __get_basic_constraints(table_name="pages", data=args) + ch_sub_query.append("pages.event_type='LOCATION'") + ch_sub_query.append("isNotNull(pages.speed_index)") + ch_sub_query.append("pages.speed_index>0") + meta_condition = __get_meta_constraint(args) + ch_sub_query += meta_condition + + with ch_client.ClickHouseClient() as ch: + ch_query = f"""SELECT sessions.user_country, COALESCE(avgOrNull(pages.speed_index),0) AS value + FROM {exp_ch_helper.get_main_events_table(startTimestamp)} AS pages + INNER JOIN {exp_ch_helper.get_main_sessions_table(startTimestamp)} AS sessions USING (session_id) + WHERE {" AND ".join(ch_sub_query)} + GROUP BY sessions.user_country + ORDER BY value ,sessions.user_country;""" + params = {"project_id": project_id, + "startTimestamp": startTimestamp, + "endTimestamp": endTimestamp, **__get_constraint_values(args)} + rows = ch.execute(query=ch_query, params=params) + ch_query = f"""SELECT COALESCE(avgOrNull(pages.speed_index),0) AS avg + FROM {exp_ch_helper.get_main_events_table(startTimestamp)} AS pages + WHERE {" AND ".join(ch_sub_query)};""" + avg = ch.execute(query=ch_query, params=params)[0]["avg"] if len(rows) > 0 else 0 + return {"value": avg, "chart": helper.list_to_camel_case(rows), "unit": schemas.TemplatePredefinedUnits.MILLISECOND}