feat(api): flat custom_metrics
This commit is contained in:
parent
258ea6d923
commit
555de84ccf
3 changed files with 27 additions and 7 deletions
|
|
@ -26,7 +26,9 @@ def try_live(project_id, data: schemas.TryCustomMetricsSchema):
|
|||
|
||||
|
||||
def make_chart(project_id, user_id, metric_id, data: schemas.CustomMetricChartPayloadSchema):
|
||||
metric = get(metric_id=metric_id, project_id=project_id, user_id=user_id)
|
||||
metric = get(metric_id=metric_id, project_id=project_id, user_id=user_id, flatten=False)
|
||||
if metric is None:
|
||||
return None
|
||||
metric: schemas.TryCustomMetricsSchema = schemas.TryCustomMetricsSchema.parse_obj({**data.dict(), **metric})
|
||||
return try_live(project_id=project_id, data=metric)
|
||||
|
||||
|
|
@ -113,10 +115,10 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche
|
|||
u AS (UPDATE metric_series
|
||||
SET name=series.name,
|
||||
filter=series.filter,
|
||||
index=series.filter.index
|
||||
index=series.index
|
||||
FROM (VALUES {",".join([f"(%(u_series_id_{s['i']})s,%(u_index_{s['i']})s,%(u_name_{s['i']})s,%(u_filter_{s['i']})s::jsonb)"
|
||||
for s in n_series])}) AS series(series_id, index, name, filter)
|
||||
WHERE metric_id =%(metric_id)s AND series_id=series.series_id
|
||||
for s in u_series])}) AS series(series_id, index, name, filter)
|
||||
WHERE metric_series.metric_id =%(metric_id)s AND metric_series.series_id=series.series_id
|
||||
RETURNING 1)""")
|
||||
if len(d_series_ids) > 0:
|
||||
sub_queries.append("""\
|
||||
|
|
@ -133,7 +135,6 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche
|
|||
cur.execute(
|
||||
query
|
||||
)
|
||||
r = cur.fetchone()
|
||||
return get(metric_id=metric_id, project_id=project_id, user_id=user_id)
|
||||
|
||||
|
||||
|
|
@ -158,6 +159,8 @@ def get_all(project_id, user_id):
|
|||
rows = cur.fetchall()
|
||||
for r in rows:
|
||||
r["created_at"] = TimeUTC.datetime_to_timestamp(r["created_at"])
|
||||
for s in r["series"]:
|
||||
s["filter"] = helper.old_search_payload_to_flat(s["filter"])
|
||||
rows = helper.list_to_camel_case(rows)
|
||||
return rows
|
||||
|
||||
|
|
@ -177,7 +180,7 @@ def delete(project_id, metric_id, user_id):
|
|||
return {"state": "success"}
|
||||
|
||||
|
||||
def get(metric_id, project_id, user_id):
|
||||
def get(metric_id, project_id, user_id, flatten=True):
|
||||
with pg_client.PostgresClient() as cur:
|
||||
cur.execute(
|
||||
cur.mogrify(
|
||||
|
|
@ -197,7 +200,12 @@ def get(metric_id, project_id, user_id):
|
|||
)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
row["created_at"] = TimeUTC.datetime_to_timestamp(row["created_at"])
|
||||
if flatten:
|
||||
for s in row["series"]:
|
||||
s["filter"] = helper.old_search_payload_to_flat(s["filter"])
|
||||
return helper.dict_to_camel_case(row)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -366,3 +366,14 @@ def has_smtp():
|
|||
|
||||
def get_edition():
|
||||
return "ee" if "ee" in config("ENTERPRISE_BUILD", default="").lower() else "foss"
|
||||
|
||||
|
||||
def old_search_payload_to_flat(values):
|
||||
# in case the old search body was passed
|
||||
if values.get("events") is not None:
|
||||
for v in values["events"]:
|
||||
v["isEvent"] = True
|
||||
for v in values.get("filters", []):
|
||||
v["isEvent"] = False
|
||||
values["filters"] = values.pop("events") + values.get("filters", [])
|
||||
return values
|
||||
|
|
|
|||
|
|
@ -629,7 +629,8 @@ class MobileSignPayloadSchema(BaseModel):
|
|||
keys: List[str] = Field(...)
|
||||
|
||||
|
||||
class CustomMetricSeriesFilterSchema(SessionsSearchPayloadSchema):
|
||||
class CustomMetricSeriesFilterSchema(FlatSessionsSearchPayloadSchema):
|
||||
# class CustomMetricSeriesFilterSchema(SessionsSearchPayloadSchema):
|
||||
startDate: Optional[int] = Field(None)
|
||||
endDate: Optional[int] = Field(None)
|
||||
sort: Optional[str] = Field(None)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue