@@ -52,6 +52,8 @@ def get_result_admin_query(
5252 return __get_template_usage (** parameters , as_query = as_query )
5353 elif query == enums .AdminQueries .PRIVATEMODE_USE_OVER_TIME :
5454 return __get_privatemode_use_over_time (** parameters , as_query = as_query )
55+ elif query == enums .AdminQueries .INCOGNITO_USE_OVER_TIME :
56+ return __get_incognito_mode_use_over_time (** parameters , as_query = as_query )
5557 return []
5658
5759
@@ -82,6 +84,80 @@ def __get_template_usage(organization_id: str = "", as_query: bool = False):
8284 return general .execute_all (query )
8385
8486
87+ def __get_incognito_mode_use_over_time (
88+ period : str = "days" ,
89+ slices : int = 7 ,
90+ organization_id : str = "" ,
91+ as_query : bool = False ,
92+ ):
93+
94+ if period not in PERIOD_OPTIONS :
95+ raise ValueError (f"Invalid period: { period } . Must be one of { PERIOD_OPTIONS } ." )
96+
97+ slices = max (min (slices , 30 ), 1 )
98+
99+ org_where = ""
100+ if organization_id :
101+ organization_id = prevent_sql_injection (
102+ organization_id , isinstance (organization_id , str )
103+ )
104+ org_where = f"AND summary.organization_id = '{ organization_id } '"
105+
106+ query = f"""
107+ WITH params AS (
108+ SELECT
109+ '{ period } ' ::text AS period, -- ← 'days' | 'weeks' | 'months'
110+ { slices } ::int AS n -- ← how many of those periods you want
111+ ),
112+ periods AS (
113+ SELECT
114+ (generate_series(
115+ date_trunc(p.period, CURRENT_DATE)
116+ - (p.n - 1) * ('1 ' || p.period)::interval,
117+ date_trunc(p.period, CURRENT_DATE),
118+ ('1 ' || p.period)::interval
119+ ))::date AS period_start
120+ FROM params p
121+ ),
122+ filtered AS (
123+ SELECT
124+ summary.organization_id,
125+ summary.day,
126+ summary.incognito_messages
127+ FROM cognition.admin_query_message_summary summary
128+ WHERE summary.day >= (SELECT MIN(period_start) FROM periods)
129+ AND summary.day < (
130+ SELECT MAX(period_start)
131+ + ('1 ' || (SELECT period FROM params))::interval
132+ FROM periods
133+ )
134+ { org_where }
135+ ),
136+ aggregated AS (
137+ SELECT
138+ f.organization_id,
139+ date_trunc((SELECT period FROM params), f.day)::date AS period_start,
140+ SUM(f.incognito_messages) AS incognito_messages
141+ FROM filtered f
142+ GROUP BY 1,2
143+ )
144+ SELECT
145+ o.name organization_name,
146+ a.period_start,
147+ (a.period_start + ('1 ' || (SELECT period FROM params))::interval - INTERVAL '1 day')::date AS period_end,
148+ a.incognito_messages
149+ FROM aggregated a
150+ INNER JOIN organization o
151+ ON o.id = a.organization_id
152+ ORDER BY o.name, a.period_start DESC
153+ """
154+
155+ if as_query :
156+ return query
157+
158+ return general .execute_all (query )
159+
160+
85161def __get_privatemode_use_over_time (
86162 organization_id : str = "" , without_kern_email : bool = False , as_query : bool = False
87163):
0 commit comments