|
9 | 9 | from backend.schemas import ( |
10 | 10 | AlertAckRequest, |
11 | 11 | AlertListResponse, |
| 12 | + AlertNoteCreate, |
| 13 | + AlertNoteListResponse, |
| 14 | + AlertNoteResponse, |
| 15 | + AlertNoteUpdate, |
12 | 16 | AlertResponse, |
| 17 | + AlertTagsUpdate, |
| 18 | +) |
| 19 | +from backend.services import ( |
| 20 | + acknowledge_alert, |
| 21 | + add_alert_tag, |
| 22 | + create_alert_note, |
| 23 | + delete_alert_note, |
| 24 | + get_alert_notes, |
| 25 | + get_alerts, |
| 26 | + remove_alert_tag, |
| 27 | + resolve_alert, |
| 28 | + update_alert_note, |
| 29 | + update_alert_tags, |
13 | 30 | ) |
14 | | -from backend.services import acknowledge_alert, get_alerts, resolve_alert |
15 | 31 |
|
16 | 32 | router = APIRouter(prefix="/alerts", tags=["alerts"]) |
17 | 33 |
|
18 | 34 |
|
| 35 | +# ─── Note routes (must be before /{alert_id} to avoid UUID collision) ── |
| 36 | + |
| 37 | + |
| 38 | +@router.put( |
| 39 | + "/notes/{note_id}", |
| 40 | + response_model=AlertNoteResponse, |
| 41 | + summary="Update a note", |
| 42 | +) |
| 43 | +async def edit_note( |
| 44 | + note_id: uuid.UUID, |
| 45 | + body: AlertNoteUpdate, |
| 46 | + db: AsyncSession = Depends(get_db), |
| 47 | +) -> AlertNoteResponse: |
| 48 | + """Update the text of an existing note.""" |
| 49 | + note = await update_alert_note(db, str(note_id), body.text) |
| 50 | + if not note: |
| 51 | + raise HTTPException(status_code=404, detail="Note not found") |
| 52 | + return AlertNoteResponse.model_validate(note) |
| 53 | + |
| 54 | + |
| 55 | +@router.delete( |
| 56 | + "/notes/{note_id}", |
| 57 | + status_code=204, |
| 58 | + summary="Delete a note", |
| 59 | +) |
| 60 | +async def remove_note( |
| 61 | + note_id: uuid.UUID, |
| 62 | + db: AsyncSession = Depends(get_db), |
| 63 | +) -> None: |
| 64 | + """Delete a note.""" |
| 65 | + deleted = await delete_alert_note(db, str(note_id)) |
| 66 | + if not deleted: |
| 67 | + raise HTTPException(status_code=404, detail="Note not found") |
| 68 | + |
| 69 | + |
| 70 | +# ─── Alert list & detail ────────────────────────────────── |
| 71 | + |
| 72 | + |
19 | 73 | @router.get( |
20 | 74 | "", |
21 | 75 | response_model=AlertListResponse, |
@@ -67,6 +121,9 @@ async def get_alert( |
67 | 121 | return AlertResponse.model_validate(alert) |
68 | 122 |
|
69 | 123 |
|
| 124 | +# ─── Alert actions ───────────────────────────────────────── |
| 125 | + |
| 126 | + |
70 | 127 | @router.post( |
71 | 128 | "/{alert_id}/acknowledge", |
72 | 129 | response_model=AlertResponse, |
@@ -103,3 +160,96 @@ async def resolve( |
103 | 160 | raise HTTPException(status_code=404, detail="Alert not found") |
104 | 161 |
|
105 | 162 | return AlertResponse.model_validate(alert) |
| 163 | + |
| 164 | + |
| 165 | +# ─── Tags ────────────────────────────────────────────────── |
| 166 | + |
| 167 | + |
| 168 | +@router.put( |
| 169 | + "/{alert_id}/tags", |
| 170 | + response_model=AlertResponse, |
| 171 | + summary="Set alert tags", |
| 172 | +) |
| 173 | +async def set_tags( |
| 174 | + alert_id: uuid.UUID, |
| 175 | + body: AlertTagsUpdate, |
| 176 | + db: AsyncSession = Depends(get_db), |
| 177 | +) -> AlertResponse: |
| 178 | + """Replace all tags on an alert.""" |
| 179 | + alert = await update_alert_tags(db, str(alert_id), body.tags) |
| 180 | + if not alert: |
| 181 | + raise HTTPException(status_code=404, detail="Alert not found") |
| 182 | + return AlertResponse.model_validate(alert) |
| 183 | + |
| 184 | + |
| 185 | +@router.post( |
| 186 | + "/{alert_id}/tags/{tag}", |
| 187 | + response_model=AlertResponse, |
| 188 | + summary="Add a tag to an alert", |
| 189 | +) |
| 190 | +async def add_tag( |
| 191 | + alert_id: uuid.UUID, |
| 192 | + tag: str, |
| 193 | + db: AsyncSession = Depends(get_db), |
| 194 | +) -> AlertResponse: |
| 195 | + """Add a single tag to an alert.""" |
| 196 | + alert = await add_alert_tag(db, str(alert_id), tag) |
| 197 | + if not alert: |
| 198 | + raise HTTPException(status_code=404, detail="Alert not found") |
| 199 | + return AlertResponse.model_validate(alert) |
| 200 | + |
| 201 | + |
| 202 | +@router.delete( |
| 203 | + "/{alert_id}/tags/{tag}", |
| 204 | + response_model=AlertResponse, |
| 205 | + summary="Remove a tag from an alert", |
| 206 | +) |
| 207 | +async def delete_tag( |
| 208 | + alert_id: uuid.UUID, |
| 209 | + tag: str, |
| 210 | + db: AsyncSession = Depends(get_db), |
| 211 | +) -> AlertResponse: |
| 212 | + """Remove a single tag from an alert.""" |
| 213 | + alert = await remove_alert_tag(db, str(alert_id), tag) |
| 214 | + if not alert: |
| 215 | + raise HTTPException(status_code=404, detail="Alert not found") |
| 216 | + return AlertResponse.model_validate(alert) |
| 217 | + |
| 218 | + |
| 219 | +# ─── Notes ───────────────────────────────────────────────── |
| 220 | + |
| 221 | + |
| 222 | +@router.get( |
| 223 | + "/{alert_id}/notes", |
| 224 | + response_model=AlertNoteListResponse, |
| 225 | + summary="List alert notes", |
| 226 | +) |
| 227 | +async def list_notes( |
| 228 | + alert_id: uuid.UUID, |
| 229 | + db: AsyncSession = Depends(get_db), |
| 230 | +) -> AlertNoteListResponse: |
| 231 | + """Get all notes for an alert.""" |
| 232 | + notes = await get_alert_notes(db, str(alert_id)) |
| 233 | + return AlertNoteListResponse( |
| 234 | + notes=[AlertNoteResponse.model_validate(n) for n in notes], |
| 235 | + total=len(notes), |
| 236 | + ) |
| 237 | + |
| 238 | + |
| 239 | +@router.post( |
| 240 | + "/{alert_id}/notes", |
| 241 | + response_model=AlertNoteResponse, |
| 242 | + status_code=201, |
| 243 | + summary="Add a note to an alert", |
| 244 | +) |
| 245 | +async def add_note( |
| 246 | + alert_id: uuid.UUID, |
| 247 | + body: AlertNoteCreate, |
| 248 | + db: AsyncSession = Depends(get_db), |
| 249 | +) -> AlertNoteResponse: |
| 250 | + """Add a timestamped note to an alert.""" |
| 251 | + try: |
| 252 | + note = await create_alert_note(db, str(alert_id), body.text, body.author) |
| 253 | + except ValueError: |
| 254 | + raise HTTPException(status_code=404, detail="Alert not found") |
| 255 | + return AlertNoteResponse.model_validate(note) |
0 commit comments