@@ -39,6 +39,24 @@ def __init__(self):
39
39
self ._headers = {"Authorization" : f"Bearer { self ._token } " }
40
40
self ._version = get_server_version ()
41
41
42
+ def get_run_id_from_name (self , name ):
43
+ """
44
+ Get run id for the specified run name
45
+ """
46
+ params = {'filters' : json .dumps ([f"name == { name } " ])}
47
+
48
+ response = requests .get (f"{ self ._url } /api/runs" , headers = self ._headers , params = params )
49
+
50
+ if response .status_code == 200 :
51
+ if 'data' in response .json ():
52
+ if len (response .json ()['data' ]) == 0 :
53
+ raise RuntimeError ("Could not collect ID - no run found with this name." )
54
+ if len (response .json ()['data' ]) > 1 :
55
+ raise RuntimeError ("Could not collect ID - more than one run exists with this name." )
56
+ else :
57
+ return response .json ()['data' ][0 ]['id' ]
58
+ raise RuntimeError (response .text )
59
+
42
60
def get_run (self , run , system = False , tags = False , metadata = False ):
43
61
"""
44
62
Get a single run
@@ -468,3 +486,36 @@ def get_events(self, run, filter=None, start=0, num=0):
468
486
return response .json ()['data' ]
469
487
470
488
raise Exception (response .text )
489
+
490
+ def get_alerts (self , run , triggered_only = True , names_only = True ):
491
+ """_summary_
492
+
493
+ Parameters
494
+ ----------
495
+ run : str
496
+ The ID of the run to find alerts for
497
+ critical_only : bool, optional
498
+ Whether to only return details about alerts which are currently critical, by default True
499
+ names_only: bool, optional
500
+ Whether to only return the names of the alerts (otherwise return the full details of the alerts), by default True
501
+ """
502
+ response = requests .get (f"{ self ._url } /api/runs/{ run } " , headers = self ._headers )
503
+
504
+ if response .status_code == 404 :
505
+ if 'detail' in response .json ():
506
+ if response .json ()['detail' ] == 'run does not exist' :
507
+ raise Exception ('Run does not exist' )
508
+
509
+ elif response .status_code == 200 :
510
+ if triggered_only :
511
+ if names_only :
512
+ return [alert ['alert' ]['name' ] for alert in response .json ()['alerts' ] if alert ['status' ]['current' ] == 'critical' ]
513
+ else :
514
+ return [alert for alert in response .json ()['alerts' ] if alert ['status' ]['current' ] == 'critical' ]
515
+ else :
516
+ if names_only :
517
+ return [alert ['alert' ]['name' ] for alert in response .json ()['alerts' ]]
518
+ else :
519
+ return response .json ()['alerts' ]
520
+
521
+ raise Exception (response .text )
0 commit comments