From 8c151e5bea5307949ac9b499d6176fb07481fe3f Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Wed, 2 Mar 2022 20:01:59 +0100 Subject: [PATCH 1/3] feat(api): custom_metrics support metric_format --- api/chalicelib/core/custom_metrics.py | 11 +++++++---- api/schemas.py | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/api/chalicelib/core/custom_metrics.py b/api/chalicelib/core/custom_metrics.py index a48723ec5..ecb61732f 100644 --- a/api/chalicelib/core/custom_metrics.py +++ b/api/chalicelib/core/custom_metrics.py @@ -85,9 +85,10 @@ def create(project_id, user_id, data: schemas.CreateCustomMetricsSchema): data.series = None params = {"user_id": user_id, "project_id": project_id, **data.dict(), **_data} query = cur.mogrify(f"""\ - WITH m AS (INSERT INTO metrics (project_id, user_id, name, is_public, view_type, metric_type, metric_of, metric_value) + WITH m AS (INSERT INTO metrics (project_id, user_id, name, is_public, + view_type, metric_type, metric_of, metric_value, metric_format) VALUES (%(project_id)s, %(user_id)s, %(name)s, %(is_public)s, - %(view_type)s, %(metric_type)s, %(metric_of)s, %(metric_value)s) + %(view_type)s, %(metric_type)s, %(metric_of)s, %(metric_value)s, %(metric_format)s) RETURNING *) INSERT INTO metric_series(metric_id, index, name, filter) @@ -113,7 +114,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche u_series_ids = [] 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_type": data.metric_type, "metric_of": data.metric_of, + "metric_value": data.metric_value, "metric_format": data.metric_format} for i, s in enumerate(data.series): prefix = "u_" if s.series_id is None or s.series_id not in series_ids: @@ -160,7 +162,8 @@ def update(metric_id, user_id, project_id, data: schemas.UpdateCustomMetricsSche UPDATE metrics SET name = %(name)s, is_public= %(is_public)s, view_type= %(view_type)s, metric_type= %(metric_type)s, - metric_of= %(metric_of)s, metric_value= %(metric_value)s + metric_of= %(metric_of)s, metric_value= %(metric_value)s, + metric_format= %(metric_format)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/schemas.py b/api/schemas.py index 40cc8e24d..7245dd82a 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -775,6 +775,7 @@ class CreateCustomMetricsSchema(CustomMetricChartPayloadSchema): metric_type: MetricType = Field(MetricType.timeseries) metric_of: Union[TableMetricOfType, TimeseriesMetricOfType] = Field(TableMetricOfType.user_id) metric_value: List[IssueType] = Field([]) + metric_format: Optional[str] = Field(None) # metricFraction: float = Field(None, gt=0, lt=1) @root_validator From fb72c2894b922a0504601f0ed4325e83c5cf8324 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Wed, 2 Mar 2022 20:13:18 +0100 Subject: [PATCH 2/3] feat(api): custom_metrics support metric_format feat(api): custom_metrics metric_value accept wrong values --- api/schemas.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api/schemas.py b/api/schemas.py index 7245dd82a..6c94e2efa 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -778,6 +778,15 @@ class CreateCustomMetricsSchema(CustomMetricChartPayloadSchema): metric_format: Optional[str] = Field(None) # metricFraction: float = Field(None, gt=0, lt=1) + # This is used to handle wrong values sent by the UI + @root_validator(pre=True) + def remove_metric_value(cls, values): + if values.get("metric_type") == MetricType.timeseries \ + or values.get("metric_type") == MetricType.table \ + and values.get("metric_of") != TableMetricOfType.issues: + values["metric_of"] = [] + return values + @root_validator def validator(cls, values): if values.get("metric_type") == MetricType.table: From 27bb11e009294e34b7f86e6e172fea1fd2bcc808 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Wed, 2 Mar 2022 20:15:40 +0100 Subject: [PATCH 3/3] feat(DB): add metric_value and metric_format to custom_metrics --- .../db/init_dbs/postgresql/1.5.99/1.5.99.sql | 10 +++++--- .../db/init_dbs/postgresql/init_schema.sql | 24 ++++++++++--------- .../db/init_dbs/postgresql/1.5.99/1.5.99.sql | 10 +++++--- .../db/init_dbs/postgresql/init_schema.sql | 24 ++++++++++--------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/ee/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql b/ee/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql index e89a97f47..88e1aab74 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql @@ -41,10 +41,14 @@ LANGUAGE plpgsql; ALTER TABLE metrics ADD COLUMN IF NOT EXISTS - metric_type metric_type NOT NULL DEFAULT 'timeseries', + metric_type metric_type NOT NULL DEFAULT 'timeseries', ADD COLUMN IF NOT EXISTS - view_type metric_view_type NOT NULL DEFAULT 'lineChart', + view_type metric_view_type NOT NULL DEFAULT 'lineChart', ADD COLUMN IF NOT EXISTS - metric_of text NOT NULL DEFAULT 'sessionCount'; + metric_of text NOT NULL DEFAULT 'sessionCount', + ADD COLUMN IF NOT EXISTS + metric_value text[] NOT NULL DEFAULT '{}'::text[], + ADD COLUMN IF NOT EXISTS + metric_format text; COMMIT; \ No newline at end of file diff --git a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql index 2cca09903..1f0f4d6f1 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -774,17 +774,19 @@ $$ CREATE TYPE metric_view_type AS ENUM ('lineChart','progress','table','pieChart'); CREATE TABLE IF NOT EXISTS metrics ( - metric_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, - project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, - user_id integer REFERENCES users (user_id) ON DELETE SET NULL, - name text NOT NULL, - is_public boolean NOT NULL DEFAULT FALSE, - active boolean NOT NULL DEFAULT TRUE, - created_at timestamp DEFAULT timezone('utc'::text, now()) not null, - deleted_at timestamp, - metric_type metric_type NOT NULL DEFAULT 'timeseries', - view_type metric_view_type NOT NULL DEFAULT 'lineChart', - metric_of text NOT NULL DEFAULT 'sessionCount' + metric_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, + project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, + user_id integer REFERENCES users (user_id) ON DELETE SET NULL, + name text NOT NULL, + is_public boolean NOT NULL DEFAULT FALSE, + active boolean NOT NULL DEFAULT TRUE, + created_at timestamp DEFAULT timezone('utc'::text, now()) not null, + deleted_at timestamp, + metric_type metric_type NOT NULL DEFAULT 'timeseries', + view_type metric_view_type NOT NULL DEFAULT 'lineChart', + metric_of text NOT NULL DEFAULT 'sessionCount', + metric_value text[] NOT NULL DEFAULT '{}'::text[], + metric_format text ); CREATE INDEX IF NOT EXISTS metrics_user_id_is_public_idx ON public.metrics (user_id, is_public); CREATE TABLE IF NOT EXISTS metric_series diff --git a/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql b/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql index 704e08ab5..b471511f4 100644 --- a/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql +++ b/scripts/helm/db/init_dbs/postgresql/1.5.99/1.5.99.sql @@ -40,10 +40,14 @@ LANGUAGE plpgsql; ALTER TABLE metrics ADD COLUMN IF NOT EXISTS - metric_type metric_type NOT NULL DEFAULT 'timeseries', + metric_type metric_type NOT NULL DEFAULT 'timeseries', ADD COLUMN IF NOT EXISTS - view_type metric_view_type NOT NULL DEFAULT 'lineChart', + view_type metric_view_type NOT NULL DEFAULT 'lineChart', ADD COLUMN IF NOT EXISTS - metric_of text NOT NULL DEFAULT 'sessionCount'; + metric_of text NOT NULL DEFAULT 'sessionCount', + ADD COLUMN IF NOT EXISTS + metric_value text[] NOT NULL DEFAULT '{}'::text[], + ADD COLUMN IF NOT EXISTS + metric_format text; COMMIT; \ No newline at end of file diff --git a/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/scripts/helm/db/init_dbs/postgresql/init_schema.sql index 51fa20242..232596696 100644 --- a/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -904,17 +904,19 @@ $$ CREATE TYPE metric_view_type AS ENUM ('lineChart','progress','table','pieChart'); CREATE TABLE metrics ( - metric_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, - project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, - user_id integer REFERENCES users (user_id) ON DELETE SET NULL, - name text NOT NULL, - is_public boolean NOT NULL DEFAULT FALSE, - active boolean NOT NULL DEFAULT TRUE, - created_at timestamp default timezone('utc'::text, now()) not null, - deleted_at timestamp, - metric_type metric_type NOT NULL DEFAULT 'timeseries', - view_type metric_view_type NOT NULL DEFAULT 'lineChart', - metric_of text NOT NULL DEFAULT 'sessionCount' + metric_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, + project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, + user_id integer REFERENCES users (user_id) ON DELETE SET NULL, + name text NOT NULL, + is_public boolean NOT NULL DEFAULT FALSE, + active boolean NOT NULL DEFAULT TRUE, + created_at timestamp default timezone('utc'::text, now()) not null, + deleted_at timestamp, + metric_type metric_type NOT NULL DEFAULT 'timeseries', + view_type metric_view_type NOT NULL DEFAULT 'lineChart', + metric_of text NOT NULL DEFAULT 'sessionCount', + metric_value text[] NOT NULL DEFAULT '{}'::text[], + metric_format text ); CREATE INDEX metrics_user_id_is_public_idx ON public.metrics (user_id, is_public); CREATE TABLE metric_series