Skip to content

Commit 36bf230

Browse files
authored
Refactor some database functions (#540)
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
1 parent 5868999 commit 36bf230

13 files changed

Lines changed: 1194 additions & 844 deletions

database/migrations/functions/001_load_functions.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
{{ template "common/list_event_ticket_types.sql" }} -- Dependency for get_event_full and payments
2424
{{ template "common/get_group_summary.sql" }} -- Do not sort alphabetically, has dependency
2525
{{ template "common/questionnaire_answers_exist_for_event.sql" }} -- Do not sort alphabetically, dependency for get_event_full and update_event
26+
{{ template "common/stats_label_count_series.sql" }}
27+
{{ template "common/stats_label_count_series_by_name.sql" }}
28+
{{ template "common/stats_running_total_series.sql" }}
29+
{{ template "common/stats_running_total_series_by_name.sql" }}
2630
{{ template "common/validate_questionnaire_questions_payload.sql" }} -- Do not sort alphabetically, dependency for add/update_event and validate_questionnaire_answers_payload
2731
{{ template "common/validate_questionnaire_answers_payload.sql" }} -- Do not sort alphabetically, dependency for attend_event, submit_event_registration_answers and prepare_event_checkout_purchase
2832
{{ template "common/get_event_full.sql" }}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Builds an ordered label count series from labeled count rows.
2+
create or replace function stats_label_count_series(p_counts jsonb)
3+
returns json as $$
4+
select coalesce(
5+
json_agg(json_build_array(label, count) order by label),
6+
'[]'::json
7+
)
8+
from (
9+
select
10+
count_row.label,
11+
count_row.count
12+
from jsonb_to_recordset(coalesce(p_counts, '[]'::jsonb)) as count_row(
13+
label text,
14+
count int
15+
)
16+
) counts;
17+
$$ language sql immutable;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Builds ordered label count series grouped by name.
2+
create or replace function stats_label_count_series_by_name(p_counts jsonb)
3+
returns json as $$
4+
with named_counts as (
5+
select
6+
count_row.series_name,
7+
jsonb_agg(to_jsonb(count_row)) as counts
8+
from jsonb_to_recordset(coalesce(p_counts, '[]'::jsonb)) as count_row(
9+
series_name text,
10+
label text,
11+
count int
12+
)
13+
group by count_row.series_name
14+
)
15+
select coalesce(
16+
json_object_agg(
17+
series_name,
18+
stats_label_count_series(counts)
19+
order by series_name
20+
),
21+
'{}'::json
22+
)
23+
from named_counts;
24+
$$ language sql immutable;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Builds an ordered cumulative count series from timestamp bucket count rows.
2+
create or replace function stats_running_total_series(p_counts jsonb)
3+
returns json as $$
4+
select coalesce(
5+
json_agg(json_build_array(ts, cumulative_total) order by ts),
6+
'[]'::json
7+
)
8+
from (
9+
select
10+
floor(extract(epoch from bucket_start) * 1000)::bigint as ts,
11+
sum(count) over (order by bucket_start)::int as cumulative_total
12+
from jsonb_to_recordset(coalesce(p_counts, '[]'::jsonb)) as count_row(
13+
bucket_start timestamptz,
14+
count int
15+
)
16+
) totals;
17+
$$ language sql immutable;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Builds ordered cumulative count series grouped by name.
2+
create or replace function stats_running_total_series_by_name(p_counts jsonb)
3+
returns json as $$
4+
with named_counts as (
5+
select
6+
series_name,
7+
jsonb_agg(to_jsonb(count_row)) as counts
8+
from jsonb_to_recordset(coalesce(p_counts, '[]'::jsonb)) as count_row(
9+
series_name text,
10+
bucket_start timestamptz,
11+
count int
12+
)
13+
group by series_name
14+
)
15+
select coalesce(
16+
json_object_agg(
17+
series_name,
18+
stats_running_total_series(counts)
19+
order by series_name
20+
),
21+
'{}'::json
22+
)
23+
from named_counts;
24+
$$ language sql immutable;

0 commit comments

Comments
 (0)