From ca16a90f525119552d7604bc2ec4fa71cc95ed0f Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 3 Jun 2025 15:30:20 +0200 Subject: [PATCH] refactor(chalice): changed filters response --- .../core/product_analytics/events.py | 23 ++- .../core/product_analytics/filters.py | 148 ++++++++++++++++++ .../core/product_analytics/properties.py | 10 +- api/routers/subs/product_analytics.py | 16 +- 4 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 api/chalicelib/core/product_analytics/filters.py diff --git a/api/chalicelib/core/product_analytics/events.py b/api/chalicelib/core/product_analytics/events.py index 99a2914d3..2de6f3608 100644 --- a/api/chalicelib/core/product_analytics/events.py +++ b/api/chalicelib/core/product_analytics/events.py @@ -16,20 +16,19 @@ PREDEFINED_EVENTS = [ ] -def get_events(project_id: int, page: schemas.PaginatedSchema): +def get_events(project_id: int): with ClickHouseClient() as ch_client: r = ch_client.format( - """SELECT DISTINCT - ON(event_name,auto_captured) - COUNT (1) OVER () AS total, - event_name AS name, display_name, description, - auto_captured - FROM product_analytics.all_events - WHERE project_id=%(project_id)s - ORDER BY auto_captured, display_name - LIMIT %(limit)s - OFFSET %(offset)s;""", - parameters={"project_id": project_id, "limit": page.limit, "offset": (page.page - 1) * page.limit}) + """ \ + SELECT DISTINCT + ON(event_name,auto_captured) + COUNT (1) OVER () AS total, + event_name AS name, display_name, description, + auto_captured + FROM product_analytics.all_events + WHERE project_id=%(project_id)s + ORDER BY auto_captured, display_name, event_name;""", + parameters={"project_id": project_id}) rows = ch_client.execute(r) if len(rows) == 0: return {"total": len(PREDEFINED_EVENTS), "list": [{ diff --git a/api/chalicelib/core/product_analytics/filters.py b/api/chalicelib/core/product_analytics/filters.py new file mode 100644 index 000000000..293eee6ea --- /dev/null +++ b/api/chalicelib/core/product_analytics/filters.py @@ -0,0 +1,148 @@ +import schemas + + +def get_sessions_filters(project_id: int): + return {"total": 13, + "list": [ + { + "id": "sf_1", + "name": schemas.FilterType.REFERRER, + "displayName": "Referrer", + "possibleTypes": [ + "String" + ], + "autoCaptured": True + }, + { + "id": "sf_2", + "name": schemas.FilterType.DURATION, + "displayName": "Duration", + "possibleTypes": [ + "int" + ], + "autoCaptured": True + }, + { + "id": "sf_3", + "name": schemas.FilterType.UTM_SOURCE, + "displayName": "UTM Source", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_4", + "name": schemas.FilterType.UTM_MEDIUM, + "displayName": "UTM Medium", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_5", + "name": schemas.FilterType.UTM_CAMPAIGN, + "displayName": "UTM Campaign", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_6", + "name": schemas.FilterType.USER_COUNTRY, + "displayName": "Country", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_7", + "name": schemas.FilterType.USER_CITY, + "displayName": "City", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_8", + "name": schemas.FilterType.USER_STATE, + "displayName": "State / Province", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_9", + "name": schemas.FilterType.USER_OS, + "displayName": "OS", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_10", + "name": schemas.FilterType.USER_BROWSER, + "displayName": "Browser", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_11", + "name": schemas.FilterType.USER_DEVICE, + "displayName": "Device", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_12", + "name": schemas.FilterType.PLATFORM, + "displayName": "Platform", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + }, + { + "id": "sf_13", + "name": schemas.FilterType.REV_ID, + "displayName": "Version ID", + "possibleTypes": [ + "string" + ], + "autoCaptured": True + } + ]} + + +def get_users_filters(project_id: int): + return {"total": 2, + "list": [ + { + "id": "uf_1", + "name": schemas.FilterType.USER_ID, + "displayName": "User ID", + "possibleTypes": [ + "string" + ], + "autoCaptured": False + }, + { + "id": "uf_2", + "name": schemas.FilterType.USER_ANONYMOUS_ID, + "displayName": "User Anonymous ID", + "possibleTypes": [ + "string" + ], + "autoCaptured": False + } + ]} diff --git a/api/chalicelib/core/product_analytics/properties.py b/api/chalicelib/core/product_analytics/properties.py index 24fcc7f7e..f2be2a554 100644 --- a/api/chalicelib/core/product_analytics/properties.py +++ b/api/chalicelib/core/product_analytics/properties.py @@ -67,7 +67,7 @@ EVENT_DEFAULT_PROPERTIES = { } -def get_all_properties(project_id: int, page: schemas.PaginatedSchema): +def get_all_properties(project_id: int): with ClickHouseClient() as ch_client: r = ch_client.format( """SELECT COUNT(1) OVER () AS total, property_name AS name, @@ -77,12 +77,8 @@ def get_all_properties(project_id: int, page: schemas.PaginatedSchema): LEFT JOIN product_analytics.event_properties USING (project_id, property_name) WHERE all_properties.project_id = %(project_id)s GROUP BY property_name, display_name - ORDER BY display_name - LIMIT %(limit)s - OFFSET %(offset)s;""", - parameters={"project_id": project_id, - "limit": page.limit, - "offset": (page.page - 1) * page.limit}) + ORDER BY display_name, property_name;""", + parameters={"project_id": project_id}) properties = ch_client.execute(r) if len(properties) == 0: return {"total": 0, "list": []} diff --git a/api/routers/subs/product_analytics.py b/api/routers/subs/product_analytics.py index 3b289cd25..ba414e261 100644 --- a/api/routers/subs/product_analytics.py +++ b/api/routers/subs/product_analytics.py @@ -4,7 +4,7 @@ from fastapi import Body, Depends, Query import schemas from chalicelib.core import metadata -from chalicelib.core.product_analytics import events, properties, autocomplete +from chalicelib.core.product_analytics import events, properties, autocomplete, filters from or_dependencies import OR_context from routers.base import get_routers from typing import Optional @@ -13,15 +13,13 @@ public_app, app, app_apikey = get_routers() @app.get('/{projectId}/filters', tags=["product_analytics"]) -def get_all_filters(projectId: int, filter_query: Annotated[schemas.PaginatedSchema, Query()], - context: schemas.CurrentContext = Depends(OR_context)): - # TODO: fix total attribute to return the total count instead of the total number of pages - # TODO: no pagination, return everything - # TODO: remove icon +def get_all_filters(projectId: int, context: schemas.CurrentContext = Depends(OR_context)): return { "data": { - "events": events.get_events(project_id=projectId, page=filter_query), - "filters": properties.get_all_properties(project_id=projectId, page=filter_query), + "events": events.get_events(project_id=projectId), + "eventProperties": properties.get_all_properties(project_id=projectId), + "sessionFilters": filters.get_sessions_filters(project_id=projectId), + "userFilters": filters.get_users_filters(project_id=projectId), "metadata": metadata.get_for_filters(project_id=projectId) } } @@ -30,7 +28,7 @@ def get_all_filters(projectId: int, filter_query: Annotated[schemas.PaginatedSch @app.get('/{projectId}/events/names', tags=["product_analytics"]) def get_all_events(projectId: int, filter_query: Annotated[schemas.PaginatedSchema, Query()], context: schemas.CurrentContext = Depends(OR_context)): - return {"data": events.get_events(project_id=projectId, page=filter_query)} + return {"data": events.get_events(project_id=projectId)} @app.get('/{projectId}/properties/search', tags=["product_analytics"])