refactor(chalice): refactored autocomplete
This commit is contained in:
parent
0a5d4413ca
commit
56b6c6c7e6
3 changed files with 14 additions and 8 deletions
|
|
@ -30,21 +30,23 @@ def search_properties(project_id: int, property_name: Optional[str] = None, even
|
||||||
with ClickHouseClient() as ch_client:
|
with ClickHouseClient() as ch_client:
|
||||||
select = "value"
|
select = "value"
|
||||||
full_args = {"project_id": project_id, "limit": 20,
|
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",
|
constraints = ["project_id = %(project_id)s",
|
||||||
"_timestamp >= now()-INTERVAL 1 MONTH"]
|
"_timestamp >= now()-INTERVAL 1 MONTH"]
|
||||||
if event_name:
|
if event_name:
|
||||||
constraints += ["event_name = %(event_name)s"]
|
constraints += ["event_name = %(event_name)s"]
|
||||||
|
|
||||||
if property_name and q:
|
if property_name and q:
|
||||||
constraints += ["property_name = %(property_name)s"]
|
constraints += ["property_name = %(property_name)s"]
|
||||||
elif property_name:
|
elif property_name:
|
||||||
select = "DISTINCT ON(property_name) property_name AS value"
|
select = "DISTINCT ON(property_name) property_name AS value"
|
||||||
constraints += ["property_name ILIKE %(property_name)s"]
|
constraints += ["property_name ILIKE %(property_name_l)s"]
|
||||||
full_args["property_name"] = helper.string_to_sql_like(property_name)
|
|
||||||
if q:
|
if q:
|
||||||
constraints += ["value ILIKE %(q)s"]
|
constraints += ["value ILIKE %(q_l)s"]
|
||||||
full_args["q"] = helper.string_to_sql_like(q)
|
|
||||||
query = ch_client.format(
|
query = ch_client.format(
|
||||||
f"""SELECT {select},data_count
|
f"""SELECT {select},data_count
|
||||||
FROM product_analytics.autocomplete_event_properties_grouped
|
FROM product_analytics.autocomplete_event_properties_grouped
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,8 @@ def allow_captcha():
|
||||||
|
|
||||||
|
|
||||||
def string_to_sql_like(value):
|
def string_to_sql_like(value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
value = re.sub(' +', ' ', value)
|
value = re.sub(' +', ' ', value)
|
||||||
value = value.replace("*", "%")
|
value = value.replace("*", "%")
|
||||||
if value.startswith("^"):
|
if value.startswith("^"):
|
||||||
|
|
@ -334,5 +336,3 @@ def cast_session_id_to_string(data):
|
||||||
for key in keys:
|
for key in keys:
|
||||||
data[key] = cast_session_id_to_string(data[key])
|
data[key] = cast_session_id_to_string(data[key])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,8 +63,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: 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)):
|
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,
|
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,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue