Skip to content

Commit 1198fda

Browse files
authored
Adds new admin query (#178)
* Adds new admin query * Adds new filter option * Remove print * PR comments
1 parent e9ba0bf commit 1198fda

File tree

2 files changed

+136
-23
lines changed

2 files changed

+136
-23
lines changed

enums.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -898,17 +898,20 @@ class EvaluationRunState(Enum):
898898

899899
class AdminQueries(Enum):
900900
# default values for parameters can be found in file admin_queries.py
901-
USERS_TO_PROJECTS = "USERS_TO_PROJECTS" # parameter options: organization_id
902-
ACTIVE_USERS_GLOBAL = "ACTIVE_USERS_GLOBAL" # parameter options: min_msg_count, period (days, weeks or months), slices, organization_id
903-
ACTIVE_USERS_BY_ORG = "ACTIVE_USERS_BY_ORG" # parameter options: min_msg_count, period (days, weeks or months), slices, organization_id
904-
MESSAGES_CREATED = "MESSAGES_CREATED" # parameter options: period (days, weeks or months), slices, organization_id
905-
MESSAGES_CREATED_BY_PROJECT = "MESSAGES_CREATED_BY_PROJECT" # parameter options: period (days, weeks or months), slices, organization_id
906-
MESSAGES_FEEDBACK_PER_PROJECT = "MESSAGES_FEEDBACK_PER_PROJECT" # parameter options: period (days, weeks or months), slices, organization_id
907-
AVG_MESSAGES_PER_CONVERSATION_GLOBAL = (
908-
"AVG_MESSAGES_PER_CONVERSATION_GLOBAL" # parameter options: organization_id
901+
USERS_TO_PROJECTS = (
902+
"USERS_TO_PROJECTS" # parameter options: organization_id, without_kern_email
909903
)
910-
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
904+
USERS_BY_ORG = (
905+
"USERS_BY_ORG" # parameter options: organization_id, without_kern_email
906+
)
907+
ACTIVE_USERS_GLOBAL = "ACTIVE_USERS_GLOBAL" # parameter options: min_msg_count, period (days, weeks or months), slices, organization_id, without_kern_email
908+
ACTIVE_USERS_BY_ORG = "ACTIVE_USERS_BY_ORG" # parameter options: min_msg_count, period (days, weeks or months), slices, organization_id, without_kern_email
909+
MESSAGES_CREATED = "MESSAGES_CREATED" # parameter options: period (days, weeks or months), slices, organization_id, without_kern_email
910+
MESSAGES_CREATED_BY_PROJECT = "MESSAGES_CREATED_BY_PROJECT" # parameter options: period (days, weeks or months), slices, organization_id, without_kern_email
911+
MESSAGES_FEEDBACK_PER_PROJECT = "MESSAGES_FEEDBACK_PER_PROJECT" # parameter options: period (days, weeks or months), slices, organization_id, without_kern_email
912+
AVG_MESSAGES_PER_CONVERSATION_GLOBAL = "AVG_MESSAGES_PER_CONVERSATION_GLOBAL" # parameter options: organization_id, without_kern_email
913+
AVG_MESSAGES_PER_CONVERSATION = "AVG_MESSAGES_PER_CONVERSATION" # parameter options: period (days, weeks or months), slices, organization_id, without_kern_email
914+
MACRO_EXECUTIONS = "MACRO_EXECUTIONS" # parameter options: period (days, weeks or months), slices, organization_id, without_kern_email
912915
FOLDER_MACRO_EXECUTION_SUMMARY = (
913916
"FOLDER_MACRO_EXECUTION_SUMMARY" # parameter options: organization_id
914917
)

global_objects/admin_queries.py

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, Optional, Any
1+
from typing import Dict, List, Any
22
from ..util import prevent_sql_injection
33

44
from ..business_objects import general
@@ -14,13 +14,15 @@
1414

1515
def get_result_admin_query(
1616
query: enums.AdminQueries,
17-
parameters: Optional[Dict[str, Any]] = None,
17+
parameters: Dict[str, Any],
1818
as_query: bool = False,
1919
) -> List[Row]:
2020
if parameters is None:
2121
parameters = {}
2222
if query == enums.AdminQueries.USERS_TO_PROJECTS:
2323
return __get_users_to_projects(**parameters, as_query=as_query)
24+
if query == enums.AdminQueries.USERS_BY_ORG:
25+
return __get_users_by_org(**parameters, as_query=as_query)
2426
elif query == enums.AdminQueries.ACTIVE_USERS_GLOBAL:
2527
return __get_active_users_global(**parameters, as_query=as_query)
2628
elif query == enums.AdminQueries.ACTIVE_USERS_BY_ORG:
@@ -44,7 +46,7 @@ def get_result_admin_query(
4446

4547
def __get_folder_macro_execution_summary(
4648
slices: int = 7, # how many chunks are relevant
47-
organization_id: Optional[str] = None,
49+
organization_id: str = "",
4850
as_query: bool = False,
4951
) -> List[Row]:
5052

@@ -116,7 +118,8 @@ def __get_folder_macro_execution_summary(
116118
def __get_macro_executions(
117119
period: str = "days", # options: days, weeks, months
118120
slices: int = 7, # how many chunks are relevant
119-
organization_id: Optional[str] = None,
121+
organization_id: str = "",
122+
without_kern_email: bool = False,
120123
as_query: bool = False,
121124
) -> List[Row]:
122125

@@ -131,6 +134,13 @@ def __get_macro_executions(
131134
)
132135
org_where = f""" WHERE me.organization_id = '{organization_id}'"""
133136

137+
filter_join = ""
138+
if without_kern_email:
139+
filter_join = """
140+
INNER JOIN PUBLIC.user u
141+
ON me.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
142+
"""
143+
134144
query = f"""
135145
WITH params AS (
136146
SELECT
@@ -155,6 +165,7 @@ def __get_macro_executions(
155165
me.organization_id,
156166
date_trunc(p.period, me.created_at)::date AS period_start
157167
FROM cognition.macro_execution me
168+
{filter_join}
158169
INNER JOIN params p
159170
ON me.created_at >= (
160171
SELECT MIN(period_start)
@@ -196,7 +207,8 @@ def __get_macro_executions(
196207
def __get_avg_messages_per_conversation(
197208
period: str = "days", # options: days, weeks, months
198209
slices: int = 7, # how many chunks are relevant
199-
organization_id: Optional[str] = None,
210+
organization_id: str = "",
211+
without_kern_email: bool = False,
200212
as_query: bool = False,
201213
) -> List[Row]:
202214

@@ -211,7 +223,12 @@ def __get_avg_messages_per_conversation(
211223
)
212224
org_where = f""" INNER JOIN cognition.project pr
213225
ON m.project_id = pr.id AND pr.organization_id = '{organization_id}'"""
214-
226+
filter_join = ""
227+
if without_kern_email:
228+
filter_join = """
229+
INNER JOIN PUBLIC.user u
230+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
231+
"""
215232
query = f"""
216233
WITH params AS (
217234
SELECT
@@ -237,6 +254,7 @@ def __get_avg_messages_per_conversation(
237254
date_trunc(p.period, c.created_at)::date AS period_start,
238255
m.conversation_id
239256
FROM cognition.message m
257+
{filter_join}
240258
{org_where}
241259
INNER JOIN cognition.conversation c
242260
ON m.conversation_id = c.id
@@ -294,7 +312,9 @@ def __get_avg_messages_per_conversation(
294312

295313

296314
def __get_global_messages_per_conversation(
297-
organization_id: Optional[str] = None, as_query: bool = False
315+
organization_id: str = "",
316+
without_kern_email: bool = False,
317+
as_query: bool = False,
298318
):
299319
org_where = ""
300320
if organization_id:
@@ -303,6 +323,14 @@ def __get_global_messages_per_conversation(
303323
)
304324
org_where = f""" INNER JOIN cognition.project pr
305325
ON m.project_id = pr.id AND pr.organization_id = '{organization_id}'"""
326+
327+
filter_join = ""
328+
if without_kern_email:
329+
filter_join = """
330+
INNER JOIN PUBLIC.user u
331+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
332+
"""
333+
306334
query = f"""
307335
SELECT
308336
o.name organization_name,
@@ -322,6 +350,7 @@ def __get_global_messages_per_conversation(
322350
conversation_id,
323351
COUNT(*) cnt
324352
FROM cognition.message M
353+
{filter_join}
325354
{org_where}
326355
GROUP BY
327356
m.project_id,
@@ -342,7 +371,8 @@ def __get_global_messages_per_conversation(
342371
def __get_messages_feedback_by_project(
343372
period: str = "days", # options: days, weeks, months
344373
slices: int = 7, # how many chunks are relevant
345-
organization_id: Optional[str] = None,
374+
organization_id: str = "",
375+
without_kern_email: bool = False,
346376
as_query: bool = False,
347377
) -> List[Row]:
348378

@@ -358,6 +388,12 @@ def __get_messages_feedback_by_project(
358388
org_where = f""" INNER JOIN cognition.project pr
359389
ON m.project_id = pr.id AND pr.organization_id = '{organization_id}'"""
360390

391+
filter_join = ""
392+
if without_kern_email:
393+
filter_join = """
394+
INNER JOIN PUBLIC.user u
395+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
396+
"""
361397
query = f"""
362398
WITH params AS (
363399
SELECT
@@ -386,6 +422,7 @@ def __get_messages_feedback_by_project(
386422
m.feedback_category,
387423
m.feedback_value
388424
FROM cognition.message m
425+
{filter_join}
389426
{org_where}
390427
INNER JOIN params p
391428
ON m.created_at >= (SELECT MIN(period_start) FROM periods )
@@ -480,7 +517,8 @@ def __get_messages_feedback_by_project(
480517
def __get_messages_created_by_project(
481518
period: str = "days", # options: days, weeks, months
482519
slices: int = 7, # how many chunks are relevant
483-
organization_id: Optional[str] = None,
520+
organization_id: str = "",
521+
without_kern_email: bool = False,
484522
as_query: bool = False,
485523
) -> List[Row]:
486524

@@ -496,6 +534,12 @@ def __get_messages_created_by_project(
496534
org_where = f""" INNER JOIN cognition.project pr
497535
ON m.project_id = pr.id AND pr.organization_id = '{organization_id}'"""
498536

537+
filter_join = ""
538+
if without_kern_email:
539+
filter_join = """
540+
INNER JOIN PUBLIC.user u
541+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
542+
"""
499543
query = f"""
500544
WITH params AS (
501545
SELECT
@@ -518,6 +562,7 @@ def __get_messages_created_by_project(
518562
m.project_id,
519563
m.created_at
520564
FROM cognition.message m
565+
{filter_join}
521566
{org_where}
522567
INNER JOIN params p
523568
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
@@ -566,7 +611,8 @@ def __get_messages_created_by_project(
566611
def __get_messages_created(
567612
period: str = "days", # options: days, weeks, months
568613
slices: int = 7, # how many chunks are relevant
569-
organization_id: Optional[str] = None,
614+
organization_id: str = "",
615+
without_kern_email: bool = False,
570616
as_query: bool = False,
571617
) -> List[Row]:
572618

@@ -583,6 +629,13 @@ def __get_messages_created(
583629
org_where = f""" INNER JOIN cognition.project pr
584630
ON m.project_id = pr.id AND pr.organization_id = '{organization_id}'"""
585631
org_select = f"(SELECT MAX(NAME) FROM organization WHERE id = '{organization_id}') organization_name,"
632+
633+
filter_join = ""
634+
if without_kern_email:
635+
filter_join = """
636+
INNER JOIN PUBLIC.user u
637+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
638+
"""
586639
query = f"""
587640
WITH
588641
params AS (
@@ -605,6 +658,7 @@ def __get_messages_created(
605658
SELECT
606659
m.created_at
607660
FROM cognition.message m
661+
{filter_join}
608662
{org_where}
609663
INNER JOIN params p
610664
ON m.created_at >= (SELECT MIN(period_start) FROM periods)
@@ -637,7 +691,8 @@ def __get_active_users_by_org(
637691
min_msg_count: int = 1, # minimum number of messages to be considered active
638692
period: str = "days", # options: days, weeks, months
639693
slices: int = 7, # how many chunks are relevant
640-
organization_id: Optional[str] = None,
694+
organization_id: str = "",
695+
without_kern_email: bool = False,
641696
as_query: bool = False,
642697
) -> List[Row]:
643698
min_msg_count = max(min(min_msg_count, 5), 1)
@@ -651,6 +706,12 @@ def __get_active_users_by_org(
651706
organization_id, isinstance(organization_id, str)
652707
)
653708
org_where = f"AND p.organization_id = '{organization_id}'"
709+
filter_join = ""
710+
if without_kern_email:
711+
filter_join = """
712+
INNER JOIN PUBLIC.user u
713+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
714+
"""
654715

655716
query = f"""
656717
WITH params AS (
@@ -676,6 +737,7 @@ def __get_active_users_by_org(
676737
m.created_by,
677738
date_trunc(pa.period, m.created_at)::date AS period_start
678739
FROM cognition.message m
740+
{filter_join}
679741
INNER JOIN cognition.project p
680742
ON m.project_id = p.id {org_where}
681743
INNER JOIN params pa
@@ -719,7 +781,8 @@ def __get_active_users_global(
719781
min_msg_count: int = 1, # minimum number of messages to be considered active
720782
period: str = "days", # options: days, weeks, months
721783
slices: int = 7, # how many chunks are relevant
722-
organization_id: Optional[str] = None,
784+
organization_id: str = "",
785+
without_kern_email: bool = False,
723786
as_query: bool = False,
724787
) -> List[Row]:
725788
# includes type check for sql injection prevention
@@ -737,6 +800,13 @@ def __get_active_users_global(
737800
org_where = f"AND p.organization_id = '{organization_id}'"
738801
org_select = f"(SELECT MAX(NAME) FROM organization WHERE id = '{organization_id}') organization_name,"
739802

803+
filter_join = ""
804+
if without_kern_email:
805+
filter_join = """
806+
INNER JOIN PUBLIC.user u
807+
ON m.created_by = u.id AND u.email NOT LIKE '%@kern.ai'
808+
"""
809+
740810
query = f"""
741811
WITH params AS (
742812
SELECT
@@ -761,6 +831,7 @@ def __get_active_users_global(
761831
m.created_by,
762832
date_trunc(pa.period, m.created_at)::date AS period_start
763833
FROM cognition.message m
834+
{filter_join}
764835
INNER JOIN cognition.project p
765836
ON m.project_id = p.id {org_where}
766837
INNER JOIN params pa
@@ -795,8 +866,42 @@ def __get_active_users_global(
795866
return general.execute_all(query)
796867

797868

869+
def __get_users_by_org(
870+
organization_id: str = "",
871+
without_kern_email: bool = False,
872+
as_query: bool = False,
873+
) -> List[Row]:
874+
where_add = ""
875+
876+
if organization_id:
877+
organization_id = prevent_sql_injection(
878+
organization_id, isinstance(organization_id, str)
879+
)
880+
where_add = f"WHERE o.id = '{organization_id}'"
881+
if without_kern_email:
882+
if where_add:
883+
where_add += " AND"
884+
else:
885+
where_add = "WHERE"
886+
where_add += " u.email NOT LIKE '%@kern.ai'"
887+
888+
query = f"""
889+
SELECT o.name, u.role, COUNT(*)
890+
FROM PUBLIC.user u
891+
INNER JOIN organization o
892+
ON u.organization_id = o.id
893+
{where_add}
894+
GROUP BY 1,2
895+
"""
896+
if as_query:
897+
return query
898+
return general.execute_all(query)
899+
900+
798901
def __get_users_to_projects(
799-
organization_id: Optional[str] = None, as_query: bool = False
902+
organization_id: str = "",
903+
without_kern_email: bool = False,
904+
as_query: bool = False,
800905
) -> List[Row]:
801906

802907
org_where = "u.organization_id IS NOT NULL"
@@ -809,6 +914,10 @@ def __get_users_to_projects(
809914
org_where = f"u.organization_id = '{organization_id}'"
810915
where_add = f"WHERE o.id = '{organization_id}'"
811916

917+
user_where = ""
918+
if without_kern_email:
919+
user_where = "AND u.email NOT LIKE '%@kern.ai'"
920+
812921
query = f"""
813922
WITH user_lookup AS (
814923
SELECT
@@ -834,6 +943,7 @@ def __get_users_to_projects(
834943
WHERE NOT (t.project_id IS NULL AND u.role = '{enums.UserRoles.ANNOTATOR.value}')
835944
AND NOT u.role = '{enums.UserRoles.EXPERT.value}'
836945
AND {org_where}
946+
{user_where}
837947
GROUP BY 1,2,3
838948
)
839949

0 commit comments

Comments
 (0)