diff --git a/api/chalicelib/core/product_analytics/autocomplete.py b/api/chalicelib/core/product_analytics/autocomplete.py index 5915a8ab6..fafcc72d6 100644 --- a/api/chalicelib/core/product_analytics/autocomplete.py +++ b/api/chalicelib/core/product_analytics/autocomplete.py @@ -30,21 +30,23 @@ def search_properties(project_id: int, property_name: Optional[str] = None, even with ClickHouseClient() as ch_client: select = "value" full_args = {"project_id": project_id, "limit": 20, - "event_name": event_name, "property_name": property_name} + "event_name": event_name, "property_name": property_name, "q": q, + "property_name_l": helper.string_to_sql_like(property_name), + "q_l": helper.string_to_sql_like(q)} constraints = ["project_id = %(project_id)s", "_timestamp >= now()-INTERVAL 1 MONTH"] 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)s"] - full_args["property_name"] = helper.string_to_sql_like(property_name) + constraints += ["property_name ILIKE %(property_name_l)s"] + if q: - constraints += ["value ILIKE %(q)s"] - full_args["q"] = helper.string_to_sql_like(q) + constraints += ["value ILIKE %(q_l)s"] query = ch_client.format( f"""SELECT {select},data_count FROM product_analytics.autocomplete_event_properties_grouped diff --git a/api/chalicelib/utils/helper.py b/api/chalicelib/utils/helper.py index 4d0d09427..0c128d4a1 100644 --- a/api/chalicelib/utils/helper.py +++ b/api/chalicelib/utils/helper.py @@ -99,6 +99,8 @@ def allow_captcha(): def string_to_sql_like(value): + if value is None: + return None value = re.sub(' +', ' ', value) value = value.replace("*", "%") if value.startswith("^"): @@ -334,5 +336,3 @@ def cast_session_id_to_string(data): for key in keys: data[key] = cast_session_id_to_string(data[key]) return data - - diff --git a/api/routers/subs/product_analytics.py b/api/routers/subs/product_analytics.py index d7dbcba23..6141af313 100644 --- a/api/routers/subs/product_analytics.py +++ b/api/routers/subs/product_analytics.py @@ -63,8 +63,12 @@ def autocomplete_events(projectId: int, q: Optional[str] = None, @app.get('/{projectId}/properties/autocomplete', tags=["autocomplete"]) -def autocomplete_properties(projectId: int, propertyName: str, eventName: Optional[str] = None, +def autocomplete_properties(projectId: int, propertyName: Optional[str] = None, 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"]} return {"data": autocomplete.search_properties(project_id=projectId, event_name=None if not eventName \ or len(eventName) == 0 else eventName,