From 9c3596a2ed12e6ec895b1a946fe897a1d859ac97 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Mon, 17 Oct 2022 19:25:01 +0200 Subject: [PATCH] feat(chalice): event-level-error tags --- api/chalicelib/core/errors.py | 32 +++++++--------- ee/api/chalicelib/core/errors.py | 38 ++++++++----------- .../db/init_dbs/postgresql/1.8.2/1.8.2.sql | 7 +++- .../db/init_dbs/postgresql/init_schema.sql | 7 +++- .../db/init_dbs/postgresql/1.8.2/1.8.2.sql | 7 +++- .../db/init_dbs/postgresql/init_schema.sql | 7 +++- 6 files changed, 53 insertions(+), 45 deletions(-) diff --git a/api/chalicelib/core/errors.py b/api/chalicelib/core/errors.py index 07a7fe200..20c53a07a 100644 --- a/api/chalicelib/core/errors.py +++ b/api/chalicelib/core/errors.py @@ -135,7 +135,7 @@ def get_details(project_id, error_id, user_id, **data): country_partition, chart24, chart30, - COALESCE(tags,'[]')::jsonb AS custom_tags + custom_tags FROM (SELECT error_id, name, message, @@ -146,25 +146,22 @@ def get_details(project_id, error_id, user_id, **data): INNER JOIN public.sessions USING (session_id) WHERE {" AND ".join(pg_sub_query30_err)} GROUP BY error_id, name, message) AS details - INNER JOIN (SELECT error_id, - MAX(timestamp) AS last_occurrence, + INNER JOIN (SELECT MAX(timestamp) AS last_occurrence, MIN(timestamp) AS first_occurrence FROM events.errors WHERE error_id = %(error_id)s - GROUP BY error_id) AS time_details USING (error_id) - INNER JOIN (SELECT error_id, - session_id AS last_session_id, - user_os, - user_os_version, - user_browser, - user_browser_version, - user_device, - user_device_type, - user_uuid - FROM events.errors INNER JOIN public.sessions USING (session_id) + GROUP BY error_id) AS time_details ON (TRUE) + INNER JOIN (SELECT session_id AS last_session_id, + coalesce(custom_tags, '[]')::jsonb AS custom_tags + FROM events.errors + LEFT JOIN LATERAL ( + SELECT jsonb_agg(jsonb_build_object(errors_tags.key, errors_tags.value)) AS custom_tags + FROM errors_tags + WHERE errors_tags.error_id = %(error_id)s + AND errors_tags.session_id = errors.session_id) AS errors_tags ON (TRUE) WHERE error_id = %(error_id)s ORDER BY errors.timestamp DESC - LIMIT 1) AS last_session_details USING (error_id) + LIMIT 1) AS last_session_details ON (TRUE) INNER JOIN (SELECT jsonb_agg(browser_details) AS browsers_partition FROM (SELECT * FROM (SELECT user_browser AS name, @@ -248,10 +245,7 @@ def get_details(project_id, error_id, user_id, **data): WHERE {" AND ".join(pg_sub_query30)}) AS chart_details ON (TRUE) GROUP BY timestamp - ORDER BY timestamp) AS chart_details) AS chart_details30 ON (TRUE) - LEFT JOIN (SELECT jsonb_agg(jsonb_build_object(errors_tags.key, errors_tags.value)) - FROM errors_tags INNER JOIN errors USING(error_id) - WHERE {" AND ".join(pg_basic_query)}) AS raw_tags(tags) ON (TRUE); + ORDER BY timestamp) AS chart_details) AS chart_details30 ON (TRUE); """ # print("--------------------") diff --git a/ee/api/chalicelib/core/errors.py b/ee/api/chalicelib/core/errors.py index c037c2364..e9ff405f8 100644 --- a/ee/api/chalicelib/core/errors.py +++ b/ee/api/chalicelib/core/errors.py @@ -141,7 +141,7 @@ def get_details(project_id, error_id, user_id, **data): country_partition, chart24, chart30, - COALESCE(tags,'{{}}')::jsonb AS custom_tags + custom_tags FROM (SELECT error_id, name, message, @@ -152,25 +152,22 @@ def get_details(project_id, error_id, user_id, **data): INNER JOIN public.sessions USING (session_id) WHERE {" AND ".join(pg_sub_query30_err)} GROUP BY error_id, name, message) AS details - INNER JOIN (SELECT error_id, - MAX(timestamp) AS last_occurrence, + INNER JOIN (SELECT MAX(timestamp) AS last_occurrence, MIN(timestamp) AS first_occurrence FROM events.errors WHERE error_id = %(error_id)s - GROUP BY error_id) AS time_details USING (error_id) - INNER JOIN (SELECT error_id, - session_id AS last_session_id, - user_os, - user_os_version, - user_browser, - user_browser_version, - user_device, - user_device_type, - user_uuid - FROM events.errors INNER JOIN public.sessions USING (session_id) + GROUP BY error_id) AS time_details ON (TRUE) + INNER JOIN (SELECT session_id AS last_session_id, + coalesce(custom_tags, '[]')::jsonb AS custom_tags + FROM events.errors + LEFT JOIN LATERAL ( + SELECT jsonb_agg(jsonb_build_object(errors_tags.key, errors_tags.value)) AS custom_tags + FROM errors_tags + WHERE errors_tags.error_id = %(error_id)s + AND errors_tags.session_id = errors.session_id) AS errors_tags ON (TRUE) WHERE error_id = %(error_id)s ORDER BY errors.timestamp DESC - LIMIT 1) AS last_session_details USING (error_id) + LIMIT 1) AS last_session_details ON (TRUE) INNER JOIN (SELECT jsonb_agg(browser_details) AS browsers_partition FROM (SELECT * FROM (SELECT user_browser AS name, @@ -254,15 +251,12 @@ def get_details(project_id, error_id, user_id, **data): WHERE {" AND ".join(pg_sub_query30)}) AS chart_details ON (TRUE) GROUP BY timestamp - ORDER BY timestamp) AS chart_details) AS chart_details30 ON (TRUE) - LEFT JOIN (SELECT jsonb_agg(jsonb_build_object(errors_tags.key, errors_tags.value)) - FROM errors_tags INNER JOIN errors USING(error_id) - WHERE {" AND ".join(pg_basic_query)}) AS raw_tags(tags) ON (TRUE); + ORDER BY timestamp) AS chart_details) AS chart_details30 ON (TRUE); """ - print("--------------------") - print(cur.mogrify(main_pg_query, params)) - print("--------------------") + # print("--------------------") + # print(cur.mogrify(main_pg_query, params)) + # print("--------------------") cur.execute(cur.mogrify(main_pg_query, params)) row = cur.fetchone() if row is None: diff --git a/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql b/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql index 5e0856f7e..63b3344b6 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql @@ -28,9 +28,14 @@ CREATE TABLE IF NOT EXISTS errors_tags key text NOT NULL, value text NOT NULL, created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), - error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE + error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE, + session_id bigint NOT NULL, + message_id bigint NOT NULL, + FOREIGN KEY (session_id, message_id) REFERENCES events.errors (session_id, message_id) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS errors_tags_error_id_idx ON errors_tags (error_id); +CREATE INDEX IF NOT EXISTS errors_tags_session_id_idx ON errors_tags (session_id); +CREATE INDEX IF NOT EXISTS errors_tags_message_id_idx ON errors_tags (message_id); 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 1a11bb8a9..cd8f88cb1 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -501,10 +501,15 @@ $$ key text NOT NULL, value text NOT NULL, created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), - error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE + error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE, + session_id bigint NOT NULL, + message_id bigint NOT NULL, + FOREIGN KEY (session_id, message_id) REFERENCES events.errors (session_id, message_id) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS errors_tags_error_id_idx ON errors_tags (error_id); + CREATE INDEX IF NOT EXISTS errors_tags_session_id_idx ON errors_tags (session_id); + CREATE INDEX IF NOT EXISTS errors_tags_message_id_idx ON errors_tags (message_id); IF NOT EXISTS(SELECT * FROM pg_type typ diff --git a/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql b/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql index b89eb2343..f2ad7e68e 100644 --- a/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql +++ b/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql @@ -28,9 +28,14 @@ CREATE TABLE IF NOT EXISTS errors_tags key text NOT NULL, value text NOT NULL, created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), - error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE + error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE, + session_id bigint NOT NULL, + message_id bigint NOT NULL, + FOREIGN KEY (session_id, message_id) REFERENCES events.errors (session_id, message_id) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS errors_tags_error_id_idx ON errors_tags (error_id); +CREATE INDEX IF NOT EXISTS errors_tags_session_id_idx ON errors_tags (session_id); +CREATE INDEX IF NOT EXISTS errors_tags_message_id_idx ON errors_tags (message_id); 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 fd534900d..9fe80c718 100644 --- a/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -408,10 +408,15 @@ $$ key text NOT NULL, value text NOT NULL, created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), - error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE + error_id text NOT NULL REFERENCES errors (error_id) ON DELETE CASCADE, + session_id bigint NOT NULL, + message_id bigint NOT NULL, + FOREIGN KEY (session_id, message_id) REFERENCES events.errors (session_id, message_id) ON DELETE CASCADE ); CREATE INDEX errors_tags_error_id_idx ON errors_tags (error_id); + CREATE INDEX errors_tags_session_id_idx ON errors_tags (session_id); + CREATE INDEX errors_tags_message_id_idx ON errors_tags (message_id); -- --- sessions.sql --- CREATE TYPE device_type AS ENUM ('desktop', 'tablet', 'mobile', 'other');