@@ -677,10 +677,10 @@ async def get_prompts_with_output_alerts_usage_by_workspace_id(
677677 # If trigger category is None we want to get all alerts
678678 trigger_category = trigger_category if trigger_category else "%"
679679 conditions = {"workspace_id" : workspace_id , "trigger_category" : trigger_category }
680- rows : List [IntermediatePromptWithOutputUsageAlerts ] = (
681- await self . _exec_select_conditions_to_pydantic (
682- IntermediatePromptWithOutputUsageAlerts , sql , conditions , should_raise = True
683- )
680+ rows : List [
681+ IntermediatePromptWithOutputUsageAlerts
682+ ] = await self . _exec_select_conditions_to_pydantic (
683+ IntermediatePromptWithOutputUsageAlerts , sql , conditions , should_raise = True
684684 )
685685 prompts_dict : Dict [str , GetPromptWithOutputsRow ] = {}
686686 for row in rows :
@@ -746,6 +746,35 @@ async def get_alerts_by_workspace(
746746 )
747747 return prompts
748748
749+ async def get_alerts_summary_by_workspace (self , workspace_id : str ) -> dict :
750+ """Get aggregated alert summary counts for a given workspace_id."""
751+ sql = text (
752+ """
753+ SELECT
754+ COUNT(*) AS total_alerts,
755+ SUM(CASE WHEN a.trigger_type = 'codegate-secrets' THEN 1 ELSE 0 END) AS codegate_secrets_count,
756+ SUM(CASE WHEN a.trigger_type = 'codegate-context-retriever' THEN 1 ELSE 0 END) AS codegate_context_retriever_count,
757+ SUM(CASE WHEN a.trigger_type = 'codegate-pii' THEN 1 ELSE 0 END) AS codegate_pii_count
758+ FROM alerts a
759+ INNER JOIN prompts p ON p.id = a.prompt_id
760+ WHERE p.workspace_id = :workspace_id
761+ """
762+ )
763+ conditions = {"workspace_id" : workspace_id }
764+
765+ async with self ._async_db_engine .begin () as conn :
766+ result = await conn .execute (sql , conditions )
767+ row = result .fetchone ()
768+
769+ # Return a dictionary with counts (handling None values safely)
770+ return {
771+ "codegate_secrets_count" : row .codegate_secrets_count or 0 if row else 0 ,
772+ "codegate_context_retriever_count" : (
773+ row .codegate_context_retriever_count or 0 if row else 0
774+ ),
775+ "codegate_pii_count" : row .codegate_pii_count or 0 if row else 0 ,
776+ }
777+
749778 async def get_workspaces (self ) -> List [WorkspaceWithSessionInfo ]:
750779 sql = text (
751780 """
0 commit comments