From d75735696f6adf54eda52ca3a57fafcb510f1764 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 9 Aug 2024 17:26:59 +0100 Subject: [PATCH] feat(chalice): table of referrers --- api/chalicelib/core/custom_metrics.py | 5 +++++ api/chalicelib/core/sessions.py | 8 ++++++-- api/schemas/schemas.py | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/api/chalicelib/core/custom_metrics.py b/api/chalicelib/core/custom_metrics.py index e26e480201..7c2325e46e 100644 --- a/api/chalicelib/core/custom_metrics.py +++ b/api/chalicelib/core/custom_metrics.py @@ -137,6 +137,10 @@ def __get_table_of_urls(project_id: int, data: schemas.CardTable, user_id: int = return __get_table_of_series(project_id=project_id, data=data) +def __get_table_of_referrers(project_id: int, data: schemas.CardTable, user_id: int = None): + return __get_table_of_series(project_id=project_id, data=data) + + def __get_table_chart(project_id: int, data: schemas.CardTable, user_id: int): supported = { schemas.MetricOfTable.SESSIONS: __get_table_of_sessions, @@ -147,6 +151,7 @@ def __get_table_chart(project_id: int, data: schemas.CardTable, user_id: int): schemas.MetricOfTable.USER_DEVICE: __get_table_of_devises, schemas.MetricOfTable.USER_COUNTRY: __get_table_of_countries, schemas.MetricOfTable.VISITED_URL: __get_table_of_urls, + schemas.MetricOfTable.REFERRER: __get_table_of_referrers, } return supported.get(data.metric_of, not_supported)(project_id=project_id, data=data, user_id=user_id) diff --git a/api/chalicelib/core/sessions.py b/api/chalicelib/core/sessions.py index 9d88e1265c..f7cf63f1f0 100644 --- a/api/chalicelib/core/sessions.py +++ b/api/chalicelib/core/sessions.py @@ -357,9 +357,13 @@ def search2_table(data: schemas.SessionsSearchPayloadSchema, project_id: int, de main_col = "path" extra_col = ", path" distinct_on += ",path" + elif metric_of == schemas.MetricOfTable.REFERRER: + main_col = "referrer" + extra_col = ", referrer" + if metric_format == schemas.MetricExtendedFormatType.SESSION_COUNT: main_query = f"""SELECT COUNT(*) AS count, - COALESCE(SUM(users_sessions.session_count),0) AS count, + COALESCE(SUM(users_sessions.total),0) AS total, COALESCE(JSONB_AGG(users_sessions) FILTER ( WHERE rn > %(limit_s)s AND rn <= %(limit_e)s ), '[]'::JSONB) AS values @@ -376,7 +380,7 @@ def search2_table(data: schemas.SessionsSearchPayloadSchema, project_id: int, de ) AS full_sessions {extra_where} GROUP BY {main_col} - ORDER BY session_count DESC) AS users_sessions;""" + ORDER BY total DESC) AS users_sessions;""" else: main_query = f"""SELECT COUNT(*) AS count, COALESCE(SUM(users_sessions.user_count),0) AS count, diff --git a/api/schemas/schemas.py b/api/schemas/schemas.py index 145b8cd9db..8154ede067 100644 --- a/api/schemas/schemas.py +++ b/api/schemas/schemas.py @@ -1008,6 +1008,7 @@ class MetricOfTable(str, Enum): VISITED_URL = "location" SESSIONS = "sessions" ERRORS = "jsException" + REFERRER = "referrer" class MetricOfTimeseries(str, Enum):