Skip to content

Commit

Permalink
[ENHANCEMENT] Allow filtering alerts on alert list page #242
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm authored Feb 26, 2020
2 parents 87ef8f2 + 7adf749 commit 6f104d4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
11 changes: 11 additions & 0 deletions promgen/management/commands/index-alerts.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions promgen/migrations/0016_alertlabel.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
4 changes: 4 additions & 0 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions promgen/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

logger = logging.getLogger(__name__)

@shared_task
def index_alert(alert_pk):
alert = models.Alert.objects.get(pk=alert_pk)
labels = alert.json.get("commonLabels")
for name, value in labels.items():
models.AlertLabel.objects.create(alert=alert, name=name, value=value)

@shared_task
def process_alert(alert_pk):
Expand Down
13 changes: 9 additions & 4 deletions promgen/templates/promgen/alert_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

{% breadcrumb label="Alerts" %}

{% include 'promgen/pagination_short.html' %}
<form class="navbar-form navbar-left" action="/alert">
<div class="form-group">
<input name="search" type="text" value="{{ request.GET.search }}" class="form-control" placeholder="Service, Project, Job">
<button type="submit" class="btn btn-default">Search</button>
</div>
</form>

<table class="table">
<tr>
Expand All @@ -22,9 +27,9 @@
<td><a href="{{alert.get_absolute_url}}">{{alert.created}}</a></td>
<td>{{alert.json.commonLabels.alertname}}</td>
<td>{{alert.json.commonLabels.datasource}}</td>
<td>{{alert.json.commonLabels.service}}</td>
<td>{{alert.json.commonLabels.project}}</td>
<td>{{alert.json.commonLabels.job}}</td>
<td><a href="?service={{alert.json.commonLabels.service}}">{{alert.json.commonLabels.service}}</a></td>
<td><a href="?project={{alert.json.commonLabels.project}}">{{alert.json.commonLabels.project}}</a></td>
<td><a href="?job={{alert.json.commonLabels.job}}">{{alert.json.commonLabels.job}}</a></td>
<td {% if alert.sent_count == 0 %}class="warning" {% endif %}>{{alert.sent_count}}</td>
<td {% if alert.error_count %}class="danger" {% endif %}>{{alert.error_count}}</td>
</tr>
Expand Down
18 changes: 17 additions & 1 deletion promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,29 @@ 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')
queryset = models.Alert.objects.order_by("-created")

def get_queryset(self):
search = self.request.GET.get('search')
if search:
return self.queryset.filter(
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 in ["page", "search"]:
continue
qs = qs.filter(alertlabel__name=key, alertlabel__value=value)
return qs

class AlertDetail(LoginRequiredMixin, DetailView):
model = models.Alert
Expand Down

0 comments on commit 6f104d4

Please sign in to comment.