Skip to content

Commit c5663e9

Browse files
v1.20.0 (#175)
* Record edit on embedding lists * New admin queries * get execution group --------- Co-authored-by: LennartSchmidtKern <lennart.schmidt@kern.ai>
1 parent 74e14eb commit c5663e9

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

business_objects/embedding.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ def delete_by_record_ids(
830830
def delete_by_record_ids_and_sub_keys(
831831
project_id: str,
832832
embedding_id: str,
833-
to_del: Iterable[Tuple[str, str]],
833+
to_del: Iterable[Tuple[str, Any]],
834834
with_commit: bool = False,
835835
) -> None:
836836
# deletes entries based on record_id and sub_key tuples for record changes
@@ -843,7 +843,11 @@ def delete_by_record_ids_and_sub_keys(
843843
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
844844
embedding_id = prevent_sql_injection(embedding_id, isinstance(embedding_id, str))
845845
query_adds = [
846-
(prevent_sql_injection(r), prevent_sql_injection(s)) for r, s in to_del
846+
(
847+
prevent_sql_injection(r, isinstance(r, str)),
848+
prevent_sql_injection(s, isinstance(s, int)),
849+
)
850+
for r, s in to_del
847851
]
848852

849853
query_adds = [

cognition_objects/macro.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ def get_overview_for_all_for_me(
7777
return final_list
7878

7979

80+
def get_execution_group(execution_group_id: str) -> CognitionMacro:
81+
query = session.query(CognitionMacroExecution).filter(
82+
CognitionMacroExecution.execution_group_id == execution_group_id,
83+
)
84+
return query.first()
85+
86+
8087
def get_all_macro_executions(
8188
macro_id: str,
8289
execution_group_id: str,

enums.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,10 @@ class AdminQueries(Enum):
908908
"AVG_MESSAGES_PER_CONVERSATION_GLOBAL" # parameter options: organization_id
909909
)
910910
AVG_MESSAGES_PER_CONVERSATION = "AVG_MESSAGES_PER_CONVERSATION" # parameter options: period (days, weeks or months), slices, organization_id
911+
MACRO_EXECUTIONS = "MACRO_EXECUTIONS" # parameter options: period (days, weeks or months), slices, organization_id
912+
FOLDER_MACRO_EXECUTION_SUMMARY = (
913+
"FOLDER_MACRO_EXECUTION_SUMMARY" # parameter options: organization_id
914+
)
911915

912916

913917
class CognitionIntegrationType(Enum):

global_objects/admin_queries.py

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
41196
def __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

Comments
 (0)