@@ -1018,30 +1018,45 @@ def _get_alerts(self, accreditation):
10181018 conf = self ._config [accreditation ]
10191019 pd_service = conf ['service_id' ] if isinstance (conf , dict ) else conf
10201020
1021- # Get all triggered alerts
1022- # NOTE: statuses[]=triggered is supposed to work, but doesn't
1023- # so we have to filter the response. leaving it in the query,
1024- # as it will be a bit more efficient if they fix the issue.
1025- pages = pagerduty .get (
1026- 'alerts' ,
1021+ # Get all current incidents for the service, we need to specify both
1022+ # acknowledged and triggered so we do not send a second page if there
1023+ # has already been one acknowledged
1024+ incidents_data = pagerduty .get (
1025+ 'incidents' ,
10271026 params = {
10281027 'service_ids[]' : pd_service ,
1029- 'statuses[]' : ' triggered' ,
1028+ 'statuses[]' : [ 'acknowledged' , ' triggered'] ,
10301029 'time_zone' : 'UTC'
10311030 },
10321031 creds = self ._creds
10331032 )
1034- alerts = [
1035- {
1036- 'test' : alert ['alert_key' ],
1037- 'details' : alert .get ('body' , {}).get ('details' , '' ),
1038- 'created_at' : alert ['created_at' ]
1039- }
1040- for page in pages
1041- for alert in page .json ().get ('alerts' , [])
1042- if alert ['status' ] == 'triggered'
1043- ]
1044-
1033+ alerts = []
1034+
1035+ # If there are any incidents then loop through them and fetch all the
1036+ # alerts that are related to the service
1037+ for inc_data in incidents_data :
1038+ inc_data .raise_for_status ()
1039+ incidents = inc_data .json ()['incidents' ]
1040+ for inc in incidents :
1041+ alerts_data = pagerduty .get (
1042+ f'incidents/{ inc ["id" ]} /alerts' ,
1043+ params = {
1044+ 'service_ids[]' : pd_service ,
1045+ 'statuses[]' : ['acknowledged' , 'triggered' ],
1046+ 'time_zone' : 'UTC'
1047+ },
1048+ creds = self ._creds
1049+ )
1050+ # Fetch all alerts for the incidents
1051+ for alert_data in alerts_data :
1052+ alert_data .raise_for_status ()
1053+ alerts += [
1054+ {
1055+ 'test' : a ['alert_key' ],
1056+ 'details' : a .get ('body' , {}).get ('details' , '' ),
1057+ 'created_at' : a ['created_at' ]
1058+ } for a in alert_data .json ()['alerts' ]
1059+ ]
10451060 # if more than one alert is listed for a check, only return the latest
10461061 latest_alerts = []
10471062 alerts .sort (key = lambda alert : alert ['created_at' ], reverse = True )
0 commit comments