@@ -288,12 +288,15 @@ def get_silences(instrument_name: MurfeyInstrumentName):
288288 index = valid_instrument_names .index (instrument_name )
289289 query_params = f"filter=microscope={ sanitise (valid_instrument_names [index ])} "
290290 else :
291- return None
291+ logger .warning (
292+ f"invalid query parameters for get alerts on { sanitise (instrument_name )} "
293+ )
294+ return []
292295 machine_config = all_configs [instrument_name ]
293296 alertmanager_url = machine_config .alertmanager_url
294- if alertmanager_url == "" :
297+ if not alertmanager_url :
295298 logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
296- return None
299+ return []
297300 response = requests .get (f"{ alertmanager_url } /api/v2/silences?{ query_params } " )
298301 if response .status_code != 200 :
299302 logger .warning (
@@ -307,12 +310,16 @@ def get_silences(instrument_name: MurfeyInstrumentName):
307310
308311
309312@router .post ("/silences/{instrument_name}" )
310- def create_silence (instrument_name : MurfeyInstrumentName , end_time : datetime ):
313+ def create_silence (
314+ instrument_name : MurfeyInstrumentName , end_time : datetime
315+ ) -> JSONResponse :
311316 machine_config = machine_info_by_instrument (instrument_name )
312317 alertmanager_url = machine_config .alertmanager_url
313- if alertmanager_url == "" :
318+ if not alertmanager_url :
314319 logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
315- return None
320+ return JSONResponse (
321+ status_code = 500 , content = "URL not set. Could not turn off alerts"
322+ )
316323 start_time = datetime .now ().astimezone ().isoformat ()
317324 end_time_str = end_time .astimezone ().isoformat ()
318325 silence_json = {
@@ -329,7 +336,7 @@ def create_silence(instrument_name: MurfeyInstrumentName, end_time: datetime):
329336 response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
330337 if response .status_code != 200 :
331338 logger .warning (
332- f"Tried to create silence for { sanitise (instrument_name )} , but received status { response .status_code } from alertmanager API"
339+ f"Create silence for { sanitise (instrument_name )} received status { response .status_code } from alertmanager API"
333340 )
334341 return JSONResponse (
335342 status_code = response .status_code , content = response .json ()
@@ -342,18 +349,18 @@ def create_silence(instrument_name: MurfeyInstrumentName, end_time: datetime):
342349def delete_silences (instrument_name : MurfeyInstrumentName ):
343350 machine_config = machine_info_by_instrument (instrument_name )
344351 alertmanager_url = machine_config .alertmanager_url
345- if alertmanager_url == "" :
352+ if not alertmanager_url :
346353 logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
347- return None
354+ return JSONResponse ( status_code = 500 , content = "URL not set" )
348355 silences = get_silences (instrument_name )
349- if len ( silences ) == 0 :
350- return None
356+ if not silences :
357+ return { "success" : False }
351358 for silence in silences :
352359 id = silence ["id" ]
353360 response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
354361 if response .status_code != 200 :
355362 logger .warning (
356- f"Tried to delete silence { id } for { sanitise (instrument_name )} , but received status { response .status_code } from alertmanager API"
363+ f"Delete silence { id } for { sanitise (instrument_name )} received status { response .status_code } from alertmanager API"
357364 )
358365 return response # returns final response in loop
359366
0 commit comments