|
| 1 | +from typing import Any, Dict, List, Union |
| 2 | +from datetime import datetime, timedelta |
| 3 | + |
| 4 | +from ..business_objects import general |
| 5 | +from ..session import session |
| 6 | +from ..models import SumsTable |
| 7 | +from ..enums import LLMProvider, StrategyStepType |
| 8 | + |
| 9 | + |
| 10 | +def get(id: str) -> SumsTable: |
| 11 | + return ( |
| 12 | + session.query(SumsTable) |
| 13 | + .filter( |
| 14 | + SumsTable.id == id, |
| 15 | + ) |
| 16 | + .first() |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def get_all_by_key(sum_key: str) -> List[SumsTable]: |
| 21 | + return ( |
| 22 | + session.query(SumsTable) |
| 23 | + .filter( |
| 24 | + SumsTable.sum_key == sum_key, |
| 25 | + ) |
| 26 | + .all() |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def get_last_execution_by_key(sum_key: str) -> datetime: |
| 31 | + entry = ( |
| 32 | + session.query(SumsTable) |
| 33 | + .filter( |
| 34 | + SumsTable.sum_key == sum_key, |
| 35 | + ) |
| 36 | + .order_by(SumsTable.created_at.desc()) |
| 37 | + .first() |
| 38 | + ) |
| 39 | + if entry: |
| 40 | + return entry.created_at |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +def get_privatemode_sum_snapshot(as_query: bool = False) -> Dict[str, Any]: |
| 45 | + ## counts messages created with something related to privatemode |
| 46 | + ## this means either tmp_doc, llm or templated values |
| 47 | + ## the messages are count distinct for the previous day to understand when and how much it's used |
| 48 | + ## deleted messages on the fly are not included, for this we would need to track them on creation |
| 49 | + query = f""" |
| 50 | + SELECT json_build_object('counted_for',(CURRENT_DATE - INTERVAL '1 day')::date,'values',array_agg(row_to_json(y))) |
| 51 | + FROM ( |
| 52 | + SELECT o.id organization_id, o.name organization_name, p.id project_id, p.name project_name, is_kern_user, COUNT(*) |
| 53 | + FROM ( |
| 54 | + SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user |
| 55 | + FROM cognition.pipeline_logs pl |
| 56 | + INNER JOIN cognition.strategy_step ss |
| 57 | + ON pl.project_id = ss.project_id AND pl.strategy_step_id = ss.id |
| 58 | + INNER JOIN PUBLIC.user u |
| 59 | + ON pl.created_by = u.id |
| 60 | + WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day' |
| 61 | + AND ss.step_type IN ('{StrategyStepType.LLM.value}' ,'{StrategyStepType.QUERY_REPHRASING.value}','{StrategyStepType.HEADER.value}') |
| 62 | + AND ss.config->>'llmIdentifier' = '{LLMProvider.PRIVATEMODE_AI.value}' |
| 63 | + UNION |
| 64 | + SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user |
| 65 | + FROM cognition.pipeline_logs pl |
| 66 | + INNER JOIN cognition.strategy_step ss |
| 67 | + ON pl.project_id = ss.project_id AND pl.strategy_step_id = ss.id AND ss.step_type = '{StrategyStepType.TEMPLATED.value}' |
| 68 | + INNER JOIN cognition.step_templates st |
| 69 | + ON (ss.config->>'templateId')::UUID = st.id |
| 70 | + INNER JOIN PUBLIC.user u |
| 71 | + ON pl.created_by = u.id |
| 72 | + WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day' |
| 73 | + AND pl.strategy_step_type = '{StrategyStepType.TEMPLATED.value}' |
| 74 | + AND st.config::jsonb -> 'steps' @> '[{{"config": {{"llmIdentifier": "{LLMProvider.PRIVATEMODE_AI.value}"}}}}]' |
| 75 | + UNION |
| 76 | + SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user |
| 77 | + FROM cognition.pipeline_logs pl |
| 78 | + INNER JOIN cognition.project p |
| 79 | + ON pl.project_id = p.id |
| 80 | + INNER JOIN PUBLIC.user u |
| 81 | + ON pl.created_by = u.id |
| 82 | + WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day' |
| 83 | + AND pl.strategy_step_type = '{StrategyStepType.TMP_DOC_RETRIEVAL.value}' |
| 84 | + AND (p.llm_config::jsonb -> 'extraction' ->> 'llmIdentifier' = 'PRIVATEMODE_AI' --written like the enum here so not interpolated |
| 85 | + OR p.llm_config::jsonb -> 'transformation' ->> 'llmIdentifier' = 'PRIVATEMODE_AI') --written like the enum here so not interpolated |
| 86 | + ) x |
| 87 | + INNER JOIN cognition.project p |
| 88 | + ON x.project_id = p.id |
| 89 | + INNER JOIN organization o |
| 90 | + ON p.organization_id = o.id |
| 91 | + group BY o.id, p.id,is_kern_user |
| 92 | + ) y """ |
| 93 | + if as_query: |
| 94 | + return query |
| 95 | + result = general.execute_first(query) |
| 96 | + if result and result[0]: |
| 97 | + return result[0] |
| 98 | + return None |
| 99 | + |
| 100 | + |
| 101 | +def create( |
| 102 | + sum_key: str, |
| 103 | + data: Union[List[Any], Dict[str, Any]], |
| 104 | + with_commit: bool = True, |
| 105 | +) -> SumsTable: |
| 106 | + obj = SumsTable( |
| 107 | + sum_key=sum_key, |
| 108 | + data=data, |
| 109 | + ) |
| 110 | + general.add(obj, with_commit) |
| 111 | + |
| 112 | + return obj |
| 113 | + |
| 114 | + |
| 115 | +def clean_old_entries(sum_key: str, delta: timedelta, with_commit: bool = True) -> None: |
| 116 | + |
| 117 | + session.query(SumsTable).filter( |
| 118 | + SumsTable.sum_key == sum_key, |
| 119 | + SumsTable.created_at < datetime.now() - delta, |
| 120 | + ).delete() |
| 121 | + general.flush_or_commit(with_commit) |
0 commit comments