예제 #1
0
파일: logic.py 프로젝트: FengLee1113/sentry
def build_incident_query_params(incident,
                                start=None,
                                end=None,
                                windowed_stats=False):
    params = {}
    params["start"], params["end"] = calculate_incident_time_range(
        incident, start, end, windowed_stats=windowed_stats)

    group_ids = list(
        IncidentGroup.objects.filter(incident=incident).values_list("group_id",
                                                                    flat=True))
    if group_ids:
        params["group_ids"] = group_ids
    project_ids = list(
        IncidentProject.objects.filter(incident=incident).values_list(
            "project_id", flat=True))
    if project_ids:
        params["project_id"] = project_ids

    snuba_filter = get_filter(incident.alert_rule.snuba_query.query, params)
    conditions = resolve_discover_aliases(snuba_filter)[0].conditions
    if incident.alert_rule:
        conditions = apply_dataset_conditions(
            QueryDatasets(incident.alert_rule.snuba_query.dataset), conditions)
    return {
        "start": snuba_filter.start,
        "end": snuba_filter.end,
        "conditions": conditions,
        "filter_keys": snuba_filter.filter_keys,
        "having": [],
    }
예제 #2
0
def bulk_build_incident_query_params(incidents,
                                     start=None,
                                     end=None,
                                     windowed_stats=False):
    incident_groups = defaultdict(list)
    for incident_id, group_id in IncidentGroup.objects.filter(
            incident__in=incidents).values_list("incident_id", "group_id"):
        incident_groups[incident_id].append(group_id)
    incident_projects = defaultdict(list)
    for incident_id, project_id in IncidentProject.objects.filter(
            incident__in=incidents).values_list("incident_id", "project_id"):
        incident_projects[incident_id].append(project_id)

    attach_foreignkey(incidents, Incident.alert_rule)

    query_args_list = []
    for incident in incidents:
        params = {}

        params["start"], params["end"] = calculate_incident_time_range(
            incident, start, end, windowed_stats=windowed_stats)

        group_ids = incident_groups[incident.id]
        if group_ids:
            params["group_ids"] = group_ids
        project_ids = incident_projects[incident.id]
        if project_ids:
            params["project_id"] = project_ids

        snuba_filter = get_filter(incident.alert_rule.snuba_query.query,
                                  params)
        conditions = resolve_discover_aliases(snuba_filter)[0].conditions
        if incident.alert_rule:
            conditions = apply_dataset_conditions(
                QueryDatasets(incident.alert_rule.snuba_query.dataset),
                conditions)
        snuba_args = {
            "start": snuba_filter.start,
            "end": snuba_filter.end,
            "conditions": conditions,
            "filter_keys": snuba_filter.filter_keys,
            "having": [],
        }
        query_args_list.append(snuba_args)

    return query_args_list