From 839b8d420d122e76e6dcedb4b17b557d586c600a Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Mon, 11 Dec 2017 15:07:01 +0100 Subject: [PATCH] instance view: added logs section to graphs (renamed graphs -> stats) --- instances/templates/instance.html | 41 +++++++++++++++++++++++++++++-- logs/urls.py | 1 + logs/views.py | 33 ++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 1579da2..f548b02 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -84,8 +84,8 @@
  • - - {% trans "Graphs" %} + + {% trans "Stats" %}
  • @@ -1043,6 +1043,11 @@ {% trans "Real Time" %}
  • +
  • + + {% trans "Logs" %} + +
  • @@ -1089,6 +1094,23 @@ {% endfor %}
    +
    +
    + + + + + + + + + + + +
    {% trans "Date" %}{% trans "User" %}{% trans "Message" %}
    None ...
    +
    +
    +
    @@ -1467,4 +1489,19 @@ }); } + {% endblock %} diff --git a/logs/urls.py b/logs/urls.py index e579387..574405c 100644 --- a/logs/urls.py +++ b/logs/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ url(r'^$', views.showlogs, name='showlogs'), url(r'^(?P[0-9]+)/$', views.showlogs, name='showlogspage'), + url(r'^vm_logs/(?P[\w\-\.]+)/$', views.vm_logs, name='vm_logs'), ] diff --git a/logs/views.py b/logs/views.py index 830925e..ab6ae1b 100644 --- a/logs/views.py +++ b/logs/views.py @@ -1,8 +1,11 @@ from django.shortcuts import render -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required +from instances.models import Instance from logs.models import Logs from django.conf import settings +import json def addlogmsg(user, instance, message): @@ -14,15 +17,13 @@ def addlogmsg(user, instance, message): add_log_msg.save() +@login_required def showlogs(request, page=1): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -34,3 +35,27 @@ def showlogs(request, page=1): # TODO: remove last element from queryset, but do not affect database return render(request, 'showlogs.html', locals()) + +@login_required +def vm_logs(request, vname): + """ + :param request: + :param vm: + :return: + """ + + if not request.user.is_superuser: + return HttpResponseRedirect(reverse('index')) + + vm = Instance.objects.get(name=vname) + logs_ = Logs.objects.filter(instance=vm.name, date__gte=vm.created).order_by('-date') + logs = [] + for l in logs_: + log = {} + log['user'] = l.user + log['instance'] = l.instance + log['message'] = l.message + log['date'] = l.date.strftime('%x %X') + logs.append(log) + + return HttpResponse(json.dumps(logs))