@@ -35,9 +35,164 @@ def get_result_admin_query(
3535 return __get_global_messages_per_conversation (** parameters , as_query = as_query )
3636 elif query == enums .AdminQueries .AVG_MESSAGES_PER_CONVERSATION :
3737 return __get_avg_messages_per_conversation (** parameters , as_query = as_query )
38+ elif query == enums .AdminQueries .MACRO_EXECUTIONS :
39+ return __get_macro_executions (** parameters , as_query = as_query )
40+ elif query == enums .AdminQueries .FOLDER_MACRO_EXECUTION_SUMMARY :
41+ return __get_folder_macro_execution_summary (** parameters , as_query = as_query )
3842 return []
3943
4044
45+ def __get_folder_macro_execution_summary (
46+ slices : int = 7 , # how many chunks are relevant
47+ organization_id : Optional [str ] = None ,
48+ as_query : bool = False ,
49+ ) -> List [Row ]:
50+
51+ slices = max (min (slices , 30 ), 1 )
52+
53+ org_where = ""
54+ if organization_id :
55+ organization_id = prevent_sql_injection (
56+ organization_id , isinstance (organization_id , str )
57+ )
58+ org_where = f""" WHERE me.organization_id = '{ organization_id } '"""
59+
60+ query = f"""
61+ WITH params AS (
62+ SELECT
63+ 'months' ::text AS period, -- sum table so fixed to months
64+ { slices } ::int AS n -- ← how many of those periods you want
65+ ),
66+
67+ -- 1) build the list of period-start dates
68+ periods AS (
69+ SELECT
70+ (generate_series(
71+ date_trunc(p.period, CURRENT_DATE)
72+ - (p.n - 1) * ( '1 ' || p.period )::interval,
73+ date_trunc(p.period, CURRENT_DATE),
74+ ( '1 ' || p.period )::interval
75+ ))::date AS period_start,
76+ p.period
77+ FROM params p
78+ ),
79+ filtered AS (
80+ SELECT
81+ me.organization_id,
82+ date_trunc(p.period, me.creation_month)::date AS period_start,
83+ execution_count,
84+ processed_files_count
85+ FROM cognition.macro_execution_summary me
86+ INNER JOIN params p
87+ ON me.creation_month >= (
88+ SELECT MIN(period_start)
89+ FROM periods
90+ )
91+ AND me.creation_month < (
92+ SELECT MAX(period_start) + ( '1 ' || p.period )::interval
93+ FROM periods, params
94+ )
95+ { org_where }
96+ )
97+
98+
99+ SELECT
100+ o.name organization_name,
101+ period_start,
102+ (period_start + ( '1 ' || pa.period )::INTERVAL - '1 day'::interval )::date AS period_end,
103+ execution_count,
104+ processed_files_count
105+ FROM filtered m
106+ INNER JOIN organization o
107+ ON m.organization_id = o.id
108+ , params pa
109+ ORDER BY 1,2 DESC
110+ """
111+ if as_query :
112+ return query
113+ return general .execute_all (query )
114+
115+
116+ def __get_macro_executions (
117+ period : str = "days" , # options: days, weeks, months
118+ slices : int = 7 , # how many chunks are relevant
119+ organization_id : Optional [str ] = None ,
120+ as_query : bool = False ,
121+ ) -> List [Row ]:
122+
123+ if period not in PERIOD_OPTIONS :
124+ raise ValueError (f"Invalid period: { period } . Must be one of { PERIOD_OPTIONS } ." )
125+ slices = max (min (slices , 30 ), 1 )
126+
127+ org_where = ""
128+ if organization_id :
129+ organization_id = prevent_sql_injection (
130+ organization_id , isinstance (organization_id , str )
131+ )
132+ org_where = f""" WHERE me.organization_id = '{ organization_id } '"""
133+
134+ query = f"""
135+ WITH params AS (
136+ SELECT
137+ '{ period } ' ::text AS period, -- ← 'days' | 'weeks' | 'months'
138+ { slices } ::int AS n -- ← how many of those periods you want
139+ ),
140+
141+ -- 1) build the list of period-start dates
142+ periods AS (
143+ SELECT
144+ (generate_series(
145+ date_trunc(p.period, CURRENT_DATE)
146+ - (p.n - 1) * ( '1 ' || p.period )::interval,
147+ date_trunc(p.period, CURRENT_DATE),
148+ ( '1 ' || p.period )::interval
149+ ))::date AS period_start,
150+ p.period
151+ FROM params p
152+ ),
153+ filtered AS (
154+ SELECT
155+ me.organization_id,
156+ date_trunc(p.period, me.created_at)::date AS period_start
157+ FROM cognition.macro_execution me
158+ INNER JOIN params p
159+ ON me.created_at >= (
160+ SELECT MIN(period_start)
161+ FROM periods
162+ )
163+ AND me.created_at < (
164+ SELECT MAX(period_start) + ( '1 ' || p.period )::interval
165+ FROM periods, params
166+ )
167+ { org_where }
168+ )
169+
170+
171+ SELECT
172+ o.name organization_name,
173+ period_start,
174+ (period_start + ( '1 ' || pa.period )::INTERVAL - '1 day'::interval )::date AS period_end,
175+ macro_executions
176+ FROM (
177+ SELECT
178+ organization_id,
179+ period_start,
180+ COUNT(*) macro_executions
181+ FROM filtered M
182+ GROUP BY
183+ m.organization_id,
184+ period_start
185+ )y
186+ INNER JOIN organization o
187+ ON y.organization_id = o.id
188+ , params pa
189+ ORDER BY 1,2 DESC
190+ """
191+ if as_query :
192+ return query
193+ return general .execute_all (query )
194+
195+
41196def __get_avg_messages_per_conversation (
42197 period : str = "days" , # options: days, weeks, months
43198 slices : int = 7 , # how many chunks are relevant
@@ -131,7 +286,7 @@ def __get_avg_messages_per_conversation(
131286 INNER JOIN organization o
132287 ON p.organization_id = o.id
133288 , params pa
134- ORDER BY 1,5 DESC
289+ ORDER BY 1,3 DESC
135290"""
136291 if as_query :
137292 return query
0 commit comments