Example #1
0
 def do_post_processing(
     self,
     project_ids: Sequence[int],
     query: Query,
     request_settings: RequestSettings,
 ) -> None:
     if not request_settings.get_turbo():
         final, exclude_group_ids = get_projects_query_flags(
             project_ids, self.__replacer_state_name)
         if not final and exclude_group_ids:
             # If the number of groups to exclude exceeds our limit, the query
             # should just use final instead of the exclusion set.
             max_group_ids_exclude = get_config(
                 "max_group_ids_exclude",
                 settings.REPLACER_MAX_GROUP_IDS_TO_EXCLUDE)
             if len(exclude_group_ids) > max_group_ids_exclude:
                 query.set_final(True)
             else:
                 query.add_conditions([(["assumeNotNull", ["group_id"]],
                                        "NOT IN", exclude_group_ids)])
                 query.add_condition_to_ast(
                     not_in_condition(
                         None,
                         FunctionCall(None, "assumeNotNull",
                                      (Column(None, "group_id", None), )),
                         [Literal(None, p) for p in exclude_group_ids],
                     ))
         else:
             query.set_final(final)
Example #2
0
    def test_query_time_flags(self):
        project_ids = [1, 2]

        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.ERRORS
        ) == (False, [],)

        errors_replacer.set_project_needs_final(100, ReplacerState.ERRORS)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.ERRORS
        ) == (False, [],)

        errors_replacer.set_project_needs_final(1, ReplacerState.ERRORS)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.ERRORS
        ) == (True, [],)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.EVENTS
        ) == (False, [],)

        errors_replacer.set_project_needs_final(2, ReplacerState.ERRORS)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.ERRORS
        ) == (True, [],)

        errors_replacer.set_project_exclude_groups(1, [1, 2], ReplacerState.ERRORS)
        errors_replacer.set_project_exclude_groups(2, [3, 4], ReplacerState.ERRORS)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.ERRORS
        ) == (True, [1, 2, 3, 4],)
        assert errors_replacer.get_projects_query_flags(
            project_ids, ReplacerState.EVENTS
        ) == (False, [],)
Example #3
0
    def process_query(self, query: Query,
                      request_settings: RequestSettings) -> None:
        if request_settings.get_turbo():
            return

        project_ids = get_project_ids_in_query_ast(query,
                                                   self.__project_column)

        set_final = False
        condition_to_add = None
        if project_ids:
            final, exclude_group_ids = get_projects_query_flags(
                list(project_ids),
                self.__replacer_state_name,
            )
            if final:
                metrics.increment("final", tags={"cause": "final_flag"})
            if not final and exclude_group_ids:
                # If the number of groups to exclude exceeds our limit, the query
                # should just use final instead of the exclusion set.
                max_group_ids_exclude = get_config(
                    "max_group_ids_exclude",
                    settings.REPLACER_MAX_GROUP_IDS_TO_EXCLUDE)
                if len(exclude_group_ids) > max_group_ids_exclude:
                    metrics.increment("final", tags={"cause": "max_groups"})
                    set_final = True
                else:
                    condition_to_add = (
                        ["assumeNotNull", ["group_id"]],
                        "NOT IN",
                        exclude_group_ids,
                    )
                    query.add_condition_to_ast(
                        not_in_condition(
                            None,
                            FunctionCall(None, "assumeNotNull",
                                         (Column(None, None, "group_id"), )),
                            [Literal(None, p) for p in exclude_group_ids],
                        ))
            else:
                set_final = final

        query.set_final(set_final)
        if condition_to_add:
            query.add_conditions([condition_to_add])