Commit 2baf8daa authored by Élie Bouttier's avatar Élie Bouttier

status page

parent d4c97894
......@@ -52,6 +52,11 @@
<span class="fas fa-coins"></span>&nbsp;Transparence
</a>
</li>
<li class="nav-item{% block statustab %}{% endblock %}">
<a class="nav-link" href="{% url 'status' %}">
<span class="fas fa-bullhorn"></span>&nbsp;Status
</a>
</li>
</ul>
<ul class="navbar-nav">
{% if request.user.is_staff %}
......
{% extends 'base.html' %}
{% load humanize %}
{% block statustab %} active{% endblock %}
{% block content %}
<div class="list-group">
{% for route, last_time_up, allocs in status %}
<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><span class="badge badge-primary badge-pill">{{ allocs|length }} IPs</span> <b>{{ route }}</b></h5>
<small>{{ last_time_up|naturaltime }}</small>
</div>
<ul>
{% for alloc in allocs %}
<li>{{ alloc.resource }} — {{ alloc.service.adhesion }} – {{ alloc.service }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
{% endblock %}
......@@ -4,6 +4,7 @@ from . import views
urlpatterns = [
path('status/', views.Status.as_view(), name='status'),
path('services/<int:pk>/', views.ServiceDetail.as_view(), name='service-detail'),
path('api/fastpinger/', views.fastpinger, name='fastpinger'),
re_path(r'^api/routes/(?:(?P<route>[\w-]+)/)?$', views.routes, name='routes'), # TODO: path_re => path
......
from django.views.generic import DetailView
from django.views.generic import DetailView, TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.decorators.http import require_GET, require_POST
from django.http import HttpResponseForbidden, HttpResponse
......@@ -7,12 +7,41 @@ from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import get_object_or_404
from django.db import models
from django.db.models.functions import Concat
from django.utils import timezone
from itertools import groupby
from datetime import timedelta, datetime
from djadhere.utils import get_active_filter
from djadhere.decorators import api_key_required
from .models import Service, Route, IPResource, ServiceAllocation
from .utils.fastpinger import fastpinger_update
from .utils.architecture import export_switch, export_ip
from services.models import IPResource, IPResourceState, ServiceAllocation
class Status(LoginRequiredMixin, TemplateView):
template_name = 'services/status.html'
def get_status(self):
begin = timezone.make_aware(datetime(2019, 3, 1))
down = IPResource.objects.filter(last_state__state=IPResourceState.STATE_DOWN, last_state__date__gte=begin)
allocations = ServiceAllocation.objects.filter(get_active_filter())\
.filter(resource__ip__in=down.values('ip'))
events = []
threshold = getattr(settings, 'STATUS_THRESHOLD', 2)
allocations = sorted(allocations, reverse=True, key=lambda a: (a.resource.last_state.date, a.route.pk))
for (last_time_up, route), allocs in groupby(allocations, key=lambda a: (a.resource.last_state.date, a.route.pk)):
allocs = list(allocs)
route = allocs[0].route
if len(allocs) >= threshold:
events.append((route, last_time_up, allocs))
return events
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['status'] = self.get_status()
return context
class ServiceDetail(LoginRequiredMixin, DetailView):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment