|
41 | 41 | ) |
42 | 42 | from sentry.incidents.tasks import handle_trigger_action |
43 | 43 | from sentry.incidents.utils.process_update_helpers import ( |
| 44 | + calculate_event_date_from_update_date, |
44 | 45 | get_comparison_aggregation_value, |
45 | 46 | get_crash_rate_alert_metrics_aggregation_value_helper, |
46 | 47 | ) |
@@ -631,25 +632,6 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None: |
631 | 632 | # before the next one then we might alert twice. |
632 | 633 | self.update_alert_rule_stats() |
633 | 634 |
|
634 | | - def calculate_event_date_from_update_date(self, update_date: datetime) -> datetime: |
635 | | - """ |
636 | | - Calculates the date that an event actually happened based on the date that we |
637 | | - received the update. This takes into account time window and threshold period. |
638 | | - :return: |
639 | | - """ |
640 | | - # Subscriptions label buckets by the end of the bucket, whereas discover |
641 | | - # labels them by the front. This causes us an off-by-one error with event dates, |
642 | | - # so to prevent this we subtract a bucket off of the date. |
643 | | - update_date -= timedelta(seconds=self.alert_rule.snuba_query.time_window) |
644 | | - # We want to also subtract `frequency * (threshold_period - 1)` from the date. |
645 | | - # This allows us to show the actual start of the event, rather than the date |
646 | | - # of the last update that we received. |
647 | | - return update_date - timedelta( |
648 | | - seconds=( |
649 | | - self.alert_rule.snuba_query.resolution * (self.alert_rule.threshold_period - 1) |
650 | | - ) |
651 | | - ) |
652 | | - |
653 | 635 | def trigger_alert_threshold( |
654 | 636 | self, trigger: AlertRuleTrigger, metric_value: float |
655 | 637 | ) -> IncidentTrigger | None: |
@@ -695,7 +677,9 @@ def trigger_alert_threshold( |
695 | 677 |
|
696 | 678 | # Only create a new incident if we don't already have an active incident for the AlertRule |
697 | 679 | if not self.active_incident: |
698 | | - detected_at = self.calculate_event_date_from_update_date(self.last_update) |
| 680 | + detected_at = calculate_event_date_from_update_date( |
| 681 | + self.last_update, self.alert_rule.snuba_query, self.alert_rule.threshold_period |
| 682 | + ) |
699 | 683 | self.active_incident = create_incident( |
700 | 684 | organization=self.alert_rule.organization, |
701 | 685 | incident_type=IncidentType.ALERT_TRIGGERED, |
@@ -767,7 +751,11 @@ def trigger_resolve_threshold( |
767 | 751 | self.active_incident, |
768 | 752 | IncidentStatus.CLOSED, |
769 | 753 | status_method=IncidentStatusMethod.RULE_TRIGGERED, |
770 | | - date_closed=self.calculate_event_date_from_update_date(self.last_update), |
| 754 | + date_closed=calculate_event_date_from_update_date( |
| 755 | + self.last_update, |
| 756 | + self.alert_rule.snuba_query, |
| 757 | + self.alert_rule.threshold_period, |
| 758 | + ), |
771 | 759 | ) |
772 | 760 | self.active_incident = None |
773 | 761 | self.incident_trigger_map.clear() |
|
0 commit comments