Beispiel #1
0
    def __init__(
        self,
        query: QueryDict,
        params: Mapping[Any, Any],
        allow_minute_resolution: Optional[bool] = True,
    ):
        raw_fields = query.getlist("field", [])
        raw_groupby = query.getlist("groupBy", [])
        if len(raw_fields) == 0:
            raise InvalidField('At least one "field" is required.')
        self.fields = {}
        self.query: List[Any] = []  # not used but needed for compat with sessions logic
        allowed_resolution = (
            AllowedResolution.one_minute if allow_minute_resolution else AllowedResolution.one_hour
        )
        start, end, rollup = get_constrained_date_range(query, allowed_resolution)
        self.dataset, self.match = _outcomes_dataset(rollup)
        self.rollup = rollup
        self.start = start
        self.end = end
        self.select_params = []
        for key in raw_fields:
            if key not in COLUMN_MAP:
                raise InvalidField(f'Invalid field: "{key}"')
            field = COLUMN_MAP[key]
            self.select_params.append(field.select_params(self.dataset))
            self.fields[key] = field

        self.groupby = []
        for key in raw_groupby:
            if key not in GROUPBY_MAP:
                raise InvalidField(f'Invalid groupBy: "{key}"')
            self.groupby.append(GROUPBY_MAP[key])

        if len(query.getlist("category", [])) == 0 and "category" not in raw_groupby:
            raise InvalidQuery("Query must have category as groupby or filter")

        query_columns = set()
        for field in self.fields.values():
            query_columns.update(field.get_snuba_columns(raw_groupby))
        for groupby in self.groupby:
            query_columns.update(groupby.get_snuba_columns())
        self.query_columns = list(query_columns)

        query_groupby = set()
        for groupby in self.groupby:
            query_groupby.update(groupby.get_snuba_groupby())
        self.query_groupby = list(query_groupby)

        self.group_by = []
        for key in self.query_groupby:
            self.group_by.append(Column(key))

        self.conditions = self.get_conditions(query, params)
Beispiel #2
0
 def resolve_filter(self, raw_filter: Sequence[str]) -> List[DataCategory]:
     resolved_categories = set()
     for category in raw_filter:
         # combine DEFAULT, ERROR, and SECURITY as errors.
         # see relay: py/sentry_relay/consts.py and relay-cabi/include/relay.h
         parsed_category = DataCategory.parse(category)
         if parsed_category is None:
             raise InvalidField(f'Invalid category: "{category}"')
         elif parsed_category == DataCategory.ERROR:
             resolved_categories.update(DataCategory.error_categories())
         else:
             resolved_categories.add(parsed_category)
     if DataCategory.ATTACHMENT in resolved_categories and len(resolved_categories) > 1:
         raise InvalidQuery("if filtering by attachment no other category may be present")
     return list(resolved_categories)