|
2 | 2 | from logging import getLogger |
3 | 3 | from pathlib import Path |
4 | 4 | from typing import Dict, List, Optional |
| 5 | +import requests |
5 | 6 |
|
6 | | -from fastapi import APIRouter, Depends, Request |
| 7 | +from fastapi import APIRouter, Depends, Request, HTTPException |
7 | 8 | from fastapi.responses import FileResponse |
8 | 9 | from pydantic import BaseModel |
9 | 10 | from sqlmodel import select |
|
58 | 59 | tags=["Session Info: General"], |
59 | 60 | ) |
60 | 61 |
|
| 62 | +alertmanager_url = "https://murfey-alertmanager.diamond.ac.uk" |
| 63 | + |
| 64 | +@router.get("/silences/{microscope}") |
| 65 | +def get_silences(microscope: str): |
| 66 | + try: |
| 67 | + silences = requests.get(f"{alertmanager_url}/api/v2/silences?filter=microscope={microscope}") |
| 68 | + return str(silences.json()) |
| 69 | + except: |
| 70 | + HTTPException(status_code=silences.status_code, detail=silences.json()) |
| 71 | + |
| 72 | +@router.post("/silences/{microscope}") |
| 73 | +def get_silences(microscope: str, end_time: datetime ): |
| 74 | + #for testing |
| 75 | + # microscope = 'm00' |
| 76 | + |
| 77 | + start_time = datetime.now().astimezone().isoformat() |
| 78 | + end_time = end_time.astimezone().isoformat() |
| 79 | + silence_json = { |
| 80 | + "matchers":[ |
| 81 | + { |
| 82 | + "name": "microscope", |
| 83 | + "value": microscope, |
| 84 | + "isRegex": False |
| 85 | + }], |
| 86 | + "createdBy": "dvu71330", |
| 87 | + "annotations":{"description": "Test"}, |
| 88 | + "comment": "test", |
| 89 | + "status": {"state": "active"}, |
| 90 | + "startsAt": str(start_time), |
| 91 | + "endsAt": str(end_time) |
| 92 | + } |
| 93 | + print(silence_json) |
| 94 | + try: |
| 95 | + alertmanager_response = requests.post(f"{alertmanager_url}/api/v2/silences", json=silence_json) |
| 96 | + print(alertmanager_response) |
| 97 | + except: |
| 98 | + raise HTTPException(status_code=500, detail= "Error creating silence") |
| 99 | + if alertmanager_response.status_code == 200: |
| 100 | + return str(alertmanager_response.json()) |
| 101 | + else: |
| 102 | + raise HTTPException(status_code=alertmanager_response.status_code, detail=alertmanager_response.json()) |
61 | 103 |
|
62 | 104 | @router.get("/health/") |
63 | 105 | def health_check(db=ispyb_db): |
|
0 commit comments