55from typing import Dict , List , Optional
66import requests
77
8- from fastapi import APIRouter , Depends , Request , HTTPException
9- from fastapi .responses import FileResponse
8+ from fastapi import APIRouter , Depends , Request
9+ from fastapi .responses import FileResponse , JSONResponse
1010from pydantic import BaseModel
1111from sqlmodel import select
1212
6464
6565@router .get ("/silences/{microscope}" )
6666def get_silences (microscope : str ):
67- try :
68- silences = requests .get (f"{ alertmanager_url } /api/v2/silences?filter=microscope={ microscope } " )
69- active_silences = []
70- for silence in silences .json ():
71- if silence ['status' ]['state' ] == 'active' :
72- active_silences .append (silence )
73- print (active_silences )
74- return (active_silences )
75- except :
76- HTTPException (status_code = silences .status_code , detail = silences .json ())
67+ silences = requests .get (f"{ alertmanager_url } /api/v2/silences?filter=microscope={ microscope } " )
68+ active_silences = []
69+ for silence in silences .json ():
70+ if silence ['status' ]['state' ] == 'active' :
71+ active_silences .append (silence )
72+ return (active_silences )
7773
7874@router .post ("/silences/{microscope}" )
7975def create_silence (microscope : str , end_time : datetime ):
80- #for testing
81- microscope = 'm00'
82-
8376 start_time = datetime .now ().astimezone ().isoformat ()
8477 end_time = end_time .astimezone ().isoformat ()
8578 silence_json = {
@@ -89,38 +82,25 @@ def create_silence(microscope: str, end_time: datetime ):
8982 "value" : microscope ,
9083 "isRegex" : False
9184 }],
92- "createdBy" : "dvu71330 " ,
85+ "createdBy" : "murfey " ,
9386 "annotations" :{"description" : "Test" },
94- "comment" : "test " ,
87+ "comment" : "silence created from murfey " ,
9588 "status" : {"state" : "active" },
9689 "startsAt" : str (start_time ),
9790 "endsAt" : str (end_time )
9891 }
99- try :
100- alertmanager_response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
101- except :
102- raise HTTPException (status_code = 500 , detail = "Error creating silence" )
103- if alertmanager_response .status_code == 200 :
104- return alertmanager_response .json ()
105- else :
106- raise HTTPException (status_code = alertmanager_response .status_code , detail = alertmanager_response .json ())
92+ response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
93+ return JSONResponse (status_code = response .status_code , content = response .json ())
10794
10895@router .delete ("/silences/{microscope}" )
10996def delete_silences (microscope : str ):
110- #for testing
111- microscope = 'm00'
112-
11397 silences = get_silences (microscope )
114-
115- ids = [] #for testing
98+ if len ( silences ) == 0 :
99+ return None
116100 for silence in silences :
117101 id = silence ['id' ]
118- try :
119- response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
120- print (response )
121- except :
122- raise HTTPException (status_code = 400 , detail = "error deleting silence" )
123- return "Silences Deleted"
102+ response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
103+ return response #returns final response in loop
124104
125105@router .get ("/health/" )
126106def health_check (db = ispyb_db ):
0 commit comments