From 3902403049f1d5b988cf9b0dda74f2bffa1fea2c Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Fri, 21 Feb 2020 14:30:13 +0900 Subject: [PATCH 1/9] Add a new model --- promgen/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/promgen/models.py b/promgen/models.py index 24744bbad..6363f24cb 100644 --- a/promgen/models.py +++ b/promgen/models.py @@ -498,6 +498,10 @@ class RuleAnnotation(models.Model): value = models.TextField() rule = models.ForeignKey('Rule', on_delete=models.CASCADE) +class AlertLabel(models.Model): + alert = models.ForeignKey('Alert', on_delete=models.CASCADE) + name = models.CharField(max_length=128) + value = models.CharField(max_length=128) class Alert(models.Model): created = models.DateTimeField(default=timezone.now) From 3f2ba280577479c38835b5cee2559e85105e247e Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Tue, 25 Feb 2020 18:28:22 +0900 Subject: [PATCH 2/9] Filter alerts --- promgen/migrations/0016_alertlabel.py | 23 ++++++++++++++++++ promgen/tasks.py | 7 ++++++ promgen/templates/promgen/alert_list.html | 29 +++++++++++++++++++++-- promgen/views.py | 17 ++++++++++++- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 promgen/migrations/0016_alertlabel.py diff --git a/promgen/migrations/0016_alertlabel.py b/promgen/migrations/0016_alertlabel.py new file mode 100644 index 000000000..21b61d83b --- /dev/null +++ b/promgen/migrations/0016_alertlabel.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.8 on 2020-02-21 06:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('promgen', '0015_internal'), + ] + + operations = [ + migrations.CreateModel( + name='AlertLabel', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128)), + ('value', models.CharField(max_length=128)), + ('alert', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='promgen.Alert')), + ], + ), + ] diff --git a/promgen/tasks.py b/promgen/tasks.py index 3f09c2c82..8ec2bdc48 100644 --- a/promgen/tasks.py +++ b/promgen/tasks.py @@ -3,6 +3,7 @@ import collections import logging import os +import json from urllib.parse import urljoin from atomicwrites import atomic_write @@ -12,6 +13,12 @@ logger = logging.getLogger(__name__) +@shared_task +def index_alert(alert_pk): + alert = models.Alert.objects.get(pk=alert_pk) + labels = json.loads(alert.body)["commonLabels"] + for name, value in labels.items(): + models.AlertLabel.objects.create(alert=alert, name=name, value=value) @shared_task def process_alert(alert_pk): diff --git a/promgen/templates/promgen/alert_list.html b/promgen/templates/promgen/alert_list.html index 4c6bf188d..2e9e2c875 100644 --- a/promgen/templates/promgen/alert_list.html +++ b/promgen/templates/promgen/alert_list.html @@ -4,7 +4,12 @@ {% breadcrumb label="Alerts" %} -{% include 'promgen/pagination_short.html' %} + @@ -31,6 +36,26 @@ {% endfor %}
-{% include 'promgen/pagination_short.html' %} + {% endblock %} diff --git a/promgen/views.py b/promgen/views.py index 582244fa7..c405bc8ba 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -1019,13 +1019,28 @@ def post(self, request, *args, **kwargs): # when we run tasks.process_alert alert = models.Alert.objects.create(body=request.body.decode("utf-8")) tasks.process_alert.delay(alert.pk) + tasks.index_alert.delay(alert.pk) return HttpResponse("OK", status=202) class AlertList(LoginRequiredMixin, ListView): paginate_by = 20 - queryset = models.Alert.objects.order_by('-created') + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + search = self.request.GET.get('search') + if search is not None: + context["search"] = search + return context + + def get_queryset(self): + search = self.request.GET.get('search') + if search: + return models.Alert.objects.filter( + Q(alertlabel__name="Service", alertlabel__value__contains=search) | + Q(alertlabel__name="Project", alertlabel__value__contains=search) + ).distinct().order_by('-created') + return models.Alert.objects.order_by('-created') class AlertDetail(LoginRequiredMixin, DetailView): model = models.Alert From c6d9658b59a4763a4c41c140dad99c6ee0e511b5 Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Tue, 25 Feb 2020 18:45:12 +0900 Subject: [PATCH 3/9] Fixed --- promgen/tasks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/promgen/tasks.py b/promgen/tasks.py index 8ec2bdc48..ef3b9a796 100644 --- a/promgen/tasks.py +++ b/promgen/tasks.py @@ -3,7 +3,6 @@ import collections import logging import os -import json from urllib.parse import urljoin from atomicwrites import atomic_write @@ -16,7 +15,7 @@ @shared_task def index_alert(alert_pk): alert = models.Alert.objects.get(pk=alert_pk) - labels = json.loads(alert.body)["commonLabels"] + labels = alert.json.get("commonLabels") for name, value in labels.items(): models.AlertLabel.objects.create(alert=alert, name=name, value=value) From 1467399aff298d8d0fd46f85ef108595791499ee Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 11:04:46 +0900 Subject: [PATCH 4/9] Use pagination_short --- promgen/templates/promgen/alert_list.html | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/promgen/templates/promgen/alert_list.html b/promgen/templates/promgen/alert_list.html index 2e9e2c875..4b31fe9cd 100644 --- a/promgen/templates/promgen/alert_list.html +++ b/promgen/templates/promgen/alert_list.html @@ -36,26 +36,6 @@ {% endfor %} - +{% include 'promgen/pagination_short.html' %} {% endblock %} From afa3659794d284c9522528a109ab8a8b049abd2e Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 11:11:08 +0900 Subject: [PATCH 5/9] Remove get_context_data --- promgen/templates/promgen/alert_list.html | 2 +- promgen/views.py | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/promgen/templates/promgen/alert_list.html b/promgen/templates/promgen/alert_list.html index 4b31fe9cd..b1ce9a0b8 100644 --- a/promgen/templates/promgen/alert_list.html +++ b/promgen/templates/promgen/alert_list.html @@ -6,7 +6,7 @@ diff --git a/promgen/views.py b/promgen/views.py index c405bc8ba..2bd6769d2 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -1026,13 +1026,6 @@ def post(self, request, *args, **kwargs): class AlertList(LoginRequiredMixin, ListView): paginate_by = 20 - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - search = self.request.GET.get('search') - if search is not None: - context["search"] = search - return context - def get_queryset(self): search = self.request.GET.get('search') if search: From 85fbd6b9b25fc055ab665df0c9e23b20c9fbe064 Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 11:13:47 +0900 Subject: [PATCH 6/9] Use queryset --- promgen/views.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/promgen/views.py b/promgen/views.py index 2bd6769d2..53078ffca 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -1025,15 +1025,16 @@ def post(self, request, *args, **kwargs): class AlertList(LoginRequiredMixin, ListView): paginate_by = 20 + queryset = models.Alert.objects.order_by("-created") def get_queryset(self): search = self.request.GET.get('search') if search: - return models.Alert.objects.filter( - Q(alertlabel__name="Service", alertlabel__value__contains=search) | - Q(alertlabel__name="Project", alertlabel__value__contains=search) - ).distinct().order_by('-created') - return models.Alert.objects.order_by('-created') + return self.queryset.filter( + Q(alertlabel__name="Service", alertlabel__value__contains=search) + | Q(alertlabel__name="Project", alertlabel__value__contains=search) + ) + return self.queryset class AlertDetail(LoginRequiredMixin, DetailView): model = models.Alert From 9a1307e06f0a3a96bb478cf26af78ef1c17be5ca Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 12:55:51 +0900 Subject: [PATCH 7/9] Add a command for indexalert --- promgen/management/commands/index-alerts.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 promgen/management/commands/index-alerts.py diff --git a/promgen/management/commands/index-alerts.py b/promgen/management/commands/index-alerts.py new file mode 100644 index 000000000..0b9fef3d8 --- /dev/null +++ b/promgen/management/commands/index-alerts.py @@ -0,0 +1,11 @@ +# Copyright (c) 2020 LINE Corporation +# These sources are released under the terms of the MIT license: see LICENSE + +from django.core.management.base import BaseCommand +from promgen import models, tasks + + +class Command(BaseCommand): + def handle(self, **kargs): + for alert in models.Alert.objects.filter(alertlabel__isnull=True): + tasks.index_alert.delay(alert.pk) From 532e315e20767fff60c2614fc7b9b1f5a472e551 Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 15:50:36 +0900 Subject: [PATCH 8/9] Add links --- promgen/templates/promgen/alert_list.html | 8 ++++---- promgen/views.py | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/promgen/templates/promgen/alert_list.html b/promgen/templates/promgen/alert_list.html index b1ce9a0b8..4963767f3 100644 --- a/promgen/templates/promgen/alert_list.html +++ b/promgen/templates/promgen/alert_list.html @@ -6,7 +6,7 @@ @@ -27,9 +27,9 @@ {{alert.created}} {{alert.json.commonLabels.alertname}} {{alert.json.commonLabels.datasource}} - {{alert.json.commonLabels.service}} - {{alert.json.commonLabels.project}} - {{alert.json.commonLabels.job}} + {{alert.json.commonLabels.service}} + {{alert.json.commonLabels.project}} + {{alert.json.commonLabels.job}} {{alert.sent_count}} {{alert.error_count}} diff --git a/promgen/views.py b/promgen/views.py index 53078ffca..ea1091624 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -1033,8 +1033,15 @@ def get_queryset(self): return self.queryset.filter( Q(alertlabel__name="Service", alertlabel__value__contains=search) | Q(alertlabel__name="Project", alertlabel__value__contains=search) + | Q(alertlabel__name="Job", alertlabel__value__contains=search) ) - return self.queryset + + qs = self.queryset + for key, value in self.request.GET.items(): + if key == "page" or key == "search": + continue + qs = qs.filter(alertlabel__name=key, alertlabel__value=value) + return qs class AlertDetail(LoginRequiredMixin, DetailView): model = models.Alert From 7adf749e538722490cd27d162bc4c49f73573468 Mon Sep 17 00:00:00 2001 From: Sho Okada Date: Wed, 26 Feb 2020 15:58:46 +0900 Subject: [PATCH 9/9] fix --- promgen/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/promgen/views.py b/promgen/views.py index ea1091624..ae9a46069 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -1031,14 +1031,14 @@ def get_queryset(self): search = self.request.GET.get('search') if search: return self.queryset.filter( - Q(alertlabel__name="Service", alertlabel__value__contains=search) - | Q(alertlabel__name="Project", alertlabel__value__contains=search) - | Q(alertlabel__name="Job", alertlabel__value__contains=search) + Q(alertlabel__name="Service", alertlabel__value__icontains=search) + | Q(alertlabel__name="Project", alertlabel__value__icontains=search) + | Q(alertlabel__name="Job", alertlabel__value__icontains=search) ) qs = self.queryset for key, value in self.request.GET.items(): - if key == "page" or key == "search": + if key in ["page", "search"]: continue qs = qs.filter(alertlabel__name=key, alertlabel__value=value) return qs