@@ -277,6 +277,65 @@ def update_current_gain_ref(
277277 db .commit ()
278278
279279
280+ # Methods for turning alerts on and off
281+
282+
283+ @router .get ("/silences/{instrument_name}" )
284+ def get_silences (instrument_name : MurfeyInstrumentName ):
285+ all_configs = get_machine_config ()
286+ microscopes = list (all_configs .keys ())
287+ if instrument_name not in microscopes :
288+ return None
289+ machine_config = all_configs [instrument_name ]
290+ alertmanager_url = machine_config .alertmanager_url
291+ silences = requests .get (
292+ f"{ alertmanager_url } /api/v2/silences?filter=microscope={ sanitise (instrument_name )} "
293+ )
294+ active_silences = []
295+ for silence in silences .json ():
296+ if silence ["status" ]["state" ] == "active" :
297+ active_silences .append (silence )
298+ return active_silences
299+
300+
301+ @router .post ("/silences/{instrument_name}" )
302+ def create_silence (instrument_name : MurfeyInstrumentName , end_time : datetime ):
303+ machine_config = machine_info_by_instrument (instrument_name )
304+ alertmanager_url = machine_config .alertmanager_url
305+ start_time = datetime .now ().astimezone ().isoformat ()
306+ end_time_str = end_time .astimezone ().isoformat ()
307+ silence_json = {
308+ "matchers" : [
309+ {"name" : "microscope" , "value" : instrument_name , "isRegex" : False }
310+ ],
311+ "createdBy" : "murfey" ,
312+ "annotations" : {"description" : "Test" },
313+ "comment" : "silence created from murfey" ,
314+ "status" : {"state" : "active" },
315+ "startsAt" : str (start_time ),
316+ "endsAt" : str (end_time_str ),
317+ }
318+ response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
319+ return JSONResponse (
320+ status_code = response .status_code , content = response .json ()
321+ ) # return a response with same data and code as from alertmanager
322+
323+
324+ @router .delete (
325+ "/silences/{instrument_name}"
326+ ) # delete all silences for given microscope
327+ def delete_silences (instrument_name : MurfeyInstrumentName ):
328+ machine_config = machine_info_by_instrument (instrument_name )
329+ alertmanager_url = machine_config .alertmanager_url
330+ silences = get_silences (instrument_name )
331+ if len (silences ) == 0 :
332+ return None
333+ for silence in silences :
334+ id = silence ["id" ]
335+ response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
336+ return response # returns final response in loop
337+
338+
280339spa_router = APIRouter (
281340 prefix = "/session_info/spa" ,
282341 dependencies = [Depends (validate_token )],
@@ -471,62 +530,3 @@ async def get_tiff_file(visit_name: str, session_id: int, tiff_path: str, db=mur
471530 visit_name = visit_name , session_id = session_id , tiff_path = tiff_path , db = db
472531 )
473532 return FileResponse (path = tiff_file ) if isinstance (tiff_file , Path ) else tiff_file
474-
475-
476- # Methods for turning alerts on and off
477-
478-
479- @router .get ("/silences/{instrument_name}" )
480- def get_silences (instrument_name : MurfeyInstrumentName ):
481- all_configs = get_machine_config ()
482- microscopes = list (all_configs .keys ())
483- if instrument_name not in microscopes :
484- return None
485- machine_config = all_configs [instrument_name ]
486- alertmanager_url = machine_config .alertmanager_url
487- silences = requests .get (
488- f"{ alertmanager_url } /api/v2/silences?filter=microscope={ sanitise (instrument_name )} "
489- )
490- active_silences = []
491- for silence in silences .json ():
492- if silence ["status" ]["state" ] == "active" :
493- active_silences .append (silence )
494- return active_silences
495-
496-
497- @router .post ("/silences/{instrument_name}" )
498- def create_silence (instrument_name : MurfeyInstrumentName , end_time : datetime ):
499- machine_config = machine_info_by_instrument (instrument_name )
500- alertmanager_url = machine_config .alertmanager_url
501- start_time = datetime .now ().astimezone ().isoformat ()
502- end_time_str = end_time .astimezone ().isoformat ()
503- silence_json = {
504- "matchers" : [
505- {"name" : "microscope" , "value" : instrument_name , "isRegex" : False }
506- ],
507- "createdBy" : "murfey" ,
508- "annotations" : {"description" : "Test" },
509- "comment" : "silence created from murfey" ,
510- "status" : {"state" : "active" },
511- "startsAt" : str (start_time ),
512- "endsAt" : str (end_time_str ),
513- }
514- response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
515- return JSONResponse (
516- status_code = response .status_code , content = response .json ()
517- ) # return a response with same data and code as from alertmanager
518-
519-
520- @router .delete (
521- "/silences/{instrument_name}"
522- ) # delete all silences for given microscope
523- def delete_silences (instrument_name : MurfeyInstrumentName ):
524- machine_config = machine_info_by_instrument (instrument_name )
525- alertmanager_url = machine_config .alertmanager_url
526- silences = get_silences (instrument_name )
527- if len (silences ) == 0 :
528- return None
529- for silence in silences :
530- id = silence ["id" ]
531- response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
532- return response # returns final response in loop
0 commit comments