@@ -607,7 +607,18 @@ def set_folder_details(self, path, metadata={}, tags=[], description=None):
607607
608608 return False
609609
610- def add_alert (self , name , type , metric , frequency , window , threshold = None , range_low = None , range_high = None , notification = 'none' ):
610+ def add_alert (self ,
611+ name ,
612+ source = 'metrics' ,
613+ frequency = 5 ,
614+ window = 5 ,
615+ rule = None ,
616+ metric = None ,
617+ threshold = None ,
618+ range_low = None ,
619+ range_high = None ,
620+ notification = 'none' ,
621+ pattern = None ):
611622 """
612623 Creates an alert with the specified name (if it doesn't exist) and applies it to the current run
613624 """
@@ -619,9 +630,10 @@ def add_alert(self, name, type, metric, frequency, window, threshold=None, range
619630 self ._error ('Run is not active' )
620631 return False
621632
622- if type not in ('is below' , 'is above' , 'is outside range' , 'is inside range' ):
623- self ._error ('alert type invalid' )
624- return False
633+ if rule :
634+ if rule not in ('is below' , 'is above' , 'is outside range' , 'is inside range' ):
635+ self ._error ('alert rule invalid' )
636+ return False
625637
626638 if type in ('is below' , 'is above' ) and threshold is None :
627639 self ._error ('threshold must be defined for the specified alert type' )
@@ -635,18 +647,30 @@ def add_alert(self, name, type, metric, frequency, window, threshold=None, range
635647 self ._error ('notification must be either none or email' )
636648 return False
637649
650+ if source not in ('metrics' , 'events' ):
651+ self ._error ('source must be either metrics or events' )
652+ return False
653+
654+ alert_definition = {}
655+
656+ if source == 'metrics' :
657+ alert_definition ['metric' ] = metric
658+ alert_definition ['window' ] = window
659+ alert_definition ['rule' ] = rule
660+ if threshold is not None :
661+ alert_definition ['threshold' ] = threshold
662+ elif range_low is not None and range_high is not None :
663+ alert_definition ['range_low' ] = range_low
664+ alert_definition ['range_high' ] = range_high
665+ else :
666+ alert_definition ['pattern' ] = pattern
667+
638668 alert = {'name' : name ,
639- 'type' : type ,
640669 'metric' : metric ,
641670 'frequency' : frequency ,
642- 'window' : window ,
643- 'notification' : notification }
644-
645- if threshold is not None :
646- alert ['threshold' ] = threshold
647- elif range_low is not None and range_high is not None :
648- alert ['range_low' ] = range_low
649- alert ['range_high' ] = range_high
671+ 'notification' : notification ,
672+ 'source' : source ,
673+ 'alert' : alert_definition }
650674
651675 try :
652676 response = requests .post (f"{ self ._url } /api/alerts" , headers = self ._headers , json = alert )
@@ -665,3 +689,5 @@ def add_alert(self, name, type, metric, frequency, window, threshold=None, range
665689
666690 if response .status_code == 200 :
667691 return True
692+
693+ return False
0 commit comments