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
This commit is contained in:
Kraiem Taha Yassine 2023-07-11 15:37:25 +01:00 committed by GitHub
parent e3f8db7c82
commit 329fcfb9d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 38 deletions

View file

@ -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"]
}

View file

@ -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"]
}

View file

@ -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,

View file

@ -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,

View file

@ -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
);