示例#1
0
def alerting_test(request):
    """
    Allows to test send data on various registered alerting channels
    """
    applications = UserService.resources_with_perms(
        request.user, ["view"], resource_types=["application"])
    # what we can select in total
    all_possible_app_ids = [app.resource_id for app in applications]
    resource = applications[0]

    alert_channels = []
    for channel in request.user.alert_channels:
        alert_channels.append(channel.get_dict())

    cname = request.params.get("channel_name")
    cvalue = request.params.get("channel_value")
    event_name = request.params.get("event_name")
    if cname and cvalue:
        for channel in request.user.alert_channels:
            if channel.channel_value == cvalue and channel.channel_name == cname:
                break
        if event_name in ["error_report_alert", "slow_report_alert"]:
            # opened
            new_event = Event(
                resource_id=resource.resource_id,
                event_type=Event.types[event_name],
                start_date=datetime.datetime.utcnow(),
                status=Event.statuses["active"],
                values={
                    "reports": 5,
                    "threshold": 10
                },
            )
            channel.notify_alert(resource=resource,
                                 event=new_event,
                                 user=request.user,
                                 request=request)

            # closed
            ev_type = Event.types[event_name.replace("open", "close")]
            new_event = Event(
                resource_id=resource.resource_id,
                event_type=ev_type,
                start_date=datetime.datetime.utcnow(),
                status=Event.statuses["closed"],
                values={
                    "reports": 5,
                    "threshold": 10
                },
            )
            channel.notify_alert(resource=resource,
                                 event=new_event,
                                 user=request.user,
                                 request=request)
        elif event_name == "notify_reports":
            report = (
                ReportGroupService.by_app_ids(all_possible_app_ids).filter(
                    ReportGroup.report_type == ReportType.error).first())
            confirmed_reports = [(5, report), (1, report)]
            channel.notify_reports(
                resource=resource,
                user=request.user,
                request=request,
                since_when=datetime.datetime.utcnow(),
                reports=confirmed_reports,
            )
            confirmed_reports = [(5, report)]
            channel.notify_reports(
                resource=resource,
                user=request.user,
                request=request,
                since_when=datetime.datetime.utcnow(),
                reports=confirmed_reports,
            )
        elif event_name == "notify_uptime":
            new_event = Event(
                resource_id=resource.resource_id,
                event_type=Event.types["uptime_alert"],
                start_date=datetime.datetime.utcnow(),
                status=Event.statuses["active"],
                values={
                    "status_code": 500,
                    "tries": 2,
                    "response_time": 0
                },
            )
            channel.notify_uptime_alert(resource=resource,
                                        event=new_event,
                                        user=request.user,
                                        request=request)
        elif event_name == "chart_alert":
            event = EventService.by_type_and_status(
                event_types=(Event.types["chart_alert"], ),
                status_types=(Event.statuses["active"], ),
            ).first()
            channel.notify_chart_alert(resource=event.resource,
                                       event=event,
                                       user=request.user,
                                       request=request)
        elif event_name == "daily_digest":
            since_when = datetime.datetime.utcnow() - datetime.timedelta(
                hours=8)
            filter_settings = {
                "resource": [resource.resource_id],
                "tags": [{
                    "name": "type",
                    "value": ["error"],
                    "op": None
                }],
                "type": "error",
                "start_date": since_when,
            }

            reports = ReportGroupService.get_trending(
                request, filter_settings=filter_settings, limit=50)
            channel.send_digest(
                resource=resource,
                user=request.user,
                request=request,
                since_when=datetime.datetime.utcnow(),
                reports=reports,
            )

    return {
        "alert_channels":
        alert_channels,
        "applications":
        dict([(app.resource_id, app.resource_name)
              for app in applications.all()]),
    }
示例#2
0
def alerting_test(request):
    """
    Allows to test send data on various registered alerting channels
    """
    applications = request.user.resources_with_perms(
        ['view'], resource_types=['application'])
    # what we can select in total
    all_possible_app_ids = [app.resource_id for app in applications]
    resource = applications[0]

    alert_channels = []
    for channel in request.user.alert_channels:
        alert_channels.append(channel.get_dict())

    cname = request.params.get('channel_name')
    cvalue = request.params.get('channel_value')
    event_name = request.params.get('event_name')
    if cname and cvalue:
        for channel in request.user.alert_channels:
            if (channel.channel_value == cvalue
                    and channel.channel_name == cname):
                break
        if event_name in ['error_report_alert', 'slow_report_alert']:
            # opened
            new_event = Event(resource_id=resource.resource_id,
                              event_type=Event.types[event_name],
                              start_date=datetime.datetime.utcnow(),
                              status=Event.statuses['active'],
                              values={
                                  'reports': 5,
                                  'threshold': 10
                              })
            channel.notify_alert(resource=resource,
                                 event=new_event,
                                 user=request.user,
                                 request=request)

            # closed
            ev_type = Event.types[event_name.replace('open', 'close')]
            new_event = Event(resource_id=resource.resource_id,
                              event_type=ev_type,
                              start_date=datetime.datetime.utcnow(),
                              status=Event.statuses['closed'],
                              values={
                                  'reports': 5,
                                  'threshold': 10
                              })
            channel.notify_alert(resource=resource,
                                 event=new_event,
                                 user=request.user,
                                 request=request)
        elif event_name == 'notify_reports':
            report = ReportGroupService.by_app_ids(all_possible_app_ids) \
                .filter(ReportGroup.report_type == ReportType.error).first()
            confirmed_reports = [(5, report), (1, report)]
            channel.notify_reports(resource=resource,
                                   user=request.user,
                                   request=request,
                                   since_when=datetime.datetime.utcnow(),
                                   reports=confirmed_reports)
            confirmed_reports = [(5, report)]
            channel.notify_reports(resource=resource,
                                   user=request.user,
                                   request=request,
                                   since_when=datetime.datetime.utcnow(),
                                   reports=confirmed_reports)
        elif event_name == 'notify_uptime':
            new_event = Event(resource_id=resource.resource_id,
                              event_type=Event.types['uptime_alert'],
                              start_date=datetime.datetime.utcnow(),
                              status=Event.statuses['active'],
                              values={
                                  "status_code": 500,
                                  "tries": 2,
                                  "response_time": 0
                              })
            channel.notify_uptime_alert(resource=resource,
                                        event=new_event,
                                        user=request.user,
                                        request=request)
        elif event_name == 'chart_alert':
            event = EventService.by_type_and_status(
                event_types=(Event.types['chart_alert'], ),
                status_types=(Event.statuses['active'], )).first()
            channel.notify_chart_alert(resource=event.resource,
                                       event=event,
                                       user=request.user,
                                       request=request)
        elif event_name == 'daily_digest':
            since_when = datetime.datetime.utcnow() - datetime.timedelta(
                hours=8)
            filter_settings = {
                'resource': [resource.resource_id],
                'tags': [{
                    'name': 'type',
                    'value': ['error'],
                    'op': None
                }],
                'type': 'error',
                'start_date': since_when
            }

            reports = ReportGroupService.get_trending(
                request, filter_settings=filter_settings, limit=50)
            channel.send_digest(resource=resource,
                                user=request.user,
                                request=request,
                                since_when=datetime.datetime.utcnow(),
                                reports=reports)

    return {
        'alert_channels':
        alert_channels,
        'applications':
        dict([(app.resource_id, app.resource_name)
              for app in applications.all()])
    }