refactor(chalice): changed autocomplete

This commit is contained in:
Taha Yassine Kraiem 2025-05-30 17:27:04 +02:00 committed by Kraiem Taha Yassine
parent 6264e21030
commit 17b72d4242
2 changed files with 22 additions and 22 deletions

View file

@ -28,32 +28,32 @@ def search_events(project_id: int, q: Optional[str] = None):
def search_properties(project_id: int, property_name: Optional[str] = None, event_name: Optional[str] = None, def search_properties(project_id: int, property_name: Optional[str] = None, event_name: Optional[str] = None,
q: Optional[str] = None): q: Optional[str] = None):
with ClickHouseClient() as ch_client: with ClickHouseClient() as ch_client:
select = "value" select = "value, data_count"
grouping = ""
full_args = {"project_id": project_id, "limit": 20, full_args = {"project_id": project_id, "limit": 20,
"event_name": event_name, "property_name": property_name, "q": q, "event_name": event_name, "property_name": property_name,
"property_name_l": helper.string_to_sql_like(property_name),
"q_l": helper.string_to_sql_like(q)} "q_l": helper.string_to_sql_like(q)}
constraints = ["project_id = %(project_id)s", constraints = ["project_id = %(project_id)s",
"_timestamp >= now()-INTERVAL 1 MONTH"] "_timestamp >= now()-INTERVAL 1 MONTH",
"property_name = %(property_name)s"]
if event_name: if event_name:
constraints += ["event_name = %(event_name)s"] constraints += ["event_name = %(event_name)s"]
else:
if property_name and q: select = "value, sum(aepg.data_count) AS data_count"
constraints += ["property_name = %(property_name)s"] grouping = "GROUP BY 1"
elif property_name:
select = "DISTINCT ON(property_name) property_name AS value"
constraints += ["property_name ILIKE %(property_name_l)s"]
if q: if q:
constraints += ["value ILIKE %(q_l)s"] constraints += ["value ILIKE %(q_l)s"]
query = ch_client.format( query = ch_client.format(
f"""SELECT {select},data_count f"""SELECT {select}
FROM product_analytics.autocomplete_event_properties_grouped FROM product_analytics.autocomplete_event_properties_grouped AS aepg
WHERE {" AND ".join(constraints)} WHERE {" AND ".join(constraints)}
ORDER BY data_count DESC {grouping}
ORDER BY data_count DESC
LIMIT %(limit)s;""", LIMIT %(limit)s;""",
parameters=full_args) parameters=full_args)
rows = ch_client.execute(query) rows = ch_client.execute(query)
return {"values": helper.list_to_camel_case(rows), "_src": 2} return {"events": helper.list_to_camel_case(rows), "_src": 2}

View file

@ -15,6 +15,9 @@ 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, filter_query: Annotated[schemas.PaginatedSchema, Query()],
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, page=filter_query),
@ -63,15 +66,12 @@ def autocomplete_events(projectId: int, q: Optional[str] = None,
@app.get('/{projectId}/properties/autocomplete', tags=["autocomplete"]) @app.get('/{projectId}/properties/autocomplete', tags=["autocomplete"])
def autocomplete_properties(projectId: int, propertyName: Optional[str] = None, eventName: Optional[str] = None, def autocomplete_properties(projectId: int, propertyName: str, eventName: Optional[str] = None,
q: Optional[str] = None, context: schemas.CurrentContext = Depends(OR_context)): q: Optional[str] = None, context: schemas.CurrentContext = Depends(OR_context)):
if not propertyName and not eventName and not q: # Specify propertyName to get top values of that property
return {"error": ["Specify eventName to get top properties", # Specify eventName&propertyName to get top values of that property for the selected event
"Specify propertyName to get top values of that property",
"Specify eventName&propertyName to get top values of that property for the selected event"]}
return {"data": autocomplete.search_properties(project_id=projectId, return {"data": autocomplete.search_properties(project_id=projectId,
event_name=None if not eventName \ event_name=None if not eventName \
or len(eventName) == 0 else eventName, or len(eventName) == 0 else eventName,
property_name=None if not propertyName \ property_name=propertyName,
or len(propertyName) == 0 else propertyName,
q=None if not q or len(q) == 0 else q)} q=None if not q or len(q) == 0 else q)}