From fdaa60d16cdd73f1fc453572116946b375f4ff74 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 25 Nov 2022 19:27:25 +0100 Subject: [PATCH] feat(chalice): support changing metric's config feat(chalice): merge widget and metric's config --- api/chalicelib/core/custom_metrics.py | 6 ++++-- api/chalicelib/core/dashboards.py | 2 ++ api/schemas.py | 6 +++--- ee/api/chalicelib/core/custom_metrics.py | 6 ++++-- ee/api/chalicelib/core/dashboards.py | 2 ++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/api/chalicelib/core/custom_metrics.py b/api/chalicelib/core/custom_metrics.py index 29c4b6fa9..743ca41e5 100644 --- a/api/chalicelib/core/custom_metrics.py +++ b/api/chalicelib/core/custom_metrics.py @@ -266,7 +266,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche params = {"metric_id": metric_id, "is_public": data.is_public, "name": data.name, "user_id": user_id, "project_id": project_id, "view_type": data.view_type, "metric_type": data.metric_type, "metric_of": data.metric_of, - "metric_value": data.metric_value, "metric_format": data.metric_format} + "metric_value": data.metric_value, "metric_format": data.metric_format, + "config": json.dumps(data.config.dict())} for i, s in enumerate(data.series): prefix = "u_" if s.index is None: @@ -316,7 +317,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche view_type= %(view_type)s, metric_type= %(metric_type)s, metric_of= %(metric_of)s, metric_value= %(metric_value)s, metric_format= %(metric_format)s, - edited_at = timezone('utc'::text, now()) + edited_at = timezone('utc'::text, now()), + default_config = %(config)s WHERE metric_id = %(metric_id)s AND project_id = %(project_id)s AND (user_id = %(user_id)s OR is_public) diff --git a/api/chalicelib/core/dashboards.py b/api/chalicelib/core/dashboards.py index 9d1dc4c81..ac98b44e7 100644 --- a/api/chalicelib/core/dashboards.py +++ b/api/chalicelib/core/dashboards.py @@ -111,6 +111,8 @@ def get_dashboard(project_id, user_id, dashboard_id): for w in row["widgets"]: w["created_at"] = TimeUTC.datetime_to_timestamp(w["created_at"]) w["edited_at"] = TimeUTC.datetime_to_timestamp(w["edited_at"]) + w["config"]["col"] = w["default_config"]["col"] + w["config"]["row"] = w["default_config"]["row"] for s in w["series"]: s["created_at"] = TimeUTC.datetime_to_timestamp(s["created_at"]) return helper.dict_to_camel_case(row) diff --git a/api/schemas.py b/api/schemas.py index f1f3d9cb7..960b89e76 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -874,14 +874,14 @@ class TryCustomMetricsPayloadSchema(CustomMetricChartPayloadSchema): class CustomMetricsConfigSchema(BaseModel): - col: Optional[int] = Field(default=2) - row: Optional[int] = Field(default=2) + col: Optional[int] = Field(...) + row: Optional[int] = Field(...) position: Optional[int] = Field(default=0) class CreateCustomMetricsSchema(TryCustomMetricsPayloadSchema): series: List[CustomMetricCreateSeriesSchema] = Field(..., min_items=1) - config: CustomMetricsConfigSchema = Field(default=CustomMetricsConfigSchema()) + config: CustomMetricsConfigSchema = Field(...) @root_validator(pre=True) def transform_series(cls, values): diff --git a/ee/api/chalicelib/core/custom_metrics.py b/ee/api/chalicelib/core/custom_metrics.py index b925429f6..e871a5646 100644 --- a/ee/api/chalicelib/core/custom_metrics.py +++ b/ee/api/chalicelib/core/custom_metrics.py @@ -279,7 +279,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche params = {"metric_id": metric_id, "is_public": data.is_public, "name": data.name, "user_id": user_id, "project_id": project_id, "view_type": data.view_type, "metric_type": data.metric_type, "metric_of": data.metric_of, - "metric_value": data.metric_value, "metric_format": data.metric_format} + "metric_value": data.metric_value, "metric_format": data.metric_format, + "config": json.dumps(data.config.dict())} for i, s in enumerate(data.series): prefix = "u_" if s.index is None: @@ -329,7 +330,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche view_type= %(view_type)s, metric_type= %(metric_type)s, metric_of= %(metric_of)s, metric_value= %(metric_value)s, metric_format= %(metric_format)s, - edited_at = timezone('utc'::text, now()) + edited_at = timezone('utc'::text, now()), + default_config = %(config)s WHERE metric_id = %(metric_id)s AND project_id = %(project_id)s AND (user_id = %(user_id)s OR is_public) diff --git a/ee/api/chalicelib/core/dashboards.py b/ee/api/chalicelib/core/dashboards.py index d96356df1..25b1551d3 100644 --- a/ee/api/chalicelib/core/dashboards.py +++ b/ee/api/chalicelib/core/dashboards.py @@ -118,6 +118,8 @@ def get_dashboard(project_id, user_id, dashboard_id): for w in row["widgets"]: w["created_at"] = TimeUTC.datetime_to_timestamp(w["created_at"]) w["edited_at"] = TimeUTC.datetime_to_timestamp(w["edited_at"]) + w["config"]["col"] = w["default_config"]["col"] + w["config"]["row"] = w["default_config"]["row"] for s in w["series"]: s["created_at"] = TimeUTC.datetime_to_timestamp(s["created_at"]) return helper.dict_to_camel_case(row)