refactor(chalice): changed filters response

This commit is contained in:
Taha Yassine Kraiem 2025-06-03 15:30:20 +02:00 committed by Kraiem Taha Yassine
parent c8f7a2e453
commit da476b2b86
4 changed files with 169 additions and 28 deletions

View file

@ -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: with ClickHouseClient() as ch_client:
r = ch_client.format( r = ch_client.format(
"""SELECT DISTINCT """ \
ON(event_name,auto_captured) SELECT DISTINCT
COUNT (1) OVER () AS total, ON(event_name,auto_captured)
event_name AS name, display_name, description, COUNT (1) OVER () AS total,
auto_captured event_name AS name, display_name, description,
FROM product_analytics.all_events auto_captured
WHERE project_id=%(project_id)s FROM product_analytics.all_events
ORDER BY auto_captured, display_name WHERE project_id=%(project_id)s
LIMIT %(limit)s ORDER BY auto_captured, display_name, event_name;""",
OFFSET %(offset)s;""", parameters={"project_id": project_id})
parameters={"project_id": project_id, "limit": page.limit, "offset": (page.page - 1) * page.limit})
rows = ch_client.execute(r) rows = ch_client.execute(r)
if len(rows) == 0: if len(rows) == 0:
return {"total": len(PREDEFINED_EVENTS), "list": [{ return {"total": len(PREDEFINED_EVENTS), "list": [{

View file

@ -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
}
]}

View file

@ -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: with ClickHouseClient() as ch_client:
r = ch_client.format( r = ch_client.format(
"""SELECT COUNT(1) OVER () AS total, property_name AS name, """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) LEFT JOIN product_analytics.event_properties USING (project_id, property_name)
WHERE all_properties.project_id = %(project_id)s WHERE all_properties.project_id = %(project_id)s
GROUP BY property_name, display_name GROUP BY property_name, display_name
ORDER BY display_name ORDER BY display_name, property_name;""",
LIMIT %(limit)s parameters={"project_id": project_id})
OFFSET %(offset)s;""",
parameters={"project_id": project_id,
"limit": page.limit,
"offset": (page.page - 1) * page.limit})
properties = ch_client.execute(r) properties = ch_client.execute(r)
if len(properties) == 0: if len(properties) == 0:
return {"total": 0, "list": []} return {"total": 0, "list": []}

View file

@ -4,7 +4,7 @@ from fastapi import Body, Depends, Query
import schemas import schemas
from chalicelib.core import metadata 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 or_dependencies import OR_context
from routers.base import get_routers from routers.base import get_routers
from typing import Optional from typing import Optional
@ -13,15 +13,13 @@ public_app, app, app_apikey = get_routers()
@app.get('/{projectId}/filters', tags=["product_analytics"]) @app.get('/{projectId}/filters', tags=["product_analytics"])
def get_all_filters(projectId: int, filter_query: Annotated[schemas.PaginatedSchema, Query()], def get_all_filters(projectId: int, context: schemas.CurrentContext = Depends(OR_context)):
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
return { return {
"data": { "data": {
"events": events.get_events(project_id=projectId, page=filter_query), "events": events.get_events(project_id=projectId),
"filters": properties.get_all_properties(project_id=projectId, page=filter_query), "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) "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"]) @app.get('/{projectId}/events/names', tags=["product_analytics"])
def get_all_events(projectId: int, filter_query: Annotated[schemas.PaginatedSchema, Query()], def get_all_events(projectId: int, filter_query: Annotated[schemas.PaginatedSchema, Query()],
context: schemas.CurrentContext = Depends(OR_context)): 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"]) @app.get('/{projectId}/properties/search', tags=["product_analytics"])