Exemple #1
0
    def serialize(self, obj, attrs, user, **kwargs):
        query_keys = [
            "environment",
            "query",
            "fields",
            "widths",
            "conditions",
            "aggregations",
            "range",
            "start",
            "end",
            "orderby",
            "limit",
            "yAxis",
            "display",
            "topEvents",
        ]
        data = {
            "id":
            str(obj.id),
            "name":
            obj.name,
            "projects": [project.id for project in obj.projects.all()],
            "version":
            obj.version or obj.query.get("version", 1),
            "expired":
            False,
            "dateCreated":
            obj.date_created,
            "dateUpdated":
            obj.date_updated,
            "createdBy":
            serialize(obj.created_by, serializer=UserSerializer())
            if obj.created_by else None,
        }

        for key in query_keys:
            if obj.query.get(key) is not None:
                data[key] = obj.query[key]

        # expire queries that are beyond the retention period
        if "start" in obj.query:
            start, end = parse_timestamp(obj.query["start"]), parse_timestamp(
                obj.query["end"])
            if start and end:
                data["expired"], data[
                    "start"] = outside_retention_with_modified_start(
                        start, end, obj.organization)

        if obj.query.get("all_projects"):
            data["projects"] = list(ALL_ACCESS_PROJECTS)

        return data
Exemple #2
0
def _prepare_query_params(query_params):
    # convert to naive UTC datetimes, as Snuba only deals in UTC
    # and this avoids offset-naive and offset-aware issues
    start = naiveify_datetime(query_params.start)
    end = naiveify_datetime(query_params.end)

    with timer("get_snuba_map"):
        forward, reverse = get_snuba_translators(
            query_params.filter_keys,
            is_grouprelease=query_params.is_grouprelease)

    if query_params.dataset in [
            Dataset.Events,
            Dataset.Discover,
            Dataset.Sessions,
            Dataset.Transactions,
    ]:
        (organization_id,
         params_to_update) = get_query_params_to_update_for_projects(
             query_params, with_org=query_params.dataset == Dataset.Sessions)
    elif query_params.dataset in [Dataset.Outcomes, Dataset.OutcomesRaw]:
        (organization_id, params_to_update
         ) = get_query_params_to_update_for_organizations(query_params)
    else:
        raise UnqualifiedQueryError(
            "No strategy found for getting an organization for the given dataset."
        )

    query_params.kwargs.update(params_to_update)

    for col, keys in forward(deepcopy(query_params.filter_keys)).items():
        if keys:
            if len(keys) == 1 and None in keys:
                query_params.conditions.append((col, "IS NULL", None))
            else:
                query_params.conditions.append((col, "IN", keys))

    expired, start = outside_retention_with_modified_start(
        start, end, Organization(organization_id))
    if expired:
        raise QueryOutsideRetentionError(
            "Invalid date range. Please try a more recent date range.")

    # if `shrink_time_window` pushed `start` after `end` it means the user queried
    # a Group for T1 to T2 when the group was only active for T3 to T4, so the query
    # wouldn't return any results anyway
    new_start = shrink_time_window(query_params.filter_keys.get("group_id"),
                                   start)

    # TODO (alexh) this is a quick emergency fix for an occasion where a search
    # results in only 1 django candidate, which is then passed to snuba to
    # check and we raised because of it. Remove this once we figure out why the
    # candidate was returned from django at all if it existed only outside the
    # time range of the query
    if new_start <= end:
        start = new_start

    if start > end:
        raise QueryOutsideGroupActivityError

    query_params.kwargs.update({
        "dataset": query_params.dataset.value,
        "from_date": start.isoformat(),
        "to_date": end.isoformat(),
        "groupby": query_params.groupby,
        "conditions": query_params.conditions,
        "aggregations": query_params.aggregations,
        "granularity": query_params.rollup,  # TODO name these things the same
    })
    kwargs = {k: v for k, v in query_params.kwargs.items() if v is not None}

    kwargs.update(OVERRIDE_OPTIONS)
    return kwargs, forward, reverse