Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter alerts #242

Merged
merged 9 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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