Skip to content

Commit d887aa2

Browse files
committed
add get and post silences
1 parent 00a3938 commit d887aa2

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

src/murfey/server/api/session_info.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from logging import getLogger
33
from pathlib import Path
44
from typing import Dict, List, Optional
5+
import requests
56

6-
from fastapi import APIRouter, Depends, Request
7+
from fastapi import APIRouter, Depends, Request, HTTPException
78
from fastapi.responses import FileResponse
89
from pydantic import BaseModel
910
from sqlmodel import select
@@ -58,6 +59,47 @@
5859
tags=["Session Info: General"],
5960
)
6061

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())
61103

62104
@router.get("/health/")
63105
def health_check(db=ispyb_db):

0 commit comments

Comments
 (0)