|
8 | 8 | from alembic import command as alembic_command
|
9 | 9 | from alembic.config import Config as AlembicConfig
|
10 | 10 | from pydantic import BaseModel
|
11 |
| -from sqlalchemy import CursorResult, TextClause, event, text |
| 11 | +from sqlalchemy import CursorResult, TextClause, bindparam, event, text |
12 | 12 | from sqlalchemy.engine import Engine
|
13 | 13 | from sqlalchemy.exc import IntegrityError, OperationalError
|
14 | 14 | from sqlalchemy.ext.asyncio import create_async_engine
|
@@ -600,38 +600,51 @@ async def get_prompts_with_output_alerts_usage_by_workspace_id(
|
600 | 600 | trigger_category: Optional[str] = None,
|
601 | 601 | limit: int = API_DEFAULT_PAGE_SIZE,
|
602 | 602 | offset: int = 0,
|
| 603 | + filter_by_ids: Optional[List[str]] = None, |
603 | 604 | ) -> List[GetPromptWithOutputsRow]:
|
604 | 605 | """
|
605 | 606 | Get all prompts with their outputs, alerts and token usage by workspace_id.
|
606 | 607 | """
|
607 |
| - sql = text( |
608 |
| - """ |
| 608 | + |
| 609 | + base_query = """ |
609 | 610 | SELECT
|
610 |
| - p.id as prompt_id, p.timestamp as prompt_timestamp, p.provider, p.request, p.type, |
611 |
| - o.id as output_id, o.output, o.timestamp as output_timestamp, o.input_tokens, o.output_tokens, o.input_cost, o.output_cost, |
612 |
| - a.id as alert_id, a.code_snippet, a.trigger_string, a.trigger_type, a.trigger_category, a.timestamp as alert_timestamp |
| 611 | + p.id as prompt_id, p.timestamp as prompt_timestamp, p.provider, p.request, p.type, |
| 612 | + o.id as output_id, o.output, o.timestamp as output_timestamp, o.input_tokens, o.output_tokens, o.input_cost, o.output_cost, |
| 613 | + a.id as alert_id, a.code_snippet, a.trigger_string, a.trigger_type, a.trigger_category, a.timestamp as alert_timestamp |
613 | 614 | FROM prompts p
|
614 | 615 | LEFT JOIN outputs o ON p.id = o.prompt_id
|
615 | 616 | LEFT JOIN alerts a ON p.id = a.prompt_id
|
616 | 617 | WHERE p.workspace_id = :workspace_id
|
617 |
| - """ # noqa: E501 |
618 |
| - ) |
619 |
| - conditions = {"workspace_id": workspace_id} |
620 |
| - if trigger_category: |
621 |
| - sql = text(sql.text + " AND a.trigger_category = :trigger_category") |
622 |
| - conditions["trigger_category"] = trigger_category |
| 618 | + AND (:trigger_category IS NULL OR a.trigger_category = :trigger_category) |
| 619 | + """ # noqa: E501 |
623 | 620 |
|
624 |
| - sql = text( |
625 |
| - sql.text + " ORDER BY o.timestamp DESC, a.timestamp DESC LIMIT :limit OFFSET :offset" |
626 |
| - ) |
627 |
| - conditions["limit"] = limit |
628 |
| - conditions["offset"] = offset |
| 621 | + if filter_by_ids: |
| 622 | + base_query += " AND p.id IN :filter_ids" |
629 | 623 |
|
630 |
| - fetched_rows: List[IntermediatePromptWithOutputUsageAlerts] = ( |
631 |
| - await self._exec_select_conditions_to_pydantic( |
632 |
| - IntermediatePromptWithOutputUsageAlerts, sql, conditions, should_raise=True |
633 |
| - ) |
| 624 | + base_query += """ |
| 625 | + ORDER BY o.timestamp DESC, a.timestamp DESC |
| 626 | + LIMIT :limit OFFSET :offset |
| 627 | + """ |
| 628 | + |
| 629 | + sql = text(base_query) |
| 630 | + |
| 631 | + conditions = { |
| 632 | + "workspace_id": workspace_id, |
| 633 | + "trigger_category": trigger_category, |
| 634 | + "limit": limit, |
| 635 | + "offset": offset, |
| 636 | + } |
| 637 | + |
| 638 | + if filter_by_ids: |
| 639 | + sql = sql.bindparams(bindparam("filter_ids", expanding=True)) |
| 640 | + conditions["filter_ids"] = filter_by_ids |
| 641 | + |
| 642 | + fetched_rows: List[ |
| 643 | + IntermediatePromptWithOutputUsageAlerts |
| 644 | + ] = await self._exec_select_conditions_to_pydantic( |
| 645 | + IntermediatePromptWithOutputUsageAlerts, sql, conditions, should_raise=True |
634 | 646 | )
|
| 647 | + |
635 | 648 | prompts_dict: Dict[str, GetPromptWithOutputsRow] = {}
|
636 | 649 | for row in fetched_rows:
|
637 | 650 | prompt_id = row.prompt_id
|
|
0 commit comments