Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions business_objects/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ def delete_by_record_ids(
def delete_by_record_ids_and_sub_keys(
project_id: str,
embedding_id: str,
to_del: Iterable[Tuple[str, str]],
to_del: Iterable[Tuple[str, Any]],
with_commit: bool = False,
) -> None:
# deletes entries based on record_id and sub_key tuples for record changes
Expand All @@ -843,7 +843,11 @@ def delete_by_record_ids_and_sub_keys(
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
embedding_id = prevent_sql_injection(embedding_id, isinstance(embedding_id, str))
query_adds = [
(prevent_sql_injection(r), prevent_sql_injection(s)) for r, s in to_del
(
prevent_sql_injection(r, isinstance(r, str)),
prevent_sql_injection(s, isinstance(s, int)),
)
for r, s in to_del
]

query_adds = [
Expand Down
7 changes: 7 additions & 0 deletions cognition_objects/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def get_overview_for_all_for_me(
return final_list


def get_execution_group(execution_group_id: str) -> CognitionMacro:
query = session.query(CognitionMacroExecution).filter(
CognitionMacroExecution.execution_group_id == execution_group_id,
)
return query.first()


def get_all_macro_executions(
macro_id: str,
execution_group_id: str,
Expand Down
4 changes: 4 additions & 0 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,10 @@ class AdminQueries(Enum):
"AVG_MESSAGES_PER_CONVERSATION_GLOBAL" # parameter options: organization_id
)
AVG_MESSAGES_PER_CONVERSATION = "AVG_MESSAGES_PER_CONVERSATION" # parameter options: period (days, weeks or months), slices, organization_id
MACRO_EXECUTIONS = "MACRO_EXECUTIONS" # parameter options: period (days, weeks or months), slices, organization_id
FOLDER_MACRO_EXECUTION_SUMMARY = (
"FOLDER_MACRO_EXECUTION_SUMMARY" # parameter options: organization_id
)


class CognitionIntegrationType(Enum):
Expand Down
157 changes: 156 additions & 1 deletion global_objects/admin_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,164 @@ def get_result_admin_query(
return __get_global_messages_per_conversation(**parameters, as_query=as_query)
elif query == enums.AdminQueries.AVG_MESSAGES_PER_CONVERSATION:
return __get_avg_messages_per_conversation(**parameters, as_query=as_query)
elif query == enums.AdminQueries.MACRO_EXECUTIONS:
return __get_macro_executions(**parameters, as_query=as_query)
elif query == enums.AdminQueries.FOLDER_MACRO_EXECUTION_SUMMARY:
return __get_folder_macro_execution_summary(**parameters, as_query=as_query)
return []


def __get_folder_macro_execution_summary(
slices: int = 7, # how many chunks are relevant
organization_id: Optional[str] = None,
as_query: bool = False,
) -> List[Row]:

slices = max(min(slices, 30), 1)

org_where = ""
if organization_id:
organization_id = prevent_sql_injection(
organization_id, isinstance(organization_id, str)
)
org_where = f""" WHERE me.organization_id = '{organization_id}'"""

query = f"""
WITH params AS (
SELECT
'months' ::text AS period, -- sum table so fixed to months
{slices} ::int AS n -- ← how many of those periods you want
),

-- 1) build the list of period-start dates
periods AS (
SELECT
(generate_series(
date_trunc(p.period, CURRENT_DATE)
- (p.n - 1) * ( '1 ' || p.period )::interval,
date_trunc(p.period, CURRENT_DATE),
( '1 ' || p.period )::interval
))::date AS period_start,
p.period
FROM params p
),
filtered AS (
SELECT
me.organization_id,
date_trunc(p.period, me.creation_month)::date AS period_start,
execution_count,
processed_files_count
FROM cognition.macro_execution_summary me
INNER JOIN params p
ON me.creation_month >= (
SELECT MIN(period_start)
FROM periods
)
AND me.creation_month < (
SELECT MAX(period_start) + ( '1 ' || p.period )::interval
FROM periods, params
)
{org_where}
)


SELECT
o.name organization_name,
period_start,
(period_start + ( '1 ' || pa.period )::INTERVAL - '1 day'::interval )::date AS period_end,
execution_count,
processed_files_count
FROM filtered m
INNER JOIN organization o
ON m.organization_id = o.id
, params pa
ORDER BY 1,2 DESC
"""
if as_query:
return query
return general.execute_all(query)


def __get_macro_executions(
period: str = "days", # options: days, weeks, months
slices: int = 7, # how many chunks are relevant
organization_id: Optional[str] = None,
as_query: bool = False,
) -> List[Row]:

if period not in PERIOD_OPTIONS:
raise ValueError(f"Invalid period: {period}. Must be one of {PERIOD_OPTIONS}.")
slices = max(min(slices, 30), 1)

org_where = ""
if organization_id:
organization_id = prevent_sql_injection(
organization_id, isinstance(organization_id, str)
)
org_where = f""" WHERE me.organization_id = '{organization_id}'"""

query = f"""
WITH params AS (
SELECT
'{period}' ::text AS period, -- ← 'days' | 'weeks' | 'months'
{slices} ::int AS n -- ← how many of those periods you want
),

-- 1) build the list of period-start dates
periods AS (
SELECT
(generate_series(
date_trunc(p.period, CURRENT_DATE)
- (p.n - 1) * ( '1 ' || p.period )::interval,
date_trunc(p.period, CURRENT_DATE),
( '1 ' || p.period )::interval
))::date AS period_start,
p.period
FROM params p
),
filtered AS (
SELECT
me.organization_id,
date_trunc(p.period, me.created_at)::date AS period_start
FROM cognition.macro_execution me
INNER JOIN params p
ON me.created_at >= (
SELECT MIN(period_start)
FROM periods
)
AND me.created_at < (
SELECT MAX(period_start) + ( '1 ' || p.period )::interval
FROM periods, params
)
{org_where}
)


SELECT
o.name organization_name,
period_start,
(period_start + ( '1 ' || pa.period )::INTERVAL - '1 day'::interval )::date AS period_end,
macro_executions
FROM (
SELECT
organization_id,
period_start,
COUNT(*) macro_executions
FROM filtered M
GROUP BY
m.organization_id,
period_start
)y
INNER JOIN organization o
ON y.organization_id = o.id
, params pa
ORDER BY 1,2 DESC
"""
if as_query:
return query
return general.execute_all(query)


def __get_avg_messages_per_conversation(
period: str = "days", # options: days, weeks, months
slices: int = 7, # how many chunks are relevant
Expand Down Expand Up @@ -131,7 +286,7 @@ def __get_avg_messages_per_conversation(
INNER JOIN organization o
ON p.organization_id = o.id
, params pa
ORDER BY 1,5 DESC
ORDER BY 1,3 DESC
"""
if as_query:
return query
Expand Down