def test(self):
     stat_keys = build_trigger_stat_keys(
         AlertRule(id=1),
         QuerySubscription(project_id=2),
         [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)],
     )
     assert stat_keys == [
         "{alert_rule:1:project:2}:trigger:3:alert_triggered",
         "{alert_rule:1:project:2}:trigger:4:alert_triggered",
     ]
    def get(self, request: Request) -> Response:
        organization = Organization(slug="myorg")
        project = Project(slug="myproject", organization=organization)

        query = SnubaQuery(
            time_window=60, query="transaction:/some/transaction", aggregate="count()"
        )
        alert_rule = AlertRule(id=1, organization=organization, name="My Alert", snuba_query=query)
        incident = Incident(
            id=2,
            identifier=123,
            organization=organization,
            title="Something broke",
            alert_rule=alert_rule,
            status=IncidentStatus.CRITICAL,
        )
        trigger = AlertRuleTrigger(alert_rule=alert_rule)

        context = generate_incident_trigger_email_context(
            project, incident, trigger, TriggerStatus.ACTIVE
        )

        return MailPreview(
            text_template="sentry/emails/incidents/trigger.txt",
            html_template="sentry/emails/incidents/trigger.html",
            context=context,
        ).render(request)
    def get(self, request):
        organization = Organization(slug="myorg")
        project = Project(id=30, slug="myproj")

        incident = Incident(identifier=123,
                            organization=organization,
                            title="Something broke")
        alert_rule = AlertRule(id=1,
                               organization=organization,
                               aggregation=1,
                               query="is:unresolved")
        alert_rule_trigger = AlertRuleTrigger(id=5,
                                              alert_rule=alert_rule,
                                              alert_threshold=100,
                                              resolve_threshold=50)
        action = AlertRuleTriggerAction(id=10,
                                        alert_rule_trigger=alert_rule_trigger)

        handler = EmailActionHandler(action, incident, project)
        email = handler.build_message(
            handler.generate_email_context(TriggerStatus.ACTIVE),
            TriggerStatus.ACTIVE, 1)
        return MailPreview(html_template=email.html_template,
                           text_template=email.template,
                           context=email.context).render(request)
    def test(self):
        alert_rule = AlertRule(id=1)
        sub = QuerySubscription(project_id=2)
        triggers = [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)]
        client = get_redis_client()
        pipeline = client.pipeline()
        timestamp = datetime.now().replace(tzinfo=pytz.utc, microsecond=0)
        pipeline.set("{alert_rule:1:project:2}:last_update", int(to_timestamp(timestamp)))
        pipeline.set("{alert_rule:1:project:2}:resolve_triggered", 20)
        for key, value in [
            ("{alert_rule:1:project:2}:trigger:3:alert_triggered", 1),
            ("{alert_rule:1:project:2}:trigger:4:alert_triggered", 3),
        ]:
            pipeline.set(key, value)
        pipeline.execute()

        last_update, alert_counts, resolve_counts = get_alert_rule_stats(alert_rule, sub, triggers)
        assert last_update == timestamp
        assert alert_counts == {3: 1, 4: 3}
        assert resolve_counts == 20
    def test(self):
        alert_rule = AlertRule(id=1)
        sub = QuerySubscription(project_id=2)
        triggers = [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)]
        client = get_redis_client()
        pipeline = client.pipeline()
        pipeline.set("{alert_rule:1:project:2}:last_update", 1234)
        for key, value in [
            ("{alert_rule:1:project:2}:trigger:3:alert_triggered", 1),
            ("{alert_rule:1:project:2}:trigger:3:resolve_triggered", 2),
            ("{alert_rule:1:project:2}:trigger:4:alert_triggered", 3),
            ("{alert_rule:1:project:2}:trigger:4:resolve_triggered", 4),
        ]:
            pipeline.set(key, value)
        pipeline.execute()

        last_update, alert_counts, resolve_counts = get_alert_rule_stats(alert_rule, sub, triggers)
        assert last_update == 1234
        assert alert_counts == {3: 1, 4: 3}
        assert resolve_counts == {3: 2, 4: 4}