From e801c7d99165d1de005152bae7a50bac82bf58cd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 11 Sep 2021 09:31:11 -0700 Subject: [PATCH] Cleaner chart intervals --- bookwyrm/views/admin/dashboard.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/bookwyrm/views/admin/dashboard.py b/bookwyrm/views/admin/dashboard.py index 49c69017..161dc6da 100644 --- a/bookwyrm/views/admin/dashboard.py +++ b/bookwyrm/views/admin/dashboard.py @@ -3,6 +3,7 @@ from datetime import timedelta from dateutil.parser import parse from django.contrib.auth.decorators import login_required, permission_required +from django.db.models import Q from django.template.response import TemplateResponse from django.utils import timezone from django.utils.decorators import method_decorator @@ -25,7 +26,7 @@ class Dashboard(View): interval = int(request.GET.get("days", 1)) now = timezone.now() - user_queryset = models.User.objects.filter(local=True, is_active=True) + user_queryset = models.User.objects.filter(local=True) user_stats = {"labels": [], "total": [], "active": []} status_queryset = models.Status.objects.filter(user__local=True, deleted=False) @@ -39,20 +40,23 @@ class Dashboard(View): end = request.GET.get("end") end = timezone.make_aware(parse(end)) if end else now + start = start.replace(hour=0, minute=0, second=0) interval_start = start interval_end = interval_start + timedelta(days=interval) - while interval_end <= end: - user_stats["total"].append( - user_queryset.filter(created_date__lte=interval_end).count() + while interval_start <= end: + print(interval_start, interval_end) + interval_queryset = user_queryset.filter( + Q(is_active=True) | Q(deactivation_date__gt=interval_end), + created_date__lte=interval_end, ) + user_stats["total"].append(interval_queryset.filter().count()) user_stats["active"].append( - user_queryset.filter( + interval_queryset.filter( last_active_date__gt=interval_end - timedelta(days=31), - created_date__lte=interval_end, ).count() ) - user_stats["labels"].append(interval_end.strftime("%b %d")) + user_stats["labels"].append(interval_start.strftime("%b %d")) status_stats["total"].append( status_queryset.filter( @@ -60,20 +64,17 @@ class Dashboard(View): created_date__lte=interval_end, ).count() ) - status_stats["labels"].append(interval_end.strftime("%b %d")) + status_stats["labels"].append(interval_start.strftime("%b %d")) interval_start = interval_end - if interval_end >= end: - break interval_end += timedelta(days=interval) - interval_end = interval_end if interval_end < end else end data = { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), "interval": interval, - "users": user_queryset.count(), + "users": user_queryset.filter(is_active=True).count(), "active_users": user_queryset.filter( - last_active_date__gte=now - timedelta(days=31) + is_active=True, last_active_date__gte=now - timedelta(days=31) ).count(), "statuses": status_queryset.count(), "works": models.Work.objects.count(),