def get(self, request, project):
        """
        Fetches alert rules and legacy rules for a project
        """
        alert_rules = AlertRule.objects.fetch_for_project(project)
        if not features.has("organizations:performance-view", project.organization):
            # Filter to only error alert rules
            alert_rules = alert_rules.filter(snuba_query__dataset=Dataset.Events.value)

        alert_rule_intermediary = CombinedQuerysetIntermediary(alert_rules, "date_added")
        rule_intermediary = CombinedQuerysetIntermediary(
            Rule.objects.filter(
                project=project, status__in=[RuleStatus.ACTIVE, RuleStatus.INACTIVE]
            ),
            "date_added",
        )

        return self.paginate(
            request,
            paginator_cls=CombinedQuerysetPaginator,
            on_results=lambda x: serialize(x, request.user, CombinedRuleSerializer()),
            default_per_page=25,
            intermediaries=[alert_rule_intermediary, rule_intermediary],
            desc=True,
        )
Beispiel #2
0
    def get(self, request, organization):
        """
        Fetches alert rules and legacy rules for an organization
        """
        project_ids = self.get_requested_project_ids(request) or None

        alert_rules = AlertRule.objects.fetch_for_organization(organization, project_ids)
        if not features.has("organizations:performance-view", organization):
            # Filter to only error alert rules
            alert_rules = alert_rules.filter(snuba_query__dataset=Dataset.Events.value)

        if project_ids is None:
            project_ids = Project.objects.filter(organization=organization).values_list(
                "id", flat=True
            )

        issue_rules = Rule.objects.filter(
            status__in=[RuleStatus.ACTIVE, RuleStatus.INACTIVE], project__in=project_ids
        )

        is_asc = request.GET.get("asc", False)
        sort_key = request.GET.get("sort", "date_added")
        rule_sort_key = (
            sort_key if sort_key != "name" else "label"
        )  # Rule's don't share the same field name for their title/label/name...so we account for that here.
        alert_rule_intermediary = CombinedQuerysetIntermediary(alert_rules, sort_key)
        rule_intermediary = CombinedQuerysetIntermediary(issue_rules, rule_sort_key)
        return self.paginate(
            request,
            paginator_cls=CombinedQuerysetPaginator,
            on_results=lambda x: serialize(x, request.user, CombinedRuleSerializer()),
            default_per_page=25,
            intermediaries=[alert_rule_intermediary, rule_intermediary],
            desc=not is_asc,
        )
Beispiel #3
0
    def get(self, request, organization):
        """
        Fetches alert rules and legacy rules for an organization
        """
        project_ids = self.get_requested_project_ids(request) or None

        alert_rules = AlertRule.objects.fetch_for_organization(organization, project_ids)
        if not features.has("organizations:performance-view", organization):
            # Filter to only error alert rules
            alert_rules = alert_rules.filter(snuba_query__dataset=Dataset.Events.value)

        if project_ids is None:
            project_ids = Project.objects.filter(organization=organization).values_list(
                "id", flat=True
            )

        issue_rules = Rule.objects.filter(
            status__in=[RuleStatus.ACTIVE, RuleStatus.INACTIVE], project__in=project_ids
        )
        return self.paginate(
            request,
            paginator_cls=CombinedQuerysetPaginator,
            on_results=lambda x: serialize(x, request.user, CombinedRuleSerializer()),
            default_per_page=25,
            order_by="-date_added",
            querysets=[alert_rules, issue_rules],
        )
    def get(self, request, project):
        """
        Fetches alert rules and legacy rules for an organization
        """
        if not features.has("organizations:incidents",
                            project.organization,
                            actor=request.user):
            raise ResourceDoesNotExist

        cursor_string = request.GET.get(
            "cursor",
            six.binary_type(int(time.time() * 1000000)) + ":0:0")
        try:
            limit = min(100, int(request.GET.get("limit", 25)))
        except ValueError as e:
            return Response(
                {
                    "detail":
                    "Invalid input for `limit`. Error: %s" % six.text_type(e)
                },
                status=400)

        cursor = Cursor.from_string(cursor_string)
        cursor_date = datetime.fromtimestamp(
            float(cursor.value) / 1000000).replace(tzinfo=timezone.utc)

        alert_rule_queryset = (
            AlertRule.objects.fetch_for_project(project).filter(
                date_added__lte=cursor_date).order_by("-date_added")[:limit +
                                                                     1])

        legacy_rule_queryset = (Rule.objects.filter(
            project=project,
            status__in=[
                RuleStatus.ACTIVE, RuleStatus.INACTIVE
            ]).select_related("project").filter(
                date_added__lte=cursor_date).order_by("-date_added")[:(limit +
                                                                       1)])
        combined_rules = list(alert_rule_queryset) + list(legacy_rule_queryset)
        combined_rules.sort(key=lambda instance:
                            (instance.date_added, type(instance)),
                            reverse=True)
        combined_rules = combined_rules[cursor.offset:cursor.offset + limit +
                                        1]

        def get_item_key(item, for_prev=False):
            return 1000000 * float(item.date_added.strftime("%s.%f"))

        cursor_result = build_cursor(results=combined_rules,
                                     cursor=cursor,
                                     key=get_item_key,
                                     limit=limit,
                                     is_desc=True)
        results = list(cursor_result)
        context = serialize(results, request.user, CombinedRuleSerializer())
        response = Response(context)
        self.add_cursor_headers(request, response, cursor_result)
        return response
 def get(self, request, project):
     """
     Fetches alert rules and legacy rules for an organization
     """
     return self.paginate(
         request,
         paginator_cls=CombinedQuerysetPaginator,
         on_results=lambda x: serialize(x, request.user,
                                        CombinedRuleSerializer()),
         default_per_page=25,
         order_by="-date_added",
         querysets=[
             AlertRule.objects.fetch_for_project(project),
             Rule.objects.filter(
                 project=project,
                 status__in=[RuleStatus.ACTIVE, RuleStatus.INACTIVE]),
         ],
     )
Beispiel #6
0
    def get(self, request, organization):
        """
        Fetches alert rules and legacy rules for an organization
        """
        project_ids = self.get_requested_project_ids(request) or None
        if project_ids == {-1}:  # All projects for org:
            project_ids = Project.objects.filter(
                organization=organization).values_list("id", flat=True)
        elif project_ids is None:  # All projects for user
            org_team_list = Team.objects.filter(
                organization=organization).values_list("id", flat=True)
            user_team_list = OrganizationMemberTeam.objects.filter(
                organizationmember__user=request.user,
                team__in=org_team_list).values_list("team", flat=True)
            project_ids = Project.objects.filter(
                teams__in=user_team_list).values_list("id", flat=True)

        teams = set(request.GET.getlist("team", []))
        team_filter_query = None
        if teams:
            # do normal teams lookup based on request params
            verified_ids = set()
            unassigned = None
            if "unassigned" in teams:
                teams.remove("unassigned")
                unassigned = Q(owner_id=None)

            if "myteams" in teams:
                teams.remove("myteams")
                if is_active_superuser(request):
                    # retrieve all teams within the organization
                    myteams = Team.objects.filter(
                        organization=organization,
                        status=TeamStatus.VISIBLE).values_list("id", flat=True)
                    verified_ids.update(myteams)
                else:
                    myteams = [t.id for t in request.access.teams]
                    verified_ids.update(myteams)

            for team_id in teams:  # Verify each passed Team id is numeric
                if type(team_id) is not int and not team_id.isdigit():
                    return Response(f"Invalid Team ID: {team_id}",
                                    status=status.HTTP_400_BAD_REQUEST)
            teams.update(verified_ids)

            teams = Team.objects.filter(id__in=teams)
            for team in teams:
                if team.id in verified_ids:
                    continue

                if not request.access.has_team_access(team):
                    return Response(
                        f"Error: You do not have permission to access {team.name}",
                        status=status.HTTP_400_BAD_REQUEST,
                    )
            team_filter_query = Q(
                owner_id__in=teams.values_list("actor_id", flat=True))
            if unassigned:
                team_filter_query = team_filter_query | unassigned

        alert_rules = AlertRule.objects.fetch_for_organization(
            organization, project_ids)
        if not features.has("organizations:performance-view", organization):
            # Filter to only error alert rules
            alert_rules = alert_rules.filter(
                snuba_query__dataset=Dataset.Events.value)
        issue_rules = Rule.objects.filter(
            status__in=[RuleStatus.ACTIVE, RuleStatus.INACTIVE],
            project__in=project_ids)
        name = request.GET.get("name", None)
        if name:
            alert_rules = alert_rules.filter(Q(name__icontains=name))
            issue_rules = issue_rules.filter(Q(label__icontains=name))

        if team_filter_query:
            alert_rules = alert_rules.filter(team_filter_query)
            issue_rules = issue_rules.filter(team_filter_query)

        is_asc = request.GET.get("asc", False) == "1"
        sort_key = request.GET.get("sort", "date_added")
        rule_sort_key = (
            sort_key if sort_key != "name" else "label"
        )  # Rule's don't share the same field name for their title/label/name...so we account for that here.
        case_insensitive = sort_key == "name"
        alert_rule_intermediary = CombinedQuerysetIntermediary(
            alert_rules, sort_key)
        rule_intermediary = CombinedQuerysetIntermediary(
            issue_rules, rule_sort_key)
        return self.paginate(
            request,
            paginator_cls=CombinedQuerysetPaginator,
            on_results=lambda x: serialize(x, request.user,
                                           CombinedRuleSerializer()),
            default_per_page=25,
            intermediaries=[alert_rule_intermediary, rule_intermediary],
            desc=not is_asc,
            cursor_cls=StringCursor if case_insensitive else Cursor,
            case_insensitive=case_insensitive,
        )