Ajout d'un monitoring des activités

This commit is contained in:
Jean-Marie Favreau 2024-11-11 14:05:47 +01:00
parent 67c65f14d1
commit 2196083894
8 changed files with 127 additions and 3 deletions

View File

@ -12,7 +12,7 @@ import os
from django.core.files import File from django.core.files import File
from django.utils import timezone from django.utils import timezone
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
from django.db.models import Q, Count, F from django.db.models import Q, Count, F, Subquery, OuterRef, Func
import recurrence.fields import recurrence.fields
import recurrence import recurrence
import copy import copy
@ -1420,6 +1420,24 @@ class Event(models.Model):
return cal return cal
def get_count_modification(when):
start = datetime(when[0].year, when[0].month, when[0].day)
end = start + timedelta(days=when[1])
recurrentimport = RecurrentImport.objects.filter(source=OuterRef("import_sources__0")).order_by().annotate(count=Func(F('id'), function='Count')).values('count')
nb_manual_creation = Event.objects.filter(created_date__gte=start, created_date__lt=end, imported_date__isnull=True).count()
imported = Event.objects.filter(created_date__gte=start, created_date__lt=end, imported_date__isnull=False, import_sources__len__gt=0).annotate(nb_rimport=Subquery(recurrentimport))
nb_manual_import = imported.filter(nb_rimport=0).count()
nb_first_import = imported.filter(nb_rimport__gt=0).count()
return {"when": (start, start + timedelta(days=when[1] - 1)), "nb_manual_creation": nb_manual_creation, "nb_manual_import": nb_manual_import, "nb_first_import": nb_first_import}
def get_count_modifications(when_list):
return [Event.get_count_modification(w) for w in when_list]
class ContactMessage(models.Model): class ContactMessage(models.Model):
class Meta: class Meta:

View File

@ -18,7 +18,7 @@
<tbody> <tbody>
{% for obj in paginator_filter %} {% for obj in paginator_filter %}
<tr> <tr>
<td>{{ obj.id }}</a></td> <td>{{ obj.id }}</td>
<td>{{ obj.created_date }}</td> <td>{{ obj.created_date }}</td>
<td>{% if obj.recurrentImport %}<a href="{{ obj.recurrentImport.get_absolute_url }}">{{ obj.recurrentImport.name }}</a>{% else %} <td>{% if obj.recurrentImport %}<a href="{{ obj.recurrentImport.get_absolute_url }}">{{ obj.recurrentImport.name }}</a>{% else %}
{% if obj.url_source %} {% if obj.url_source %}

View File

@ -30,6 +30,18 @@
</div> </div>
<div style="clear: both"></div> <div style="clear: both"></div>
{% endif %} {% endif %}
{% if daily_modifications %}
<article>
<div class="slide-buttons">
<a href="{% url 'activite' %}" role="button">Voir l'historique {% picto_from_name "chevron-right" %}</a>
</div>
<h2>Activité des derniers jours</h2>
{% include "agenda_culturel/recent-activity-inc.html" with modifications=daily_modifications %}
</article>
{% endif %}
{% include 'agenda_culturel/paginator_filter.html' %}
</header> </header>
<div> <div>

View File

@ -0,0 +1,39 @@
{% extends "agenda_culturel/page-admin.html" %}
{% load utils_extra %}
{% block title %}{% block og_title %}Activité récente{% endblock %}{% endblock %}
{% load cat_extra %}
{% block entete_header %}
{% css_categories %}
{% endblock %}
{% block sidemenu-bouton %}
<li><a href="#contenu-principal" aria-label="Aller au contenu">{% picto_from_name "chevron-up" %}</a></li>
<li><a href="#sidebar" aria-label="Aller au menu latéral">{% picto_from_name "chevron-down" %}</a></li>
{% endblock %}
{% block content %}
<div class="grid two-columns">
<article id="contenu-principal">
<header>
<h1>Activité récente</h1>
</header>
<article>
<h2>Activité des {{ daily_modifications|length }} derniers jours</h2>
{% include "agenda_culturel/recent-activity-inc.html" with modifications=daily_modifications %}
</article>
<article>
<h2>Activité des semaines précédentes</h2>
{% include "agenda_culturel/recent-activity-inc.html" with modifications=weekly_modifications %}
</article>
</article>
{% include "agenda_culturel/side-nav.html" with current="activite" %}
</div>
{% endblock %}

View File

@ -0,0 +1,24 @@
{% if modifications|length == 0 %}
<em>Aucune activité dans la période choisie</em>
{% else %}
<table role="grid">
<thead>
<tr>
<th>{% if modifications.0.when.0 == modifications.0.when.1 %}Date{% else %}Semaine{% endif %}</th>
<th>Créations manuelles</th>
<th>Imports manuels</th>
<th>Imports automatiques</th>
</tr>
</thead>
<tbody>
{% for m in modifications %}
<tr>
<td>{{ m.when.0|date:'D j b' }}{% if modifications.0.when.0 != modifications.0.when.1 %} - {{ m.when.1|date:'D j b' }}{% endif %}</td>
<td>{{ m.nb_manual_creation }}</td>
<td>{{ m.nb_manual_import }}</td>
<td>{{ m.nb_first_import }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}

View File

@ -15,6 +15,7 @@
{% if perms.agenda_culturel.change_duplicatedevents %} {% if perms.agenda_culturel.change_duplicatedevents %}
<li><a {% if current == "duplicates" %}class="selected" {% endif %}href="{% url 'duplicates' %}">Gestion des doublons</a>{% show_badge_duplicated "left" %}</li> <li><a {% if current == "duplicates" %}class="selected" {% endif %}href="{% url 'duplicates' %}">Gestion des doublons</a>{% show_badge_duplicated "left" %}</li>
{% endif %} {% endif %}
<li><a {% if current == "activite" %}class="selected" {% endif %}href="{% url 'activite' %}">Résumé des activités</a></li>
</ul> </ul>
</nav> </nav>
{% if perms.agenda_culturel.change_place %} {% if perms.agenda_culturel.change_place %}

View File

@ -24,6 +24,7 @@ urlpatterns = [
path("tag/<int:pk>/delete", TagDeleteView.as_view(), name="delete_tag"), path("tag/<int:pk>/delete", TagDeleteView.as_view(), name="delete_tag"),
path("tags/add", TagCreateView.as_view(), name="add_tag"), path("tags/add", TagCreateView.as_view(), name="add_tag"),
path("moderation/", moderation, name="moderation"), path("moderation/", moderation, name="moderation"),
path("activite/", activite, name="activite"),
path( path(
"event/<int:year>/<int:month>/<int:day>/<int:pk>-<extra>", "event/<int:year>/<int:month>/<int:day>/<int:pk>-<extra>",
EventDetailView.as_view(), EventDetailView.as_view(),

View File

@ -991,6 +991,29 @@ class ContactMessagesFilterAdmin(django_filters.FilterSet):
fields = ["closed", "spam"] fields = ["closed", "spam"]
@login_required(login_url="/accounts/login/")
@permission_required("agenda_culturel.view_event")
def activite(request):
now = date.today()
days = [now]
while len(days) < 7 or days[-1].weekday() != 0:
days.append(days[-1] + timedelta(days=-1))
weeks = [days[-1]]
for w in range(0, 8):
weeks.append(weeks[-1] + timedelta(days=-7))
daily_modifications = Event.get_count_modifications([(d, 1) for d in days])
weekly_modifications = Event.get_count_modifications([(w, 7) for w in weeks])
return render(
request,
"agenda_culturel/page-activity.html",
{"daily_modifications": daily_modifications, "weekly_modifications": weekly_modifications },
)
@login_required(login_url="/accounts/login/") @login_required(login_url="/accounts/login/")
@permission_required("agenda_culturel.view_event") @permission_required("agenda_culturel.view_event")
def moderation(request): def moderation(request):
@ -1000,6 +1023,12 @@ def moderation(request):
paginator = PaginatorFilter(filter, 10, request) paginator = PaginatorFilter(filter, 10, request)
page = request.GET.get("page") page = request.GET.get("page")
daily_modifications = []
if not page or page == 1:
days = [date.today()]
for i in range(0, 2):
days.append(days[-1] + timedelta(days=-1))
daily_modifications = Event.get_count_modifications([(d, 1) for d in days])
try: try:
response = paginator.page(page) response = paginator.page(page)
except PageNotAnInteger: except PageNotAnInteger:
@ -1010,7 +1039,7 @@ def moderation(request):
return render( return render(
request, request,
"agenda_culturel/moderation.html", "agenda_culturel/moderation.html",
{"filter": filter, "paginator_filter": response}, {"filter": filter, "paginator_filter": response, "daily_modifications": daily_modifications},
) )