From 77bb201d8da45e17e2a0aecfb76b581a5cd450c5 Mon Sep 17 00:00:00 2001 From: Grizzly Date: Sun, 16 Dec 2018 17:34:57 +0000 Subject: [PATCH] =?UTF-8?q?Second=20graphique,=20changement=20de=20fichier?= =?UTF-8?q?=20templates=20pour=20suivre=20les=20autres=20cat=C3=A9gories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logs/charts.py | 50 +++++++++++++++++++++++++++ logs/templates/logs/aff_charts.html | 7 +++- logs/templates/logs/sidebar.html | 2 +- logs/templates/logs/stats_charts.html | 37 ++++++++++++++++++++ logs/views.py | 8 +++-- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 logs/charts.py create mode 100644 logs/templates/logs/stats_charts.html diff --git a/logs/charts.py b/logs/charts.py new file mode 100644 index 00000000..58dadea8 --- /dev/null +++ b/logs/charts.py @@ -0,0 +1,50 @@ +from jchart import Chart +from jchart.config import Axes + +from users.models import User, Adherent +from re2o.utils import all_adherent + +from datetime import timedelta +from django.utils import timezone +from django.db.models import Count + +class ActiveUserChart(Chart): + """Create the hart of all active users for the 3 last months""" + chart_type = 'line' + scales = {'xAxes': [Axes(type='time', position='bottom')]} + + + def get_datasets(self, **kwargs): + data=[] + for i in range(100): + d = timedelta(days=i) + date = timezone.now()-d + data.append({'x':date,'y':all_adherent(search_time=date).count()}) + + return [{ + 'type': 'line', + 'label': "Nombre d'utilisateur actifs", + 'data': data + }] + +class MachinePerUserChart(Chart): + """Create the chart displaying the number of machines per users + for the 20 firsts users""" + + qs = User.objects.annotate(num=Count('machine')).order_by('-num')[:20] + + chart_type = 'bar' + scales = {'xAxes': [Axes(type='category', position='bottom',)]} + + def get_labels(self, **kwargs): + qs = User.objects.annotate(num=Count('machine')).order_by('-num')[:20] + return [u.name for u in qs] + + def get_datasets(self, **kwargs): + qs = User.objects.annotate(num=Count('machine')).order_by('-num')[:20] + data=[u.num for u in qs] + + return [{ + 'label': "Nombre de machines par utilisateurs", + 'data': data + }] diff --git a/logs/templates/logs/aff_charts.html b/logs/templates/logs/aff_charts.html index eeb29639..aea306e4 100644 --- a/logs/templates/logs/aff_charts.html +++ b/logs/templates/logs/aff_charts.html @@ -24,4 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc., -{{ ActiveUser.as_html }} + +{% for chart in charts_list %} + +{{ chart.as_html }} + +{% endfor %} diff --git a/logs/templates/logs/sidebar.html b/logs/templates/logs/sidebar.html index f168756e..685a4a19 100644 --- a/logs/templates/logs/sidebar.html +++ b/logs/templates/logs/sidebar.html @@ -53,7 +53,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Users" %} - + {% trans "Charts" %} {% acl_end %} diff --git a/logs/templates/logs/stats_charts.html b/logs/templates/logs/stats_charts.html new file mode 100644 index 00000000..a6b23699 --- /dev/null +++ b/logs/templates/logs/stats_charts.html @@ -0,0 +1,37 @@ +{% extends "logs/sidebar.html" %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load i18n %} + +{% block title %}{% trans "Statistics" %}{% endblock %} + +{% block content %} +

{% trans "Graphiques" %}

+ {% include "logs/aff_charts.html" with charts_list=charts_list %} +
+
+
+{% endblock %} diff --git a/logs/views.py b/logs/views.py index 38562d1c..b41da926 100644 --- a/logs/views.py +++ b/logs/views.py @@ -53,6 +53,7 @@ from reversion.models import Version, ContentType from .charts import ( ActiveUserChart, + MachinePerUserChart, ) from users.models import ( User, @@ -541,9 +542,12 @@ def charts(request): """Sert les graphiques des statistiques""" ActiveUser = ActiveUserChart() + MachinePerUser = MachinePerUserChart() + + chart_list=[ActiveUser,MachinePerUser] return render( request, - 'logs/aff_charts.html', - {'ActiveUser': ActiveUser} + 'logs/stats_charts.html', + {'charts_list': chart_list} )