From 329fcfb9d7948af1ba8359318878e44ea9e5c42d Mon Sep 17 00:00:00 2001 From: Kraiem Taha Yassine Date: Tue, 11 Jul 2023 15:37:25 +0100 Subject: [PATCH] Api v1.14.0 (#1405) * fix(DB): fixed mismatching changes for feature flags * refactor(chalice): optimized project's recording-status check * fix(chalice): defined project's recording-status check for exp-sessions --- api/chalicelib/core/sessions.py | 33 ++++++++---------- ee/api/chalicelib/core/sessions_exp.py | 29 ++++++++++++++++ .../db/init_dbs/postgresql/1.14.0/1.14.0.sql | 17 ++++++++-- .../db/init_dbs/postgresql/init_schema.sql | 34 +++++++++++++------ .../db/init_dbs/postgresql/init_schema.sql | 11 +++--- 5 files changed, 86 insertions(+), 38 deletions(-) diff --git a/api/chalicelib/core/sessions.py b/api/chalicelib/core/sessions.py index d4726ecd2..9b5335326 100644 --- a/api/chalicelib/core/sessions.py +++ b/api/chalicelib/core/sessions.py @@ -1111,21 +1111,18 @@ def session_exists(project_id, session_id): def check_recording_status(project_id: int) -> dict: query = f""" - WITH project_sessions AS ( - SELECT * FROM public.sessions WHERE project_id = %(project_id)s - ), - sessions_with_duration AS ( - SELECT * FROM project_sessions WHERE duration IS NOT NULL - ) - SELECT - CASE - WHEN (SELECT COUNT(*) FROM project_sessions) = 0 THEN 0 - WHEN (SELECT COUNT(*) FROM project_sessions) > 0 AND - (SELECT COUNT(*) FROM sessions_with_duration) = 0 THEN 1 - WHEN (SELECT COUNT(*) FROM project_sessions) > 0 AND - (SELECT COUNT(*) FROM sessions_with_duration) > 0 THEN 2 - END AS recording_status, - COUNT(*) AS sessions_count + WITH project_sessions AS (SELECT COUNT(1) AS full_count, + COUNT(1) FILTER ( WHERE duration IS NOT NULL) AS nn_duration_count + FROM public.sessions + WHERE project_id = %(project_id)s + AND start_ts >= (extract(EPOCH FROM now() - INTERVAL '1 day')) * 1000 + AND start_ts <= (extract(EPOCH FROM now() + INTERVAL '1 day')) * 1000) + SELECT CASE + WHEN full_count = 0 THEN 0 + WHEN nn_duration_count = 0 THEN 1 + ELSE 2 + END AS recording_status, + full_count AS sessions_count FROM project_sessions; """ @@ -1135,8 +1132,6 @@ def check_recording_status(project_id: int) -> dict: row = cur.fetchone() return { - "recording_status": row["recording_status"], - "sessions_count": row["sessions_count"] + "recordingStatus": row["recording_status"], + "sessionsCount": row["sessions_count"] } - - diff --git a/ee/api/chalicelib/core/sessions_exp.py b/ee/api/chalicelib/core/sessions_exp.py index 6c9ce57ed..08e572740 100644 --- a/ee/api/chalicelib/core/sessions_exp.py +++ b/ee/api/chalicelib/core/sessions_exp.py @@ -1438,3 +1438,32 @@ def session_exists(project_id, session_id): {"project_id": project_id, "session_id": session_id}) row = cur.execute(query) return row is not None + + +# TODO: support this for CH +def check_recording_status(project_id: int) -> dict: + query = f""" + WITH project_sessions AS (SELECT COUNT(1) AS full_count, + COUNT(1) FILTER ( WHERE duration IS NOT NULL) AS nn_duration_count + FROM public.sessions + WHERE project_id = %(project_id)s + AND start_ts >= (extract(EPOCH FROM now() - INTERVAL '1 day')) * 1000 + AND start_ts <= (extract(EPOCH FROM now() + INTERVAL '1 day')) * 1000) + SELECT CASE + WHEN full_count = 0 THEN 0 + WHEN nn_duration_count = 0 THEN 1 + ELSE 2 + END AS recording_status, + full_count AS sessions_count + FROM project_sessions; + """ + + with pg_client.PostgresClient() as cur: + query = cur.mogrify(query, {"project_id": project_id}) + cur.execute(query) + row = cur.fetchone() + + return { + "recordingStatus": row["recording_status"], + "sessionsCount": row["sessions_count"] + } diff --git a/ee/scripts/schema/db/init_dbs/postgresql/1.14.0/1.14.0.sql b/ee/scripts/schema/db/init_dbs/postgresql/1.14.0/1.14.0.sql index 2f49e8894..527f8bfd1 100644 --- a/ee/scripts/schema/db/init_dbs/postgresql/1.14.0/1.14.0.sql +++ b/ee/scripts/schema/db/init_dbs/postgresql/1.14.0/1.14.0.sql @@ -23,9 +23,9 @@ CREATE TABLE IF NOT EXISTS public.feature_flags ( feature_flag_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, - name text NOT NULL, flag_key text NOT NULL, - description text NOT NULL, + description text DEFAULT NULL, + payload text DEFAULT NULL, flag_type text NOT NULL, is_persist boolean NOT NULL DEFAULT FALSE, is_active boolean NOT NULL DEFAULT FALSE, @@ -38,6 +38,9 @@ CREATE TABLE IF NOT EXISTS public.feature_flags CREATE INDEX IF NOT EXISTS idx_feature_flags_project_id ON public.feature_flags (project_id); +ALTER TABLE feature_flags + ADD CONSTRAINT unique_project_flag_deleted UNIQUE (project_id, flag_key, deleted_at); + CREATE TABLE IF NOT EXISTS public.feature_flags_conditions ( condition_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, @@ -47,6 +50,16 @@ CREATE TABLE IF NOT EXISTS public.feature_flags_conditions filters jsonb NOT NULL DEFAULT '[]'::jsonb ); +CREATE TABLE IF NOT EXISTS public.feature_flags_variants +( + variant_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, + feature_flag_id integer NOT NULL REFERENCES feature_flags (feature_flag_id) ON DELETE CASCADE, + value text NOT NULL, + description text DEFAULT NULL, + payload jsonb DEFAULT NULL, + rollout_percentage integer DEFAULT 0 +); + CREATE TABLE IF NOT EXISTS public.sessions_feature_flags ( session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, diff --git a/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql index 8baf479f7..d516c35f5 100644 --- a/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql @@ -1,4 +1,5 @@ BEGIN; +-- Schemas and functions definitions: CREATE SCHEMA IF NOT EXISTS events_common; CREATE SCHEMA IF NOT EXISTS events; CREATE EXTENSION IF NOT EXISTS pg_trgm; @@ -30,7 +31,6 @@ end; $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION events.funnel(steps integer[], m integer) RETURNS boolean AS $$ DECLARE @@ -55,7 +55,6 @@ END; $$ LANGUAGE plpgsql IMMUTABLE; - CREATE OR REPLACE FUNCTION notify_integration() RETURNS trigger AS $$ BEGIN @@ -71,7 +70,6 @@ END; $$ LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION notify_alert() RETURNS trigger AS $$ DECLARE @@ -96,7 +94,7 @@ BEGIN END; $$ LANGUAGE plpgsql; - +-- All tables and types: DO $$ @@ -539,11 +537,11 @@ $$ watchdogs_score bigint NOT NULL DEFAULT 0, issue_score bigint NOT NULL DEFAULT 0, issue_types issue_type[] NOT NULL DEFAULT '{}'::issue_type[], - utm_source text DEFAULT NULL, - utm_medium text DEFAULT NULL, - utm_campaign text DEFAULT NULL, - referrer text DEFAULT NULL, - base_referrer text DEFAULT NULL, + utm_source text NULL DEFAULT NULL, + utm_medium text NULL DEFAULT NULL, + utm_campaign text NULL DEFAULT NULL, + referrer text NULL DEFAULT NULL, + base_referrer text NULL DEFAULT NULL, file_key bytea DEFAULT NULL, metadata_1 text DEFAULT NULL, metadata_2 text DEFAULT NULL, @@ -804,6 +802,7 @@ $$ config jsonb NOT NULL DEFAULT '{}'::jsonb ); + CREATE TABLE IF NOT EXISTS searches ( search_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, @@ -902,9 +901,9 @@ $$ ( feature_flag_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, - name text NOT NULL, flag_key text NOT NULL, - description text NOT NULL, + description text DEFAULT NULL, + payload text DEFAULT NULL, flag_type text NOT NULL, is_persist boolean NOT NULL DEFAULT FALSE, is_active boolean NOT NULL DEFAULT FALSE, @@ -917,6 +916,9 @@ $$ CREATE INDEX IF NOT EXISTS idx_feature_flags_project_id ON public.feature_flags (project_id); + ALTER TABLE feature_flags + ADD CONSTRAINT unique_project_flag_deleted UNIQUE (project_id, flag_key, deleted_at); + CREATE TABLE IF NOT EXISTS public.feature_flags_conditions ( condition_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, @@ -926,6 +928,16 @@ $$ filters jsonb NOT NULL DEFAULT '[]'::jsonb ); + CREATE TABLE IF NOT EXISTS public.feature_flags_variants + ( + variant_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, + feature_flag_id integer NOT NULL REFERENCES feature_flags (feature_flag_id) ON DELETE CASCADE, + value text NOT NULL, + description text DEFAULT NULL, + payload jsonb DEFAULT NULL, + rollout_percentage integer DEFAULT 0 + ); + CREATE TABLE IF NOT EXISTS public.sessions_feature_flags ( session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, diff --git a/scripts/schema/db/init_dbs/postgresql/init_schema.sql b/scripts/schema/db/init_dbs/postgresql/init_schema.sql index e37a6e4b7..152603612 100644 --- a/scripts/schema/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/schema/db/init_dbs/postgresql/init_schema.sql @@ -2,6 +2,8 @@ BEGIN; -- Schemas and functions definitions: CREATE SCHEMA IF NOT EXISTS events_common; CREATE SCHEMA IF NOT EXISTS events; +CREATE EXTENSION IF NOT EXISTS pg_trgm; +CREATE EXTENSION IF NOT EXISTS pgcrypto; CREATE OR REPLACE FUNCTION openreplay_version() RETURNS text AS @@ -106,9 +108,6 @@ $$ raise notice 'Creating DB'; - CREATE EXTENSION IF NOT EXISTS pg_trgm; - CREATE EXTENSION IF NOT EXISTS pgcrypto; - CREATE TABLE tenants ( tenant_id integer NOT NULL DEFAULT 1, @@ -983,8 +982,8 @@ $$ feature_flag_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, flag_key text NOT NULL, - description text DEFAULT NULL::text, - payload text DEFAULT NULL::text, + description text DEFAULT NULL, + payload text DEFAULT NULL, flag_type text NOT NULL, is_persist boolean NOT NULL DEFAULT FALSE, is_active boolean NOT NULL DEFAULT FALSE, @@ -1014,7 +1013,7 @@ $$ variant_id integer generated BY DEFAULT AS IDENTITY PRIMARY KEY, feature_flag_id integer NOT NULL REFERENCES feature_flags (feature_flag_id) ON DELETE CASCADE, value text NOT NULL, - description text DEFAULT NULL::text, + description text DEFAULT NULL, payload jsonb DEFAULT NULL, rollout_percentage integer DEFAULT 0 );