Ajout de la fonctionnalité d’activité en direct dans le tableau de bord des statistiques et mise à jour des templates, vues, URL, et styles associés.
This commit is contained in:
parent
a7b51e3a82
commit
7cf04968eb
6 changed files with 114 additions and 2 deletions
|
|
@ -8,4 +8,5 @@ urlpatterns = [
|
|||
# Tableau de bord statistiques (réservé superadministrateurs)
|
||||
path('dashboard/stats/', views.stats_dashboard, name='stats_dashboard'),
|
||||
path('dashboard/stats/charts/', views.stats_charts, name='stats_charts'),
|
||||
path('dashboard/stats/live-activity/', views.live_activity, name='live_activity'),
|
||||
]
|
||||
|
|
@ -9,6 +9,7 @@ from courses.models import Course, Lesson
|
|||
from blog.models import Post
|
||||
from progression.models import Progression
|
||||
import json
|
||||
from django.http import JsonResponse
|
||||
|
||||
def home(request):
|
||||
courses = Course.objects.order_by('-created_at')[:6]
|
||||
|
|
@ -293,3 +294,44 @@ def stats_charts(request):
|
|||
}
|
||||
|
||||
return render(request, 'home/stats_charts.html', context)
|
||||
|
||||
|
||||
@user_passes_test(lambda u: u.is_superuser)
|
||||
def live_activity(request):
|
||||
"""Retourne en JSON l'activité récente (5 dernières minutes):
|
||||
visiteurs et utilisateurs et leur page actuelle.
|
||||
"""
|
||||
now = timezone.now()
|
||||
since = now - timezone.timedelta(minutes=5)
|
||||
qs = (
|
||||
Visit.objects
|
||||
.filter(last_seen__gte=since)
|
||||
.order_by('-last_seen')
|
||||
)
|
||||
|
||||
data = []
|
||||
for v in qs[:200]:
|
||||
username = None
|
||||
is_user = False
|
||||
if v.user_id:
|
||||
is_user = True
|
||||
# safe access if user deleted
|
||||
try:
|
||||
username = v.user.username
|
||||
except Exception:
|
||||
username = 'Utilisateur'
|
||||
visitor_label = v.visitor_id[:8]
|
||||
seconds_ago = int((now - v.last_seen).total_seconds())
|
||||
data.append({
|
||||
'visitor': visitor_label,
|
||||
'is_user': is_user,
|
||||
'username': username,
|
||||
'path': v.path,
|
||||
'last_seen': v.last_seen.isoformat(),
|
||||
'seconds_ago': seconds_ago,
|
||||
'date': str(v.date),
|
||||
'country': v.country,
|
||||
'source': v.source,
|
||||
})
|
||||
|
||||
return JsonResponse({'now': now.isoformat(), 'items': data})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue