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,
q: Optional[str] = None):
with ClickHouseClient() as ch_client:
select = "value"
select = "value, data_count"
grouping = ""
full_args = {"project_id": project_id, "limit": 20,
"event_name": event_name, "property_name": property_name, "q": q,
"property_name_l": helper.string_to_sql_like(property_name),
"event_name": event_name, "property_name": property_name,
"q_l": helper.string_to_sql_like(q)}
constraints = ["project_id = %(project_id)s",
"_timestamp >= now()-INTERVAL 1 MONTH"]
"_timestamp >= now()-INTERVAL 1 MONTH",
"property_name = %(property_name)s"]
if event_name:
constraints += ["event_name = %(event_name)s"]
if property_name and q:
constraints += ["property_name = %(property_name)s"]
elif property_name:
select = "DISTINCT ON(property_name) property_name AS value"
constraints += ["property_name ILIKE %(property_name_l)s"]
else:
select = "value, sum(aepg.data_count) AS data_count"
grouping = "GROUP BY 1"
if q:
constraints += ["value ILIKE %(q_l)s"]
query = ch_client.format(
f"""SELECT {select},data_count
FROM product_analytics.autocomplete_event_properties_grouped
f"""SELECT {select}
FROM product_analytics.autocomplete_event_properties_grouped AS aepg
WHERE {" AND ".join(constraints)}
{grouping}
ORDER BY data_count DESC
LIMIT %(limit)s;""",
parameters=full_args)
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"])
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
return {
"data": {
"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"])
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)):
if not propertyName and not eventName and not q:
return {"error": ["Specify eventName to get top properties",
"Specify propertyName to get top values of that property",
"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,
event_name=None if not eventName \
or len(eventName) == 0 else eventName,
property_name=None if not propertyName \
or len(propertyName) == 0 else propertyName,
property_name=propertyName,
q=None if not q or len(q) == 0 else q)}