* remove cache usage in AlertGroupView * remove CustomSearchFilter * remove caching for alerts * remove readonly db setup * render templates on alert creation * serialize only necessary fields on alert groups list * optimize AlertGroupListSerializer * return on-demand templating for alerts * return on-demand templating for alert groups * use CursorPaginator * remove templating on alert create * pass alert to AlertGroupWebRenderer * alert_count -> alerts_count * make sql joins after pagination * add migration * bring alert.save() back * fix tests * fix tests * fix tests * add perpage query param * add cursor pagination to incidents page * remove cached_render_for_web usage * post merge fix * keep cursor * lint * remove get_alert_groups_and_days_for_previous_same_period * fix pagination on navigate * refine search_fields on AlertGroupView Co-authored-by: Maxim <hello.makson@gmail.com> Co-authored-by: Maxim <maxim.mordasov@grafana.com>
56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
import json
|
|
import re
|
|
|
|
from django.db.models import Prefetch
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from apps.alerts.incident_appearance.renderers.web_renderer import AlertWebRenderer
|
|
from apps.alerts.models import Alert, AlertGroup
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
from common.api_helpers.exceptions import BadRequest
|
|
|
|
|
|
class RouteRegexDebuggerView(APIView):
|
|
authentication_classes = (PluginAuthentication,)
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
def get(self, request):
|
|
organization = self.request.auth.organization
|
|
team = self.request.user.current_team
|
|
|
|
regex = request.query_params.get("regex", None)
|
|
|
|
if regex is None:
|
|
raise BadRequest(detail={"regex": ["This field is required."]})
|
|
if regex == "":
|
|
return Response([])
|
|
try:
|
|
re.compile(regex)
|
|
except re.error:
|
|
raise BadRequest(detail={"regex": ["Invalid regex."]})
|
|
|
|
incidents_matching_regex = []
|
|
MAX_INCIDENTS_TO_SHOW = 5
|
|
INCIDENTS_TO_LOOKUP = 100
|
|
for ag in (
|
|
AlertGroup.unarchived_objects.prefetch_related(Prefetch("alerts", queryset=Alert.objects.order_by("pk")))
|
|
.filter(channel__organization=organization, channel__team=team)
|
|
.order_by("-started_at")[:INCIDENTS_TO_LOOKUP]
|
|
):
|
|
|
|
if len(incidents_matching_regex) < MAX_INCIDENTS_TO_SHOW:
|
|
first_alert = ag.alerts.all()[0]
|
|
if re.search(regex, json.dumps(first_alert.raw_request_data)):
|
|
title = AlertWebRenderer(first_alert).render()["title"]
|
|
incidents_matching_regex.append(
|
|
{
|
|
"title": title,
|
|
"pk": ag.public_primary_key,
|
|
"payload": first_alert.raw_request_data,
|
|
"inside_organization_number": ag.inside_organization_number,
|
|
}
|
|
)
|
|
|
|
return Response(incidents_matching_regex)
|