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

[ENHANCEMENT] Show read-only view of incoming alerts #199

Merged
merged 1 commit into from
Nov 27, 2019
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
3 changes: 3 additions & 0 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ class Alert(models.Model):
created = models.DateTimeField(default=timezone.now)
body = models.TextField()

def get_absolute_url(self):
return reverse("alert-detail", kwargs={"pk": self.pk})

def expand(self):
# Map of Prometheus labels to Promgen objects
LABEL_MAPPING = [
Expand Down
7 changes: 7 additions & 0 deletions promgen/templates/promgen/alert_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}
{% load promgen %}
{% block content %}
{% breadcrumb alert %}

<pre>{{alert.json|pretty_json}}</pre>
{% endblock %}
30 changes: 30 additions & 0 deletions promgen/templates/promgen/alert_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "base.html" %}

{% block content %}

{% include 'promgen/pagination_short.html' %}

<table class="table">
<tr>
<th>Created</th>
<th>Alertname</th>
<th>Datasource</th>
<th>Service</th>
<th>Project</th>
<th>Job</th>
</tr>
{% for alert in alert_list %}
<tr>
<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>
</tr>
{% endfor %}
</table>

{% include 'promgen/pagination_short.html' %}

{% endblock %}
18 changes: 2 additions & 16 deletions promgen/templates/promgen/audit_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,8 @@ <h1>Log</h1>
</tr>
{% endfor %}
</table>

{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="{% url 'audit-list' %}?{% qsfilter request 'page' page_obj.previous_page_number %}">previous</a>
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="{% url 'audit-list' %}?{% qsfilter request 'page' page_obj.next_page_number %}">next</a>
{% endif %}
</span>
</div>
{% endif %}
</div>

{% include 'promgen/pagination_short.html' %}

{% endblock %}
22 changes: 22 additions & 0 deletions promgen/templates/promgen/pagination_short.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% load promgen %}
<nav aria-label="Page navigation">
{% if page_obj.has_other_pages %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}

<li class="active">
<span>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }} <span class="sr-only">(current)</span></span>
</li>

{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
{% endif %}
</nav>
6 changes: 6 additions & 0 deletions promgen/templatetags/promgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def project(obj):
yield from service(obj.service)
yield obj.get_absolute_url(), obj.name

def alert(obj):
yield reverse("alert-list"), _("Alerts")
yield obj.get_absolute_url(), obj.pk

def rule(obj):
if obj.content_type.model == "site":
yield reverse("rules-list"), _("Common Rules")
Expand Down Expand Up @@ -182,6 +186,8 @@ def generator():
yield from shard(instance)
if isinstance(instance, models.Rule):
yield from rule(instance)
if isinstance(instance, models.Alert):
yield from alert(instance)

def to_tag():
yield '<ol class="breadcrumb">'
Expand Down
4 changes: 3 additions & 1 deletion promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@
path('metrics', csrf_exempt(views.Metrics.as_view()), name='metrics'),
path('commit', csrf_exempt(views.Commit.as_view()), name='commit'),

path('alert', views.AlertList.as_view(), name='alert-list'),
path('alert/<int:pk>', views.AlertDetail.as_view(), name='alert-detail'),

url('', include('django.contrib.auth.urls')),
url('', include('social_django.urls', namespace='social')),

# Public API

# Legacy API
path('alert', csrf_exempt(views.Alert.as_view())),
path('api/v1/config', csrf_exempt(views.ApiConfig.as_view())),

# Promgen API
Expand Down
15 changes: 11 additions & 4 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,18 @@ def post(self, request, *args, **kwargs):
# writing to the database, but to keep the alert ingestion queue as simple as possible
# we will go ahead and write all alerts to the database and then filter out (delete)
# when we run tasks.process_alert
alert = models.Alert.objects.create(
body=request.body.decode('utf-8')
)
alert = models.Alert.objects.create(body=request.body.decode("utf-8"))
tasks.process_alert.delay(alert.pk)
return HttpResponse('OK', status=202)
return HttpResponse("OK", status=202)


class AlertList(LoginRequiredMixin, ListView):
paginate_by = 20
queryset = models.Alert.objects.order_by('-created')


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


class Metrics(View):
Expand Down