Skip to content

Commit 4627bca

Browse files
committed
revert changes in alerts
1 parent 887eb6c commit 4627bca

File tree

2 files changed

+16
-44
lines changed

2 files changed

+16
-44
lines changed

src/codegate/api/v1.py

+8-27
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,7 @@ async def hard_delete_workspace(workspace_name: str):
379379
tags=["Workspaces"],
380380
generate_unique_id_function=uniq_name,
381381
)
382-
async def get_workspace_alerts(
383-
workspace_name: str,
384-
page: int = Query(1, ge=1),
385-
page_size: int = Query(API_DEFAULT_PAGE_SIZE, get=1, le=API_MAX_PAGE_SIZE),
386-
) -> List[v1_models.AlertConversation]:
382+
async def get_workspace_alerts(workspace_name: str) -> List[Optional[v1_models.AlertConversation]]:
387383
"""Get alerts for a workspace."""
388384
try:
389385
ws = await wscrud.get_workspace_by_name(workspace_name)
@@ -393,28 +389,13 @@ async def get_workspace_alerts(
393389
logger.exception("Error while getting workspace")
394390
raise HTTPException(status_code=500, detail="Internal server error")
395391

396-
offset = (page - 1) * page_size
397-
fetched_alerts = []
398-
399-
while len(fetched_alerts) < page_size:
400-
alerts_batch = await dbreader.get_alerts_by_workspace(
401-
ws.id, AlertSeverity.CRITICAL.value, page_size, offset
402-
)
403-
if not alerts_batch:
404-
break
405-
406-
dedup_alerts = await v1_processing.remove_duplicate_alerts(alerts_batch)
407-
fetched_alerts.extend(dedup_alerts)
408-
offset += page_size
409-
410-
final_alerts = fetched_alerts[:page_size]
411-
412-
prompt_ids = list({alert.prompt_id for alert in final_alerts if alert.prompt_id})
413-
prompts_outputs = await dbreader.get_prompts_with_output(prompt_ids)
414-
alert_conversations = await v1_processing.parse_get_alert_conversation(
415-
final_alerts, prompts_outputs
416-
)
417-
return alert_conversations
392+
try:
393+
alerts = await dbreader.get_alerts_by_workspace(ws.id, AlertSeverity.CRITICAL.value)
394+
prompts_outputs = await dbreader.get_prompts_with_output(ws.id)
395+
return await v1_processing.parse_get_alert_conversation(alerts, prompts_outputs)
396+
except Exception:
397+
logger.exception("Error while getting alerts and messages")
398+
raise HTTPException(status_code=500, detail="Internal server error")
418399

419400

420401
@v1.get(

src/codegate/db/connection.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,7 @@ async def _exec_select_conditions_to_pydantic(
570570
raise e
571571
return None
572572

573-
async def get_prompts_with_output(self, prompt_ids: List[str]) -> List[GetPromptWithOutputsRow]:
574-
if not prompt_ids:
575-
return []
576-
573+
async def get_prompts_with_output(self, workpace_id: str) -> List[GetPromptWithOutputsRow]:
577574
sql = text(
578575
"""
579576
SELECT
@@ -587,12 +584,11 @@ async def get_prompts_with_output(self, prompt_ids: List[str]) -> List[GetPrompt
587584
o.output_cost
588585
FROM prompts p
589586
LEFT JOIN outputs o ON p.id = o.prompt_id
590-
WHERE (p.id IN :prompt_ids)
587+
WHERE p.workspace_id = :workspace_id
591588
ORDER BY o.timestamp DESC
592589
"""
593-
).bindparams(bindparam("prompt_ids", expanding=True))
594-
595-
conditions = {"prompt_ids": prompt_ids if prompt_ids else None}
590+
)
591+
conditions = {"workspace_id": workpace_id}
596592
prompts = await self._exec_select_conditions_to_pydantic(
597593
GetPromptWithOutputsRow, sql, conditions, should_raise=True
598594
)
@@ -680,11 +676,7 @@ async def _exec_select_count(self, sql_command: str, conditions: dict) -> int:
680676
return 0 # Return 0 in case of failure to avoid crashes
681677

682678
async def get_alerts_by_workspace(
683-
self,
684-
workspace_id: str,
685-
trigger_category: Optional[str] = None,
686-
limit: int = API_DEFAULT_PAGE_SIZE,
687-
offset: int = 0,
679+
self, workspace_id: str, trigger_category: Optional[str] = None
688680
) -> List[Alert]:
689681
sql = text(
690682
"""
@@ -707,13 +699,12 @@ async def get_alerts_by_workspace(
707699
sql = text(sql.text + " AND a.trigger_category = :trigger_category")
708700
conditions["trigger_category"] = trigger_category
709701

710-
sql = text(sql.text + " ORDER BY a.timestamp DESC LIMIT :limit OFFSET :offset")
711-
conditions["limit"] = limit
712-
conditions["offset"] = offset
702+
sql = text(sql.text + " ORDER BY a.timestamp DESC")
713703

714-
return await self._exec_select_conditions_to_pydantic(
704+
prompts = await self._exec_select_conditions_to_pydantic(
715705
Alert, sql, conditions, should_raise=True
716706
)
707+
return prompts
717708

718709
async def get_workspaces(self) -> List[WorkspaceWithSessionInfo]:
719710
sql = text(

0 commit comments

Comments
 (0)