diff --git a/values/umami/queries/README.md b/values/umami/queries/README.md new file mode 100644 index 00000000..7da0423c --- /dev/null +++ b/values/umami/queries/README.md @@ -0,0 +1 @@ +Various scripts to setup certain views in Grafana for dashboards. diff --git a/values/umami/queries/create-sim-count-view.sql b/values/umami/queries/create-sim-count-view.sql new file mode 100644 index 00000000..cf0db374 --- /dev/null +++ b/values/umami/queries/create-sim-count-view.sql @@ -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; diff --git a/values/umami/queries/create_sim_crosstab.sql b/values/umami/queries/create_sim_crosstab.sql new file mode 100644 index 00000000..97cc70a3 --- /dev/null +++ b/values/umami/queries/create_sim_crosstab.sql @@ -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; + + diff --git a/values/umami/queries/grafana-weekly-users.sql b/values/umami/queries/grafana-weekly-users.sql new file mode 100644 index 00000000..52616144 --- /dev/null +++ b/values/umami/queries/grafana-weekly-users.sql @@ -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; diff --git a/values/umami/queries/sim_count.sql b/values/umami/queries/sim_count.sql new file mode 100644 index 00000000..9fdd6870 --- /dev/null +++ b/values/umami/queries/sim_count.sql @@ -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' + ) diff --git a/values/umami/queries/umami-groups.sql b/values/umami/queries/umami-groups.sql new file mode 100644 index 00000000..7f5a545e --- /dev/null +++ b/values/umami/queries/umami-groups.sql @@ -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 '#') + ; + diff --git a/values/umami/queries/umami-visitors.sql b/values/umami/queries/umami-visitors.sql new file mode 100644 index 00000000..5441b1b3 --- /dev/null +++ b/values/umami/queries/umami-visitors.sql @@ -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 + ; diff --git a/values/umami/queries/umami.sql b/values/umami/queries/umami.sql new file mode 100644 index 00000000..6bb035cc --- /dev/null +++ b/values/umami/queries/umami.sql @@ -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 + ; diff --git a/values/umami/queries/users.sql b/values/umami/queries/users.sql new file mode 100644 index 00000000..0f08880a --- /dev/null +++ b/values/umami/queries/users.sql @@ -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') + ; diff --git a/values/umami/queries/weekly-sim-submit-count.sql b/values/umami/queries/weekly-sim-submit-count.sql new file mode 100644 index 00000000..a883d8bd --- /dev/null +++ b/values/umami/queries/weekly-sim-submit-count.sql @@ -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; + diff --git a/values/umami/queries/weekly-users.sql b/values/umami/queries/weekly-users.sql new file mode 100644 index 00000000..c5c8c99a --- /dev/null +++ b/values/umami/queries/weekly-users.sql @@ -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') + ; diff --git a/values/umami/queries/weekly.sql b/values/umami/queries/weekly.sql new file mode 100644 index 00000000..371eb348 --- /dev/null +++ b/values/umami/queries/weekly.sql @@ -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 + ;