main
py 91 lines 3.71 KB
Raw
1 from typing import List
2 from typing import Optional
3
4 from fastapi import APIRouter
5 from fastapi import Depends
6 from fastapi import Query
7 from fastapi import Security
8 from sqlalchemy.ext.asyncio import AsyncSession
9
10 from app.auth.utils import AuthHandler
11 from app.connectors.influxdb.schema.alerts import AlertStatus
12 from app.connectors.influxdb.schema.alerts import GetInfluxDBAlertQueryParams
13 from app.connectors.influxdb.schema.alerts import InfluxDBAlertResponse
14 from app.connectors.influxdb.schema.alerts import InfluxDBCheckNamesResponse
15 from app.connectors.influxdb.schema.alerts import SeverityFilter
16 from app.connectors.influxdb.services.alerts import get_influxdb_alerts
17 from app.connectors.influxdb.services.alerts import get_influxdb_check_names
18 from app.db.db_session import get_db
19
20 influxdb_alerts_router = APIRouter()
21
22
23 @influxdb_alerts_router.get(
24 "/alerts",
25 response_model=InfluxDBAlertResponse,
26 description="Get alerts from InfluxDB with advanced filtering",
27 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
28 )
29 async def get_alerts_route(
30 days: int = Query(7, ge=1, le=90, description="Number of days to look back"),
31 severity: Optional[List[SeverityFilter]] = Query(None, description="Filter by severity (can specify multiple)"),
32 check_name: Optional[str] = Query(None, description="Filter by check name"),
33 sensor_type: Optional[str] = Query(None, description="Filter by sensor type"),
34 status: AlertStatus = Query(AlertStatus.ALL, description="Filter by status: active, cleared, or all"),
35 latest_only: bool = Query(False, description="Return only latest alert per check"),
36 exclude_ok: bool = Query(False, description="Exclude alerts with 'ok' status"),
37 limit: Optional[int] = Query(500, ge=1, le=1000, description="Limit the number of returned alerts"),
38 session: AsyncSession = Depends(get_db),
39 ) -> InfluxDBAlertResponse:
40 """
41 Get alerts from InfluxDB with advanced filtering options.
42
43 The connector is always 'InfluxDB' and doesn't need to be specified.
44
45 **Filtering Options:**
46 - `severity`: Filter by severity levels (ok, warning, error, critical)
47 - `check_name`: Filter by specific check name (e.g., "CPU CHECK", "Host Offline")
48 - `sensor_type`: Filter by sensor type keyword
49 - `status`: Show only active alerts, cleared alerts, or all
50 - `latest_only`: Show only the latest alert per check
51 - `exclude_ok`: Automatically exclude 'ok' status alerts for a cleaner view
52
53 **Common Use Cases:**
54 - See only current issues: `exclude_ok=true` or `status=active`
55 - Latest status per check: `latest_only=true`
56 - Active alerts only: `status=active` (shows alerts that haven't been cleared)
57 """
58 query_params = GetInfluxDBAlertQueryParams(
59 days=days,
60 severity=severity,
61 check_name=check_name,
62 sensor_type=sensor_type,
63 status=status,
64 latest_only=latest_only,
65 exclude_ok=exclude_ok,
66 limit=limit,
67 )
68
69 return await get_influxdb_alerts(query_params, session)
70
71
72 @influxdb_alerts_router.get(
73 "/check-names",
74 response_model=InfluxDBCheckNamesResponse,
75 description="Get available check names from InfluxDB",
76 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
77 )
78 async def get_check_names_route(
79 session: AsyncSession = Depends(get_db),
80 ) -> InfluxDBCheckNamesResponse:
81 """
82 Get a list of all available check names from InfluxDB.
83
84 This endpoint retrieves unique check names from the last 30 days,
85 useful for populating filter dropdowns or autocomplete fields.
86
87 **Returns:**
88 - List of unique check names (sorted alphabetically)
89 - Total count of check names
90 """
91 return await get_influxdb_check_names(session)