-
Notifications
You must be signed in to change notification settings - Fork 0
feat: alert lifecycle management, configurable policies, and query performance indexes #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b7abdc6
fix: handle string/date type mismatch in daily cost breakdown
acailic ff9508c
feat: add alert lifecycle management, alert policies, and query perfo…
acailic d4489d8
feat: add AlertDashboardPanel component and alert UI styles
acailic 1f5e249
fix: clean up unused imports in AlertDashboardPanel
acailic ed7b4ea
fix: address PR review feedback for Phase 4 intelligence features
acailic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| """Alert policy API routes for configurable alert thresholds.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from fastapi import APIRouter, Depends, Query | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from api.dependencies import get_db_session, get_tenant_id | ||
| from api.exceptions import NotFoundError | ||
| from api.schemas import AlertPolicyCreate, AlertPolicyListResponse, AlertPolicySchema, AlertPolicyUpdate | ||
| from storage import AlertPolicyRepository | ||
|
|
||
| router = APIRouter(tags=["alert-policies"]) | ||
|
|
||
|
|
||
| async def get_policy_repository( | ||
| session: AsyncSession = Depends(get_db_session), | ||
| tenant_id: str = Depends(get_tenant_id), | ||
| ) -> AlertPolicyRepository: | ||
| """Get an alert policy repository scoped to the current tenant.""" | ||
| return AlertPolicyRepository(session, tenant_id=tenant_id) | ||
|
|
||
|
|
||
| @router.get("/api/alert-policies", response_model=AlertPolicyListResponse) | ||
| async def list_policies( | ||
| agent_name: str | None = Query(default=None), | ||
| limit: int = Query(default=100, ge=1, le=1000), | ||
| repo: AlertPolicyRepository = Depends(get_policy_repository), | ||
| ) -> AlertPolicyListResponse: | ||
| """List all alert policies, optionally filtered by agent_name. | ||
|
|
||
| Args: | ||
| agent_name: Optional agent name filter. If provided, returns both | ||
| agent-specific and global policies for this agent. | ||
| limit: Maximum number of policies to return | ||
| repo: AlertPolicyRepository instance | ||
|
|
||
| Returns: | ||
| List of alert policies | ||
| """ | ||
| policies = await repo.list_policies(agent_name=agent_name, limit=limit) | ||
|
|
||
| return AlertPolicyListResponse( | ||
| policies=[ | ||
| AlertPolicySchema( | ||
| id=policy.id, | ||
| agent_name=policy.agent_name, | ||
| alert_type=policy.alert_type, | ||
| threshold_value=policy.threshold_value, | ||
| severity_threshold=policy.severity_threshold, | ||
| enabled=policy.enabled, | ||
| created_at=policy.created_at, | ||
| updated_at=policy.updated_at, | ||
| ) | ||
| for policy in policies | ||
| ], | ||
| total=len(policies), | ||
| ) | ||
|
|
||
|
|
||
| @router.post("/api/alert-policies", response_model=AlertPolicySchema) | ||
| async def create_policy( | ||
| data: AlertPolicyCreate, | ||
| repo: AlertPolicyRepository = Depends(get_policy_repository), | ||
| ) -> AlertPolicySchema: | ||
| """Create a new alert policy. | ||
|
|
||
| Args: | ||
| data: Policy creation data | ||
| repo: AlertPolicyRepository instance | ||
|
|
||
| Returns: | ||
| Created alert policy | ||
| """ | ||
| policy = await repo.create_policy( | ||
| agent_name=data.agent_name, | ||
| alert_type=data.alert_type, | ||
| threshold_value=data.threshold_value, | ||
| severity_threshold=data.severity_threshold, | ||
| enabled=data.enabled, | ||
| ) | ||
| # Commit to persist the policy | ||
| await repo.session.commit() | ||
| await repo.session.refresh(policy) | ||
|
|
||
| return AlertPolicySchema( | ||
| id=policy.id, | ||
| agent_name=policy.agent_name, | ||
| alert_type=policy.alert_type, | ||
| threshold_value=policy.threshold_value, | ||
| severity_threshold=policy.severity_threshold, | ||
| enabled=policy.enabled, | ||
| created_at=policy.created_at, | ||
| updated_at=policy.updated_at, | ||
| ) | ||
|
|
||
|
|
||
| @router.get("/api/alert-policies/{policy_id}", response_model=AlertPolicySchema) | ||
| async def get_policy( | ||
| policy_id: str, | ||
| repo: AlertPolicyRepository = Depends(get_policy_repository), | ||
| ) -> AlertPolicySchema: | ||
| """Get a single alert policy by ID. | ||
|
|
||
| Args: | ||
| policy_id: Unique identifier of the policy | ||
| repo: AlertPolicyRepository instance | ||
|
|
||
| Returns: | ||
| Alert policy details | ||
|
|
||
| Raises: | ||
| NotFoundError: if policy not found | ||
| """ | ||
| policy = await repo.get_policy(policy_id) | ||
| if not policy: | ||
| raise NotFoundError(f"Policy {policy_id} not found") | ||
|
|
||
| return AlertPolicySchema( | ||
| id=policy.id, | ||
| agent_name=policy.agent_name, | ||
| alert_type=policy.alert_type, | ||
| threshold_value=policy.threshold_value, | ||
| severity_threshold=policy.severity_threshold, | ||
| enabled=policy.enabled, | ||
| created_at=policy.created_at, | ||
| updated_at=policy.updated_at, | ||
| ) | ||
|
|
||
|
|
||
| @router.put("/api/alert-policies/{policy_id}", response_model=AlertPolicySchema) | ||
| async def update_policy( | ||
| policy_id: str, | ||
| data: AlertPolicyUpdate, | ||
| repo: AlertPolicyRepository = Depends(get_policy_repository), | ||
| ) -> AlertPolicySchema: | ||
| """Update an existing alert policy. | ||
|
|
||
| Args: | ||
| policy_id: Unique identifier of the policy to update | ||
| data: Policy update data | ||
| repo: AlertPolicyRepository instance | ||
|
|
||
| Returns: | ||
| Updated alert policy | ||
|
|
||
| Raises: | ||
| NotFoundError: if policy not found | ||
| """ | ||
| policy = await repo.update_policy( | ||
| policy_id=policy_id, | ||
| agent_name=data.agent_name, | ||
| alert_type=data.alert_type, | ||
| threshold_value=data.threshold_value, | ||
| severity_threshold=data.severity_threshold, | ||
| enabled=data.enabled, | ||
| ) | ||
|
|
||
| if not policy: | ||
| raise NotFoundError(f"Policy {policy_id} not found") | ||
|
|
||
| # Commit to persist changes | ||
| await repo.session.commit() | ||
| await repo.session.refresh(policy) | ||
|
|
||
| return AlertPolicySchema( | ||
| id=policy.id, | ||
| agent_name=policy.agent_name, | ||
| alert_type=policy.alert_type, | ||
| threshold_value=policy.threshold_value, | ||
| severity_threshold=policy.severity_threshold, | ||
| enabled=policy.enabled, | ||
| created_at=policy.created_at, | ||
| updated_at=policy.updated_at, | ||
| ) | ||
|
|
||
|
|
||
| @router.delete("/api/alert-policies/{policy_id}") | ||
| async def delete_policy( | ||
| policy_id: str, | ||
| repo: AlertPolicyRepository = Depends(get_policy_repository), | ||
| ) -> dict[str, Any]: | ||
| """Delete an alert policy by ID. | ||
|
|
||
| Args: | ||
| policy_id: Unique identifier of the policy to delete | ||
| repo: AlertPolicyRepository instance | ||
|
|
||
| Returns: | ||
| Deletion confirmation | ||
|
|
||
| Raises: | ||
| NotFoundError: if policy not found | ||
| """ | ||
| deleted = await repo.delete_policy(policy_id) | ||
|
|
||
| if not deleted: | ||
| raise NotFoundError(f"Policy {policy_id} not found") | ||
|
|
||
| # Commit to persist deletion | ||
| await repo.session.commit() | ||
|
|
||
| return {"deleted": True, "policy_id": policy_id} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AlertPolicyUpdatemakes every field optional, but this handler always forwardsdata.agent_name,data.alert_type,data.threshold_value, etc. torepo.update_policy. For omitted fields, Pydantic suppliesNone, which bypasses the repository’s_UNSETsentinel and overwrites existing values (including setting non-null columns likealert_typetoNone, causing a commit-time integrity error/500 on partial updates). This breaks the partial-update contract used byupdateAlertPolicy(..., Partial<AlertPolicy>)and can unintentionally clear policy data.Useful? React with 👍 / 👎.