Пример #1
0
 def test_existing_name(self):
     name = 'uh oh'
     create_alert_rule(
         self.project,
         name,
         AlertRuleThresholdType.ABOVE,
         'level:error',
         [],
         1,
         1,
         1,
         1,
     )
     with self.assertRaises(AlertRuleNameAlreadyUsedError):
         create_alert_rule(
             self.project,
             name,
             AlertRuleThresholdType.ABOVE,
             'level:error',
             [],
             1,
             1,
             1,
             1,
         )
Пример #2
0
 def test_existing_name(self):
     name = "uh oh"
     create_alert_rule(
         self.project,
         name,
         AlertRuleThresholdType.ABOVE,
         "level:error",
         QueryAggregations.TOTAL,
         1,
         1,
         1,
         1,
     )
     with self.assertRaises(AlertRuleNameAlreadyUsedError):
         create_alert_rule(
             self.project,
             name,
             AlertRuleThresholdType.ABOVE,
             "level:error",
             QueryAggregations.TOTAL,
             1,
             1,
             1,
             1,
         )
Пример #3
0
 def test_name_used(self):
     used_name = "uh oh"
     create_alert_rule(
         self.organization,
         [self.project],
         used_name,
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1,
     )
     with self.assertRaises(AlertRuleNameAlreadyUsedError):
         update_alert_rule(self.alert_rule, name=used_name)
Пример #4
0
 def test_invalid_query(self):
     with self.assertRaises(InvalidSearchQuery):
         create_alert_rule(
             self.project,
             "hi",
             AlertRuleThresholdType.ABOVE,
             "has:",
             QueryAggregations.TOTAL,
             1,
             1,
             1,
             1,
         )
Пример #5
0
 def test_invalid_query(self):
     with self.assertRaises(InvalidSearchQuery):
         create_alert_rule(
             self.project,
             'hi',
             AlertRuleThresholdType.ABOVE,
             'has:',
             [],
             1,
             1,
             1,
             1,
         )
Пример #6
0
 def test_existing_name(self):
     name = "uh oh"
     create_alert_rule(self.organization, [self.project], name,
                       "level:error", QueryAggregations.TOTAL, 1, 1)
     with self.assertRaises(AlertRuleNameAlreadyUsedError):
         create_alert_rule(
             self.organization,
             [self.project],
             name,
             "level:error",
             QueryAggregations.TOTAL,
             1,
             1,
         )
Пример #7
0
 def test_name_used(self):
     used_name = "uh oh"
     create_alert_rule(
         self.project,
         used_name,
         AlertRuleThresholdType.ABOVE,
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1000,
         400,
         1,
     )
     with self.assertRaises(AlertRuleNameAlreadyUsedError):
         update_alert_rule(self.alert_rule, name=used_name)
Пример #8
0
 def create(self, validated_data):
     try:
         return create_alert_rule(project=self.context['project'],
                                  **validated_data)
     except AlertRuleNameAlreadyUsedError:
         raise serializers.ValidationError(
             'This name is already in use for this project')
Пример #9
0
    def test_simple(self):
        alert_rule = create_alert_rule(
            self.project,
            'hello',
            AlertRuleThresholdType.ABOVE,
            'level:error',
            [AlertRuleAggregations.TOTAL],
            10,
            1000,
            400,
            1,
        )
        result = serialize(alert_rule)

        assert result['id'] == six.text_type(alert_rule.id)
        assert result['projectId'] == six.text_type(alert_rule.project_id)
        assert result['name'] == alert_rule.name
        assert result['thresholdType'] == alert_rule.threshold_type
        assert result['dataset'] == alert_rule.dataset
        assert result['query'] == alert_rule.query
        assert result['aggregations'] == alert_rule.aggregations
        assert result['timeWindow'] == alert_rule.time_window
        assert result['resolution'] == alert_rule.resolution
        assert result['alertThreshold'] == alert_rule.alert_threshold
        assert result['resolveThreshold'] == alert_rule.resolve_threshold
        assert result['thresholdPeriod'] == alert_rule.threshold_period
        assert result['dateModified'] == alert_rule.date_modified
        assert result['dateAdded'] == alert_rule.date_added
Пример #10
0
 def test(self):
     name = "hello"
     threshold_type = AlertRuleThresholdType.ABOVE
     query = "level:error"
     aggregation = QueryAggregations.TOTAL
     time_window = 10
     alert_threshold = 1000
     resolve_threshold = 400
     threshold_period = 1
     alert_rule = create_alert_rule(
         self.project,
         name,
         threshold_type,
         query,
         aggregation,
         time_window,
         alert_threshold,
         resolve_threshold,
         threshold_period,
     )
     assert alert_rule.project == self.project
     assert alert_rule.name == name
     assert alert_rule.status == AlertRuleStatus.PENDING.value
     assert alert_rule.query_subscriptions.all().count() == 1
     assert alert_rule.threshold_type == threshold_type.value
     assert alert_rule.dataset == QueryDatasets.EVENTS.value
     assert alert_rule.query == query
     assert alert_rule.aggregation == aggregation.value
     assert alert_rule.time_window == time_window
     assert alert_rule.resolution == DEFAULT_ALERT_RULE_RESOLUTION
     assert alert_rule.alert_threshold == alert_threshold
     assert alert_rule.resolve_threshold == resolve_threshold
     assert alert_rule.threshold_period == threshold_period
Пример #11
0
    def create_alert_rule(
        organization,
        projects,
        name=None,
        query="level:error",
        aggregate="count()",
        time_window=10,
        threshold_period=1,
        include_all_projects=False,
        environment=None,
        excluded_projects=None,
        date_added=None,
    ):
        if not name:
            name = petname.Generate(2, " ", letters=10).title()

        alert_rule = create_alert_rule(
            organization,
            projects,
            name,
            query,
            aggregate,
            time_window,
            threshold_period,
            environment=environment,
            include_all_projects=include_all_projects,
            excluded_projects=excluded_projects,
        )

        if date_added is not None:
            alert_rule.update(date_added=date_added)

        return alert_rule
Пример #12
0
    def create_alert_rule(
        organization,
        projects,
        name=None,
        threshold_type=AlertRuleThresholdType.ABOVE,
        query="level:error",
        aggregation=QueryAggregations.TOTAL,
        time_window=10,
        alert_threshold=100,
        resolve_threshold=10,
        threshold_period=1,
        include_all_projects=False,
        excluded_projects=None,
    ):
        if not name:
            name = petname.Generate(2, " ", letters=10).title()

        return create_alert_rule(
            organization,
            projects,
            name,
            threshold_type,
            query,
            aggregation,
            time_window,
            alert_threshold,
            resolve_threshold,
            threshold_period,
            include_all_projects=include_all_projects,
            excluded_projects=excluded_projects,
        )
Пример #13
0
    def test_incidents_list(self):
        alert_rule = create_alert_rule(self.organization, [self.project],
                                       "hello", "level:error", "count()", 10,
                                       1)

        incident = create_incident(
            self.organization,
            type_=IncidentType.DETECTED,
            title="Incident #1",
            query="hello",
            aggregation=QueryAggregations.TOTAL,
            date_started=timezone.now(),
            date_detected=timezone.now(),
            projects=[self.project],
            groups=[self.group],
            alert_rule=alert_rule,
        )

        with self.feature(FEATURE_NAME):
            self.browser.get(self.path)
            self.browser.wait_until_not(".loading-indicator")
            self.browser.wait_until_test_id("incident-sparkline")
            self.browser.snapshot("incidents - list")

            details_url = u'[href="/organizations/{}/alerts/{}/'.format(
                self.organization.slug, incident.identifier)
            self.browser.wait_until(details_url)
            self.browser.click(details_url)
            self.browser.wait_until_not(".loading-indicator")
            self.browser.wait_until_test_id("incident-title")

            self.browser.wait_until_not('[data-test-id="loading-placeholder"]')
            self.browser.snapshot("incidents - details")
Пример #14
0
 def test(self):
     name = "hello"
     threshold_type = AlertRuleThresholdType.ABOVE
     query = "level:error"
     aggregations = [AlertRuleAggregations.TOTAL]
     time_window = 10
     alert_threshold = 1000
     resolve_threshold = 400
     threshold_period = 1
     alert_rule = create_alert_rule(
         self.project,
         name,
         threshold_type,
         query,
         aggregations,
         time_window,
         alert_threshold,
         resolve_threshold,
         threshold_period,
     )
     assert alert_rule.project == self.project
     assert alert_rule.name == name
     assert alert_rule.status == AlertRuleStatus.PENDING.value
     assert alert_rule.subscription_id is not None
     assert alert_rule.threshold_type == threshold_type.value
     assert alert_rule.dataset == SnubaDatasets.EVENTS.value
     assert alert_rule.query == query
     assert alert_rule.aggregations == [agg.value for agg in aggregations]
     assert alert_rule.time_window == time_window
     assert alert_rule.resolution == DEFAULT_ALERT_RULE_RESOLUTION
     assert alert_rule.alert_threshold == alert_threshold
     assert alert_rule.resolve_threshold == resolve_threshold
     assert alert_rule.threshold_period == threshold_period
Пример #15
0
 def rule(self):
     rule = create_alert_rule(
         self.organization,
         [self.project, self.other_project],
         "some rule",
         AlertRuleThresholdType.ABOVE,
         query="",
         aggregation=QueryAggregations.TOTAL,
         time_window=1,
         alert_threshold=100,
         resolve_threshold=10,
         threshold_period=1,
     )
     # Make sure the trigger exists
     trigger = create_alert_rule_trigger(rule,
                                         "hi",
                                         AlertRuleThresholdType.ABOVE,
                                         100,
                                         resolve_threshold=10)
     create_alert_rule_trigger_action(
         trigger,
         AlertRuleTriggerAction.Type.EMAIL,
         AlertRuleTriggerAction.TargetType.USER,
         six.text_type(self.user.id),
     )
     return rule
Пример #16
0
    def test_simple(self):
        alert_rule = create_alert_rule(
            self.project,
            "hello",
            AlertRuleThresholdType.ABOVE,
            "level:error",
            QueryAggregations.TOTAL,
            10,
            1000,
            400,
            1,
        )
        result = serialize(alert_rule)

        assert result["id"] == six.text_type(alert_rule.id)
        assert result["projectId"] == six.text_type(alert_rule.project_id)
        assert result["name"] == alert_rule.name
        assert result["thresholdType"] == alert_rule.threshold_type
        assert result["dataset"] == alert_rule.dataset
        assert result["query"] == alert_rule.query
        assert result["aggregation"] == alert_rule.aggregation
        assert result["timeWindow"] == alert_rule.time_window
        assert result["resolution"] == alert_rule.resolution
        assert result["alertThreshold"] == alert_rule.alert_threshold
        assert result["resolveThreshold"] == alert_rule.resolve_threshold
        assert result["thresholdPeriod"] == alert_rule.threshold_period
        assert result["dateModified"] == alert_rule.date_modified
        assert result["dateAdded"] == alert_rule.date_added
Пример #17
0
 def test(self):
     name = "hello"
     query = "level:error"
     aggregation = QueryAggregations.TOTAL
     time_window = 10
     threshold_period = 1
     alert_rule = create_alert_rule(
         self.organization,
         [self.project],
         name,
         query,
         aggregation,
         time_window,
         threshold_period,
     )
     assert alert_rule.query_subscriptions.get().project == self.project
     assert alert_rule.name == name
     assert alert_rule.status == AlertRuleStatus.PENDING.value
     assert alert_rule.query_subscriptions.all().count() == 1
     assert alert_rule.dataset == QueryDatasets.EVENTS.value
     assert alert_rule.query == query
     assert alert_rule.aggregation == aggregation.value
     assert alert_rule.time_window == time_window
     assert alert_rule.resolution == DEFAULT_ALERT_RULE_RESOLUTION
     assert alert_rule.threshold_period == threshold_period
Пример #18
0
    def test_simple(self):
        incident_type = IncidentType.CREATED
        title = "hello"
        query = "goodbye"
        date_started = timezone.now()
        other_project = self.create_project()
        other_group = self.create_group(project=other_project)
        self.record_event.reset_mock()
        alert_rule = create_alert_rule(
            self.organization,
            [self.project],
            "hello",
            AlertRuleThresholdType.ABOVE,
            "level:error",
            QueryAggregations.TOTAL,
            10,
            1000,
            400,
            1,
        )

        incident = create_incident(
            self.organization,
            type=incident_type,
            title=title,
            query=query,
            date_started=date_started,
            projects=[self.project],
            groups=[self.group, other_group],
            alert_rule=alert_rule,
        )
        assert incident.identifier == 1
        assert incident.status == incident_type.value
        assert incident.title == title
        assert incident.query == query
        assert incident.date_started == date_started
        assert incident.date_detected == date_started
        assert incident.alert_rule == alert_rule
        assert (IncidentGroup.objects.filter(
            incident=incident, group__in=[self.group,
                                          other_group]).count() == 2)
        assert (IncidentProject.objects.filter(
            incident=incident, project__in=[self.project,
                                            other_project]).count() == 2)
        assert (IncidentActivity.objects.filter(
            incident=incident,
            type=IncidentActivityType.CREATED.value,
            event_stats_snapshot__isnull=False,
        ).count() == 1)
        assert len(self.record_event.call_args_list) == 1
        event = self.record_event.call_args[0][0]
        assert isinstance(event, IncidentCreatedEvent)
        assert event.data == {
            "organization_id": six.text_type(self.organization.id),
            "incident_id": six.text_type(incident.id),
            "incident_type": six.text_type(IncidentType.CREATED.value),
        }
        self.calculate_incident_suspects.apply_async.assert_called_once_with(
            kwargs={"incident_id": incident.id})
Пример #19
0
    def create(self, validated_data):
        try:
            # TODO: Remove this, just temporary while we're supporting both fields.
            if "aggregation" not in validated_data:
                raise serializers.ValidationError("aggregation is required")

            return create_alert_rule(organization=self.context["organization"], **validated_data)
        except AlertRuleNameAlreadyUsedError:
            raise serializers.ValidationError("This name is already in use for this project")
Пример #20
0
    def test_simple(self):
        incident_type = IncidentType.ALERT_TRIGGERED
        title = "hello"
        query = "goodbye"
        aggregation = QueryAggregations.UNIQUE_USERS
        date_started = timezone.now()
        other_project = self.create_project(fire_project_created=True)
        other_group = self.create_group(project=other_project)
        alert_rule = create_alert_rule(
            self.organization,
            [self.project],
            "hello",
            "level:error",
            QueryAggregations.TOTAL,
            10,
            1,
        )

        self.record_event.reset_mock()
        incident = create_incident(
            self.organization,
            type=incident_type,
            title=title,
            query=query,
            aggregation=aggregation,
            date_started=date_started,
            projects=[self.project],
            groups=[self.group, other_group],
            alert_rule=alert_rule,
        )
        assert incident.identifier == 1
        assert incident.status == IncidentStatus.OPEN.value
        assert incident.type == incident_type.value
        assert incident.title == title
        assert incident.query == query
        assert incident.aggregation == aggregation.value
        assert incident.date_started == date_started
        assert incident.date_detected == date_started
        assert incident.alert_rule == alert_rule
        assert (IncidentGroup.objects.filter(
            incident=incident, group__in=[self.group,
                                          other_group]).count() == 2)
        assert (IncidentProject.objects.filter(
            incident=incident, project__in=[self.project,
                                            other_project]).count() == 2)
        assert (IncidentActivity.objects.filter(
            incident=incident,
            type=IncidentActivityType.DETECTED.value).count() == 1)
        assert len(self.record_event.call_args_list) == 1
        event = self.record_event.call_args[0][0]
        assert isinstance(event, IncidentCreatedEvent)
        assert event.data == {
            "organization_id": six.text_type(self.organization.id),
            "incident_id": six.text_type(incident.id),
            "incident_type": six.text_type(IncidentType.ALERT_TRIGGERED.value),
        }
 def alert_rule(self):
     return create_alert_rule(
         self.organization,
         [self.project],
         "hello",
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1,
     )
Пример #22
0
 def create(self, validated_data):
     try:
         triggers = validated_data.pop("triggers")
         alert_rule = create_alert_rule(
             organization=self.context["organization"], **validated_data
         )
         self._handle_trigger_updates(alert_rule, triggers)
         return alert_rule
     except AlertRuleNameAlreadyUsedError:
         raise serializers.ValidationError("This name is already in use for this project")
Пример #23
0
    def test_alert_rule(self):
        incident = self.create_incident()
        alert_rule = create_alert_rule(self.organization, [self.project], "hi",
                                       "test query", QueryAggregations.TOTAL,
                                       10, 1)
        incident.update(alert_rule=alert_rule)

        serializer = DetailedIncidentSerializer()
        result = serialize(incident, serializer=serializer)
        assert result["alertRule"] == serialize(alert_rule)
Пример #24
0
    def test_simple(self):
        self.create_team(organization=self.organization, members=[self.user])
        alert_rule = create_alert_rule(
            self.organization, [self.project], "hello", "level:error", "count()", 10, 1
        )

        self.login_as(self.user)
        with self.feature("organizations:incidents"):
            resp = self.get_valid_response(self.organization.slug, self.project.slug)

        assert resp.data == serialize([alert_rule])
Пример #25
0
 def test_simple(self):
     alert_rule = create_alert_rule(
         self.organization,
         [self.project],
         "hello",
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1,
     )
     result = serialize(alert_rule)
     self.assert_alert_rule_serialized(alert_rule, result)
Пример #26
0
    def test_alert_rule(self):
        incident = self.create_incident()
        query = "test query"
        alert_rule = create_alert_rule(self.organization, [self.project], "hi",
                                       "test query", QueryAggregations.TOTAL,
                                       10, 1)
        incident.update(alert_rule=alert_rule)

        serializer = DetailedIncidentSerializer()
        result = serialize(incident, serializer=serializer)
        assert result["alertRule"] == serialize(alert_rule)
        assert result["discoverQuery"] == "event.type:error {}".format(query)
Пример #27
0
 def alert_rule(self):
     return create_alert_rule(
         self.project,
         'hello',
         AlertRuleThresholdType.ABOVE,
         'level:error',
         [AlertRuleAggregations.TOTAL],
         10,
         1000,
         400,
         1,
     )
Пример #28
0
 def alert_rule(self):
     return create_alert_rule(
         self.project,
         "hello",
         AlertRuleThresholdType.ABOVE,
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1000,
         400,
         1,
     )
Пример #29
0
 def test_delete_projects(self):
     alert_rule = create_alert_rule(
         self.organization,
         [self.project, self.create_project(fire_project_created=True)],
         "something",
         "level:error",
         QueryAggregations.TOTAL,
         10,
         1,
     )
     update_alert_rule(alert_rule, [self.project])
     assert self.alert_rule.query_subscriptions.get().project == self.project
Пример #30
0
 def create(self, validated_data):
     try:
         with transaction.atomic():
             triggers = validated_data.pop("triggers")
             alert_rule = create_alert_rule(
                 user=self.context.get("user", None),
                 organization=self.context["organization"],
                 **validated_data,
             )
             self._handle_triggers(alert_rule, triggers)
             return alert_rule
     except AlertRuleNameAlreadyUsedError:
         raise serializers.ValidationError("This name is already in use for this organization")