Beispiel #1
0
    def test_uptime_alert(self):
        if not self.integration:
            self.request.session.flash("Integration needs to be configured", "warning")
            return False

        resource = self.integration.resource

        new_event = Event(
            resource_id=resource.resource_id,
            event_type=Event.types["uptime_alert"],
            start_date=datetime.utcnow(),
            status=Event.statuses["active"],
            values={"status_code": 500, "tries": 2, "response_time": 0},
        )

        channel = AlertChannelService.by_integration_id(self.integration.id)
        channel.notify_uptime_alert(
            resource=resource,
            event=new_event,
            user=self.request.user,
            request=self.request,
        )

        self.request.session.flash("Notification sent")
        return True
Beispiel #2
0
    def test_error_alert(self):
        if not self.integration:
            self.request.session.flash('Integration needs to be configured',
                                       'warning')
            return False

        resource = self.integration.resource

        event_name = random.choice((
            'error_report_alert',
            'slow_report_alert',
        ))
        new_event = Event(resource_id=resource.resource_id,
                          event_type=Event.types[event_name],
                          start_date=datetime.utcnow(),
                          status=Event.statuses['active'],
                          values={
                              'reports': random.randint(11, 99),
                              'threshold': 10
                          })

        channel = AlertChannelService.by_integration_id(self.integration.id)

        channel.notify_alert(resource=resource,
                             event=new_event,
                             user=self.request.user,
                             request=self.request)
        self.request.session.flash('Notification sent')
        return True
Beispiel #3
0
    def test_error_alert(self):
        if not self.integration:
            self.request.session.flash("Integration needs to be configured", "warning")
            return False

        resource = self.integration.resource

        event_name = random.choice(("error_report_alert", "slow_report_alert"))
        new_event = Event(
            resource_id=resource.resource_id,
            event_type=Event.types[event_name],
            start_date=datetime.utcnow(),
            status=Event.statuses["active"],
            values={"reports": random.randint(11, 99), "threshold": 10},
        )

        channel = AlertChannelService.by_integration_id(self.integration.id)

        channel.notify_alert(
            resource=resource,
            event=new_event,
            user=self.request.user,
            request=self.request,
        )
        self.request.session.flash("Notification sent")
        return True
Beispiel #4
0
def alert_channels_actions_binds_DELETE(request):
    """
    Removes alert action from users channels
    """
    user = request.user
    channel = AlertChannelService.by_owner_id_and_pkey(
        user.id, request.GET.get("channel_pkey"))

    rule_action = AlertChannelActionService.by_owner_id_and_pkey(
        user.id, request.GET.get("action_pkey"))

    if channel and rule_action:
        if channel.pkey in [c.pkey for c in rule_action.channels]:
            rule_action.channels.remove(channel)
            return rule_action.get_dict(extended_info=True)
    return HTTPUnprocessableEntity()
Beispiel #5
0
    def test_daily_digest(self):
        if not self.integration:
            self.request.session.flash('Integration needs to be configured',
                                       'warning')
            return False

        resource = self.integration.resource
        channel = AlertChannelService.by_integration_id(self.integration.id)

        channel.send_digest(resource=resource,
                            user=self.request.user,
                            request=self.request,
                            since_when=datetime.utcnow(),
                            reports=dummy_reports)
        self.request.session.flash('Notification sent')
        return True
Beispiel #6
0
def alert_channels_actions_binds_POST(request):
    """
    Adds alert action to users channels
    """
    user = request.user
    json_body = request.unsafe_json_body
    channel = AlertChannelService.by_owner_id_and_pkey(
        user.id, json_body.get("channel_pkey"))

    rule_action = AlertChannelActionService.by_owner_id_and_pkey(
        user.id, json_body.get("action_pkey"))

    if channel and rule_action:
        if channel.pkey not in [c.pkey for c in rule_action.channels]:
            rule_action.channels.append(channel)
            return rule_action.get_dict(extended_info=True)
    return HTTPUnprocessableEntity()
Beispiel #7
0
    def test_chart_alert(self):
        if not self.integration:
            self.request.session.flash('Integration needs to be configured',
                                       'warning')
            return False

        resource = self.integration.resource

        chart_values = {
            "matched_rule": {
                'name': 'Fraud attempt limit'
            },
            "matched_step_values": {
                "labels": {
                    "0_1": {
                        "human_label": "Attempts sum"
                    }
                },
                "values": {
                    "0_1": random.randint(11, 55),
                    "key": "2015-12-16T15:49:00"
                }
            },
            "start_interval": datetime.utcnow(),
            "resource": 1,
            "chart_name": "Fraud attempts per day",
            "chart_uuid": "some_uuid",
            "step_size": 3600,
            "action_name": "Notify excessive fraud attempts"
        }

        new_event = Event(resource_id=resource.resource_id,
                          event_type=Event.types['chart_alert'],
                          status=Event.statuses['active'],
                          values=chart_values,
                          target_uuid="some_uuid",
                          start_date=datetime.utcnow())

        channel = AlertChannelService.by_integration_id(self.integration.id)
        channel.notify_chart_alert(resource=resource,
                                   event=new_event,
                                   user=self.request.user,
                                   request=self.request)

        self.request.session.flash('Notification sent')
        return True
Beispiel #8
0
    def test_report_notification(self):
        if not self.integration:
            self.request.session.flash('Integration needs to be configured',
                                       'warning')
            return False

        resource = self.integration.resource

        channel = AlertChannelService.by_integration_id(self.integration.id)

        if random.random() < 0.5:
            confirmed_reports = dummy_reports
        else:
            confirmed_reports = [random.choice(dummy_reports)]

        channel.notify_reports(resource=resource,
                               user=self.request.user,
                               request=self.request,
                               since_when=datetime.utcnow(),
                               reports=confirmed_reports)
        self.request.session.flash('Report notification sent')
        return True