11from datetime import datetime
2+ import json
23from logging import getLogger
34from pathlib import Path
45from typing import Dict , List , Optional
6566def get_silences (microscope : str ):
6667 try :
6768 silences = requests .get (f"{ alertmanager_url } /api/v2/silences?filter=microscope={ microscope } " )
68- return str (silences .json ())
69+ return (silences .json ())
6970 except :
7071 HTTPException (status_code = silences .status_code , detail = silences .json ())
7172
7273@router .post ("/silences/{microscope}" )
73- def get_silences (microscope : str , end_time : datetime ):
74+ def create_silence (microscope : str , end_time : datetime ):
7475 #for testing
75- # microscope = 'm00'
76+ microscope = 'm00'
7677
7778 start_time = datetime .now ().astimezone ().isoformat ()
7879 end_time = end_time .astimezone ().isoformat ()
@@ -90,17 +91,34 @@ def get_silences(microscope: str, end_time: datetime ):
9091 "startsAt" : str (start_time ),
9192 "endsAt" : str (end_time )
9293 }
93- print (silence_json )
9494 try :
9595 alertmanager_response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
96- print (alertmanager_response )
9796 except :
9897 raise HTTPException (status_code = 500 , detail = "Error creating silence" )
9998 if alertmanager_response .status_code == 200 :
10099 return str (alertmanager_response .json ())
101100 else :
102101 raise HTTPException (status_code = alertmanager_response .status_code , detail = alertmanager_response .json ())
103102
103+ @router .delete ("/silences/{microscope}" )
104+ def delete_silences (microscope : str ):
105+ #for testing
106+ microscope = 'm00'
107+
108+ silences = get_silences (microscope )
109+
110+ ids = [] #for testing
111+ for silence in silences :
112+ if silence ['status' ]['state' ] == 'active' :
113+ id = silence ['id' ]
114+ print (id )
115+ try :
116+ response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
117+ print (response )
118+ except :
119+ raise HTTPException (status_code = 400 , detail = "error deleting silence" )
120+ return "Silences Deleted"
121+
104122@router .get ("/health/" )
105123def health_check (db = ispyb_db ):
106124 conn = db .connection ()
0 commit comments