Persist umami sql queries and stuff

This commit is contained in:
2025-11-18 15:24:07 +01:00
parent 444f7a376e
commit 87dd246d09
12 changed files with 262 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Various scripts to setup certain views in Grafana for dashboards.
@@ -0,0 +1,20 @@
-- NOTE(simkir): Materialized view called by the crosstab to pivot sim types to cols
CREATE MATERIALIZED VIEW weekly_sim_submit_count AS
SELECT
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS group,
event_data.string_value as sim_type,
COUNT(*)
FROM
website_event
JOIN session ON session.session_id = website_event.session_id
LEFT OUTER JOIN event_data ON event_data.website_event_id = website_event.event_id
WHERE
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND website_event.event_type = 2
AND website_event.event_name = 'mapster-submit-drifters'
AND session.distinct_id IS NOT NULL
AND event_data.data_key = 'kind'
AND event_data.created_at BETWEEN CURRENT_TIMESTAMP - '7 days'::interval AND CURRENT_TIMESTAMP
GROUP BY
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#'),
event_data.string_value;
@@ -0,0 +1,16 @@
-- Type returned by the crosstab function
CREATE TYPE weekly_sim_submit_count_crosstab AS (
group_name text,
transport bigint,
lice bigint,
virus bigint,
watercontact bigint,
sedimentation bigint
);
-- Cross tab function we can call on the materialized view of weekly submit counts
CREATE OR REPLACE FUNCTION crosstab_weekly_sim_submit_count(text)
RETURNS setof weekly_sim_submit_count_crosstab
AS '$libdir/tablefunc','crosstab' LANGUAGE C STABLE STRICT;
@@ -0,0 +1,35 @@
WITH base AS (
SELECT
w.created_at,
s.distinct_id,
substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS "Group"
FROM website_event AS w
JOIN session AS s
ON s.session_id = w.session_id
WHERE
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND w.event_type = 1
AND $__timeFilter(w.created_at)
AND s.distinct_id IS NOT NULL
AND substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') IN ($groups)
),
unique_totals AS (
SELECT
"Group",
COUNT(DISTINCT distinct_id) AS "Users in range"
FROM base
GROUP BY "Group"
),
unique_users AS (
SELECT
"Group",
string_agg(DISTINCT distinct_id, ', ' ORDER BY distinct_id) AS "Users"
FROM base
GROUP BY "Group"
)
SELECT
'Total (non-oceanbox)' AS "Group",
SUM("Users in range") AS "Weekly users"
FROM unique_totals
WHERE LOWER("Group") <> '@oceanbox.io' AND LOWER("Group") <> '@gmail.com'
ORDER BY "Weekly users" DESC;
+41
View File
@@ -0,0 +1,41 @@
-- NOTE(simkir): Example of using temp table to call crosstab, since the
-- crosstab cannot find a table made with a `WITH` type query
DROP TABLE IF EXISTS simulations;
CREATE TEMP TABLE simulations (
group_name text,
sim_type text,
count integer
);
INSERT INTO
simulations (group_name, sim_type, count)
SELECT
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS group,
event_data.string_value as sim_type,
COUNT(event_data.string_value)
FROM
website_event
JOIN session ON session.session_id = website_event.session_id
LEFT OUTER JOIN event_data ON event_data.website_event_id = website_event.event_id
WHERE
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND website_event.event_type = 2
AND website_event.event_name = 'mapster-submit-drifters'
AND website_event.created_at BETWEEN '2025-11-10' AND '2025-11-18T23:59Z'
AND session.distinct_id IS NOT NULL
AND event_data.data_key = 'kind'
-- AND event_data.created_at BETWEEN '2025-11-10' AND '2025-11-18'
GROUP BY
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#'),
event_data.string_value;
SELECT
*
FROM
crosstab_integer_5_cols(
'SELECT * FROM simulations
WHERE
sim_type IN (''transport'', ''lice'', ''virus'', ''watercontact'', ''sedimentation'')
ORDER BY 1, 2'
)
+11
View File
@@ -0,0 +1,11 @@
select
substring(distinct_id similar '%#"@%#"' escape '#') as group
from
session
where
distinct_id is not null
and distinct_id like '%@%'
group by
substring(distinct_id similar '%#"@%#"' escape '#')
;
+19
View File
@@ -0,0 +1,19 @@
select
s.distinct_id,
count(distinct w.visit_id)
from
website_event as w
join
session as s
on s.session_id = w.session_id
where
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
and w.event_type = 1
and w.created_at between '2025-10-13' and '2025-10-19'
and s.distinct_id is not null
and substring(s.distinct_id similar '%#"@%#"' escape '#') not in ('@oceanbox.io')
group by
s.distinct_id
order by
count desc
;
+20
View File
@@ -0,0 +1,20 @@
select
substring(s.distinct_id similar '%#"@%#"' escape '#') as "Group",
count(distinct w.visit_id) as "Unique visits"
from
website_event as w
join
session as s
on s.session_id = w.session_id
where
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
and w.event_type = 1
and w.created_at between '2025-10-06' and '2025-10-10'
and s.distinct_id is not null
and s.distinct_id like '%@%'
group by
-- Aggregate by where the users go?
substring(s.distinct_id similar '%#"@%#"' escape '#')
order by
"Unique visits" desc
;
+23
View File
@@ -0,0 +1,23 @@
WITH users AS (
SELECT
w.created_at,
s.distinct_id,
substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS group
FROM
website_event AS w
JOIN
session AS s
ON s.session_id = w.session_id
WHERE
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND w.event_type = 1
AND w.created_at BETWEEN '2025-10-13' AND '2025-10-19'
AND s.distinct_id IS NOT NULL
)
SELECT
COUNT(DISTINCT users.distinct_id)
FROM
users
WHERE
users.group NOT IN ('@oceanbox.io')
;
@@ -0,0 +1,19 @@
SELECT
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS group,
event_data.string_value as sim_type,
COUNT(*)
FROM
website_event
JOIN session ON session.session_id = website_event.session_id
LEFT OUTER JOIN event_data ON event_data.website_event_id = website_event.event_id
WHERE
website_event.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND website_event.event_type = 2
AND website_event.event_name = 'mapster-submit-drifters'
AND session.distinct_id IS NOT NULL
AND event_data.data_key = 'kind'
AND event_data.created_at BETWEEN CURRENT_TIMESTAMP - '7 days'::interval AND CURRENT_TIMESTAMP
GROUP BY
substring(session.distinct_id SIMILAR '%#"@%#"' ESCAPE '#'),
event_data.string_value;
+36
View File
@@ -0,0 +1,36 @@
WITH base AS (
SELECT
w.created_at,
s.distinct_id,
substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') AS "Group"
FROM website_event AS w
JOIN session AS s
ON s.session_id = w.session_id
WHERE
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND w.event_type = 1
AND w.created_at BETWEEN '2025-10-13' AND '2025-10-19'
AND s.distinct_id IS NOT NULL
-- AND substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') IN ('@leroyseafood.com')
),
unique_totals AS (
SELECT
"Group",
COUNT(DISTINCT distinct_id) AS "Users in range"
FROM base
GROUP BY "Group"
),
unique_users AS (
SELECT
"Group",
string_agg(DISTINCT distinct_id, ', ' ORDER BY distinct_id) AS "Users"
FROM base
GROUP BY "Group"
)
SELECT
SUM("Users in range")
FROM
unique_totals
WHERE
LOWER("Group") NOT IN ('@oceanbox.io')
;
+21
View File
@@ -0,0 +1,21 @@
SELECT
CONCAT(
EXTRACT(YEAR FROM w.created_at),
'-',
EXTRACT(WEEK FROM w.created_at)
) as week,
COUNT(DISTINCT s.distinct_id)
FROM
website_event AS w
JOIN
session as s ON s.session_id = w.session_id
WHERE
w.website_id = '16e7d807-4db5-45fd-92a9-27393445a153'
AND w.event_type = 1
AND s.distinct_id IS NOT NULL
AND substring(s.distinct_id SIMILAR '%#"@%#"' ESCAPE '#') <> '@oceanbox.io'
GROUP BY
week
ORDER BY
week
;