def generate_email_context(self, status): trigger = self.action.alert_rule_trigger alert_rule = trigger.alert_rule return { "link": absolute_uri( reverse( "sentry-incident", kwargs={ "organization_slug": self.incident.organization.slug, "incident_id": self.incident.identifier, }, ) ), "rule_link": absolute_uri( reverse( "sentry-alert-rule", kwargs={ "organization_slug": self.incident.organization.slug, "alert_rule_id": self.action.alert_rule_trigger.alert_rule_id, }, ) ), "incident_name": self.incident.title, "aggregate": self.query_aggregations_display[QueryAggregations(alert_rule.aggregation)], "query": alert_rule.query, "threshold": trigger.alert_threshold if status == TriggerStatus.ACTIVE else trigger.resolve_threshold, "status": self.status_display[status], }
def test(self): status = TriggerStatus.ACTIVE action = self.create_alert_rule_trigger_action() incident = self.create_incident() handler = EmailActionHandler(action, incident, self.project) expected = { "link": absolute_uri( reverse( "sentry-metric-alert", kwargs={ "organization_slug": incident.organization.slug, "incident_id": incident.identifier, }, )), "rule_link": absolute_uri( reverse( "sentry-alert-rule", kwargs={ "organization_slug": incident.organization.slug, "project_slug": self.project.slug, "alert_rule_id": action.alert_rule_trigger.alert_rule_id, }, )), "incident_name": incident.title, "aggregate": handler.query_aggregations_display[QueryAggregations( action.alert_rule_trigger.alert_rule.aggregation)], "query": action.alert_rule_trigger.alert_rule.query, "threshold": action.alert_rule_trigger.alert_threshold, "status": INCIDENT_STATUS[IncidentStatus(incident.status)], "environment": "All", "is_critical": False, "is_warning": False, "threshold_direction_string": ">", "time_window": "10 minutes", "triggered_at": timezone.now(), "unsubscribe_link": None, } assert expected == handler.generate_email_context(status)
def generate_email_context(self, status): trigger = self.action.alert_rule_trigger alert_rule = trigger.alert_rule is_active = status == TriggerStatus.ACTIVE is_threshold_type_above = trigger.threshold_type == AlertRuleThresholdType.ABOVE.value # if alert threshold and threshold type is above then show '>' # if resolve threshold and threshold type is *BELOW* then show '>' # we can simplify this to be the below statement show_greater_than_string = is_active == is_threshold_type_above environments = list(alert_rule.environment.all()) environment_string = ( ", ".join(sorted([env.name for env in environments])) if len(environments) else "All" ) return { "link": absolute_uri( reverse( "sentry-metric-alert", kwargs={ "organization_slug": self.incident.organization.slug, "incident_id": self.incident.identifier, }, ) ), "rule_link": absolute_uri( reverse( "sentry-alert-rule", kwargs={ "organization_slug": self.incident.organization.slug, "project_slug": self.project.slug, "alert_rule_id": self.action.alert_rule_trigger.alert_rule_id, }, ) ), "incident_name": self.incident.title, "environment": environment_string, "time_window": format_duration(alert_rule.time_window), "triggered_at": trigger.date_added, "aggregate": self.query_aggregations_display[QueryAggregations(alert_rule.aggregation)], "query": alert_rule.query, "threshold": trigger.alert_threshold if is_active else trigger.resolve_threshold, # if alert threshold and threshold type is above then show '>' # if resolve threshold and threshold type is *BELOW* then show '>' "threshold_direction_string": ">" if show_greater_than_string else "<", "status": INCIDENT_STATUS[IncidentStatus(self.incident.status)], "is_critical": self.incident.status == IncidentStatus.CRITICAL, "is_warning": self.incident.status == IncidentStatus.WARNING, "unsubscribe_link": None, }
def test(self): status = TriggerStatus.ACTIVE action = self.create_alert_rule_trigger_action() incident = self.create_incident() handler = EmailActionHandler(action, incident, self.project) expected = { "link": absolute_uri( reverse( "sentry-metric-alert", kwargs={ "organization_slug": incident.organization.slug, "incident_id": incident.identifier, }, )), "rule_link": absolute_uri( reverse( "sentry-alert-rule", kwargs={ "organization_slug": incident.organization.slug, "project_slug": self.project.slug, "alert_rule_id": action.alert_rule_trigger.alert_rule_id, }, )), "incident_name": incident.title, "aggregate": handler.query_aggregations_display[QueryAggregations( action.alert_rule_trigger.alert_rule.aggregation)], "query": action.alert_rule_trigger.alert_rule.query, "threshold": action.alert_rule_trigger.alert_threshold, "status": handler.status_display[status], } assert expected == handler.generate_email_context(status)