|
4 | 4 | import sqlite3 |
5 | 5 | import uuid |
6 | 6 | from pathlib import Path |
7 | | -from typing import Dict, List, Optional, Type |
| 7 | +from typing import List, Optional, Type |
8 | 8 |
|
9 | 9 | import numpy as np |
10 | 10 | import sqlite_vec_sl_tmp |
|
28 | 28 | GetMessagesRow, |
29 | 29 | GetWorkspaceByNameConditions, |
30 | 30 | Instance, |
31 | | - IntermediatePromptWithOutputUsageAlerts, |
32 | 31 | MuxRule, |
33 | 32 | Output, |
34 | 33 | Persona, |
@@ -795,6 +794,36 @@ async def get_prompts( |
795 | 794 | ) |
796 | 795 | return rows |
797 | 796 |
|
| 797 | + async def get_total_messages_count_by_workspace_id( |
| 798 | + self, workspace_id: str, trigger_category: Optional[str] = None |
| 799 | + ) -> int: |
| 800 | + """ |
| 801 | + Get total count of unique messages for a given workspace_id, |
| 802 | + considering trigger_category. |
| 803 | + """ |
| 804 | + sql = text( |
| 805 | + """ |
| 806 | + SELECT COUNT(DISTINCT p.id) |
| 807 | + FROM prompts p |
| 808 | + LEFT JOIN alerts a ON p.id = a.prompt_id |
| 809 | + WHERE p.workspace_id = :workspace_id |
| 810 | + """ |
| 811 | + ) |
| 812 | + conditions = {"workspace_id": workspace_id} |
| 813 | + |
| 814 | + if trigger_category: |
| 815 | + sql = text(sql.text + " AND a.trigger_category = :trigger_category") |
| 816 | + conditions["trigger_category"] = trigger_category |
| 817 | + |
| 818 | + async with self._async_db_engine.begin() as conn: |
| 819 | + try: |
| 820 | + result = await conn.execute(sql, conditions) |
| 821 | + count = result.scalar() # Fetches the integer result directly |
| 822 | + return count or 0 # Ensure it returns an integer |
| 823 | + except Exception as e: |
| 824 | + logger.error(f"Failed to fetch message count. Error: {e}") |
| 825 | + return 0 # Return 0 in case of failure |
| 826 | + |
798 | 827 | async def get_alerts_by_workspace( |
799 | 828 | self, workspace_id: str, trigger_category: Optional[str] = None |
800 | 829 | ) -> List[Alert]: |
|
0 commit comments