1+ drop function if exists jsonb_sum_aggregate(rows jsonb[]);
12create or replace function jsonb_sum_aggregate (rows jsonb[])
23returns jsonb language plpgsql immutable as $$
34declare
@@ -22,12 +23,16 @@ begin
2223end;
2324$$;
2425
26+ drop function if exists user_analytics_aggregate_period(user_id integer ,
27+ p_start timestamptz ,
28+ p_interval interval);
29+
2530create or replace function user_analytics_aggregate_period (
31+ user_id integer ,
2632 p_start timestamptz ,
2733 p_interval interval
2834)
2935returns table (
30- user_id int ,
3136 window_start timestamptz ,
3237 window_end timestamptz ,
3338 lang_durations jsonb,
@@ -36,28 +41,61 @@ returns table (
3641 project_durations jsonb,
3742 activity_durations jsonb
3843) language sql as $$
44+ with combined as (
45+ -- aggregate_daily rows
46+ select
47+ d .window_start ,
48+ d .window_end ,
49+ d .lang_durations ,
50+ d .machine_durations ,
51+ d .editor_durations ,
52+ d .project_durations ,
53+ d .activity_durations
54+ from user_stats_aggregate_daily d
55+ where d .user_id = user_id
56+ and d .window_start >= p_start
57+ and d .window_start < p_start + p_interval
58+
59+ union all
60+
61+ -- rolling_day row
62+ select
63+ now() as window_start, -- placeholder timestamp
64+ now() as window_end, -- placeholder timestamp
65+ r .lang_durations ,
66+ r .machine_durations ,
67+ r .editor_durations ,
68+ r .project_durations ,
69+ r .activity_durations
70+ from user_stats_rolling_day r
71+ where r .user_id = user_id
72+ and p_start + p_interval > now() - interval ' 24 hours'
73+ )
3974 select
40- d .user_id ,
41- min (d .window_start ) as window_start,
42- max (d .window_end ) as window_end,
43- jsonb_sum_aggregate(array_agg(d .lang_durations )) as lang_durations,
44- jsonb_sum_aggregate(array_agg(d .machine_durations )) as machine_durations,
45- jsonb_sum_aggregate(array_agg(d .editor_durations )) as editor_durations,
46- jsonb_sum_aggregate(array_agg(d .project_durations )) as project_durations,
47- jsonb_sum_aggregate(array_agg(d .activity_durations )) as activity_durations
48- from user_stats_aggregate_daily d
49- where d .window_start >= p_start
50- and d .window_start < p_start + p_interval
51- group by d .user_id ;
75+ min (window_start) as window_start,
76+ max (window_end) as window_end,
77+ jsonb_sum_aggregate(array_agg(lang_durations)) as lang_durations,
78+ jsonb_sum_aggregate(array_agg(machine_durations)) as machine_durations,
79+ jsonb_sum_aggregate(array_agg(editor_durations)) as editor_durations,
80+ jsonb_sum_aggregate(array_agg(project_durations)) as project_durations,
81+ jsonb_sum_aggregate(array_agg(activity_durations)) as activity_durations
82+ from combined
5283$$;
5384
85+ drop function if exists user_project_analytics_aggregate_period(
86+ user_id integer ,
87+ project_path varchar ,
88+ p_start timestamptz ,
89+ p_interval interval
90+ );
91+
5492create or replace function user_project_analytics_aggregate_period (
93+ user_id integer ,
94+ project_path varchar ,
5595 p_start timestamptz ,
5696 p_interval interval
5797)
5898returns table (
59- user_id int ,
60- project_path varchar ,
6199 window_start timestamptz ,
62100 window_end timestamptz ,
63101 lang_durations jsonb,
@@ -66,18 +104,45 @@ returns table (
66104 activity_durations jsonb,
67105 files_durations jsonb
68106) language sql as $$
107+ with combined as (
108+ -- aggregate_daily rows
109+ select
110+ d .window_start ,
111+ d .window_end ,
112+ d .lang_durations ,
113+ d .machine_durations ,
114+ d .editor_durations ,
115+ d .activity_durations ,
116+ d .files_durations
117+ from user_project_stats_aggregate_daily d
118+ where d .user_id = user_id
119+ and d .project_path = project_path
120+ and d .window_start >= p_start
121+ and d .window_start < p_start + p_interval
122+
123+ union all
124+
125+ -- rolling_day row
126+ select
127+ now() as window_start, -- placeholder timestamp
128+ now() as window_end, -- placeholder timestamp
129+ r .lang_durations ,
130+ r .machine_durations ,
131+ r .editor_durations ,
132+ r .activity_durations ,
133+ r .files_durations
134+ from user_project_stats_rolling_day r
135+ where r .user_id = user_id
136+ and r .project_path = project_path
137+ and p_start + p_interval > now() - interval ' 24 hours'
138+ )
69139 select
70- d .user_id ,
71- d .project_path ,
72- min (d .window_start ) as window_start,
73- max (d .window_end ) as window_end,
74- jsonb_sum_aggregate(array_agg(d .lang_durations )) as lang_durations,
75- jsonb_sum_aggregate(array_agg(d .machine_durations )) as machine_durations,
76- jsonb_sum_aggregate(array_agg(d .editor_durations )) as editor_durations,
77- jsonb_sum_aggregate(array_agg(d .activity_durations )) as activity_durations,
78- jsonb_sum_aggregate(array_agg(d .files_durations )) as files_durations
79- from user_project_stats_aggregate_daily d
80- where d .window_start >= p_start
81- and d .window_start < p_start + p_interval
82- group by d .user_id , d .project_path ;
140+ min (window_start) as window_start,
141+ max (window_end) as window_end,
142+ jsonb_sum_aggregate(array_agg(lang_durations)) as lang_durations,
143+ jsonb_sum_aggregate(array_agg(machine_durations)) as machine_durations,
144+ jsonb_sum_aggregate(array_agg(editor_durations)) as editor_durations,
145+ jsonb_sum_aggregate(array_agg(activity_durations)) as activity_durations,
146+ jsonb_sum_aggregate(array_agg(files_durations)) as files_durations
147+ from combined
83148$$;
0 commit comments