diff --git a/ee/api/chalicelib/utils/events_queue.py b/ee/api/chalicelib/utils/events_queue.py index d95938857..b715b072e 100644 --- a/ee/api/chalicelib/utils/events_queue.py +++ b/ee/api/chalicelib/utils/events_queue.py @@ -28,9 +28,13 @@ class EventQueue(): for _key, _val in element.dict().items(): if _key == 'data': params[f'{_key}_{i}'] = json.dumps(_val) + if 'sessionId' in _val.keys(): + params[f'session_id_{i}'] = int(_val['sessionId']) + else: + params[f'session_id_{i}'] = None else: params[f'{_key}_{i}'] = _val - events.append(f"(%(project_id_{i})s, %(user_id_{i})s, %(timestamp_{i})s, %(action_{i})s, %(source_{i})s, %(category_{i})s, %(data_{i})s::jsonb)") + events.append(f"(%(project_id_{i})s, %(user_id_{i})s, %(timestamp_{i})s, %(action_{i})s, %(source_{i})s, %(category_{i})s, %(data_{i})s::jsonb, %(session_id_{i})s)") i += 1 if i == 0: return 0 @@ -38,7 +42,7 @@ class EventQueue(): print(events) return 1 conn.execute( - conn.mogrify(f"""INSERT INTO public.frontend_signals (project_id, user_id, timestamp, action, source, category, data) + conn.mogrify(f"""INSERT INTO public.frontend_signals (project_id, user_id, timestamp, action, source, category, data, session_id) VALUES {' , '.join(events)}""", params) ) return 1 diff --git a/ee/scripts/schema/db/init_dbs/postgresql/1.13.0/1.13.0.sql b/ee/scripts/schema/db/init_dbs/postgresql/1.13.0/1.13.0.sql index ce79c0f89..ecc512d67 100644 --- a/ee/scripts/schema/db/init_dbs/postgresql/1.13.0/1.13.0.sql +++ b/ee/scripts/schema/db/init_dbs/postgresql/1.13.0/1.13.0.sql @@ -54,6 +54,9 @@ UPDATE public.roles SET permissions = (SELECT array_agg(distinct e) FROM unnest(permissions || '{FEATURE_FLAGS}') AS e) where not permissions @> '{FEATURE_FLAGS}'; +ALTER TABLE IF EXISTS public.frontend_signals + ADD COLUMN IF NOT EXISTS session_id integer NULL REFERENCES sessions (session_id) ON DELETE SET NULL; + ALTER TABLE IF EXISTS public.sessions ADD COLUMN IF NOT EXISTS user_city text, ADD COLUMN IF NOT EXISTS user_state text; @@ -61,4 +64,4 @@ ALTER TABLE IF EXISTS public.sessions COMMIT; CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_project_id_user_city_idx ON sessions (project_id, user_city); -CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_project_id_user_state_idx ON sessions (project_id, user_state); +CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_project_id_user_state_idx ON sessions (project_id, user_state); \ No newline at end of file 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 4c1c55517..c409a1c76 100644 --- a/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/schema/db/init_dbs/postgresql/init_schema.sql @@ -132,7 +132,8 @@ $$ ('webhooks'), ('sessions_notes'), ('assist_records'), - ('projects_stats')) + ('projects_stats'), + ('frontend_signals')) select bool_and(exists(select * from information_schema.tables t where table_schema = 'public' @@ -637,14 +638,15 @@ $$ CREATE TABLE IF NOT EXISTS frontend_signals ( - project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, - user_id integer NOT NULL REFERENCES users (user_id) ON DELETE CASCADE, - timestamp bigint NOT NULL, - action text NOT NULL, - source text NOT NULL, - category text NOT NULL, - data jsonb, - created_at timestamp DEFAULT timezone('utc'::text, now()) NOT NULL + project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, + user_id integer NOT NULL REFERENCES users (user_id) ON DELETE CASCADE, + timestamp bigint NOT NULL, + action text NOT NULL, + source text NOT NULL, + category text NOT NULL, + data jsonb NULL, + session_id integer NULL REFERENCES sessions (session_id) ON DELETE SET NULL, + created_at timestamp NOT NULL DEFAULT timezone('utc'::text, now()) ); CREATE INDEX IF NOT EXISTS frontend_signals_user_id_idx ON frontend_signals (user_id); @@ -1287,4 +1289,4 @@ $$ $$ LANGUAGE plpgsql; -COMMIT; \ No newline at end of file +COMMIT;