File tree 3 files changed +66
-0
lines changed
3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -397,6 +397,33 @@ async def get_workspace_alerts(workspace_name: str) -> List[Optional[v1_models.A
397
397
raise HTTPException (status_code = 500 , detail = "Internal server error" )
398
398
399
399
400
+ @v1 .get (
401
+ "/workspaces/{workspace_name}/alerts-summary" ,
402
+ tags = ["Workspaces" ],
403
+ generate_unique_id_function = uniq_name ,
404
+ )
405
+ async def get_workspace_alerts_summary (workspace_name : str ) -> v1_models .AlertSummary :
406
+ """Get alert summary for a workspace."""
407
+ try :
408
+ ws = await wscrud .get_workspace_by_name (workspace_name )
409
+ except crud .WorkspaceDoesNotExistError :
410
+ raise HTTPException (status_code = 404 , detail = "Workspace does not exist" )
411
+ except Exception :
412
+ logger .exception ("Error while getting workspace" )
413
+ raise HTTPException (status_code = 500 , detail = "Internal server error" )
414
+
415
+ try :
416
+ summary = await dbreader .get_alerts_summary_by_workspace (ws .id )
417
+ return v1_models .AlertSummary (
418
+ malicious_packages = summary ["codegate_context_retriever_count" ],
419
+ pii = summary ["codegate_pii_count" ],
420
+ secrets = summary ["codegate_secrets_count" ],
421
+ )
422
+ except Exception :
423
+ logger .exception ("Error while getting alerts summary" )
424
+ raise HTTPException (status_code = 500 , detail = "Internal server error" )
425
+
426
+
400
427
@v1 .get (
401
428
"/workspaces/{workspace_name}/messages" ,
402
429
tags = ["Workspaces" ],
Original file line number Diff line number Diff line change @@ -190,6 +190,16 @@ def from_db_model(db_model: db_models.Alert) -> "Alert":
190
190
timestamp : datetime .datetime
191
191
192
192
193
+ class AlertSummary (pydantic .BaseModel ):
194
+ """
195
+ Represents a set of summary alerts
196
+ """
197
+
198
+ malicious_packages : int
199
+ pii : int
200
+ secrets : int
201
+
202
+
193
203
class PartialQuestionAnswer (pydantic .BaseModel ):
194
204
"""
195
205
Represents a partial conversation.
Original file line number Diff line number Diff line change @@ -746,6 +746,35 @@ async def get_alerts_by_workspace(
746
746
)
747
747
return prompts
748
748
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
+
749
778
async def get_workspaces (self ) -> List [WorkspaceWithSessionInfo ]:
750
779
sql = text (
751
780
"""
You can’t perform that action at this time.
0 commit comments