Cleaner chart intervals

This commit is contained in:
Mouse Reeve 2021-09-11 09:31:11 -07:00
parent 3e7847e645
commit e801c7d991

View file

@ -3,6 +3,7 @@ from datetime import timedelta
from dateutil.parser import parse from dateutil.parser import parse
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import Q
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils import timezone from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -25,7 +26,7 @@ class Dashboard(View):
interval = int(request.GET.get("days", 1)) interval = int(request.GET.get("days", 1))
now = timezone.now() 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": []} user_stats = {"labels": [], "total": [], "active": []}
status_queryset = models.Status.objects.filter(user__local=True, deleted=False) status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
@ -39,20 +40,23 @@ class Dashboard(View):
end = request.GET.get("end") end = request.GET.get("end")
end = timezone.make_aware(parse(end)) if end else now end = timezone.make_aware(parse(end)) if end else now
start = start.replace(hour=0, minute=0, second=0)
interval_start = start interval_start = start
interval_end = interval_start + timedelta(days=interval) interval_end = interval_start + timedelta(days=interval)
while interval_end <= end: while interval_start <= end:
user_stats["total"].append( print(interval_start, interval_end)
user_queryset.filter(created_date__lte=interval_end).count() interval_queryset = user_queryset.filter(
) Q(is_active=True) | Q(deactivation_date__gt=interval_end),
user_stats["active"].append(
user_queryset.filter(
last_active_date__gt=interval_end - timedelta(days=31),
created_date__lte=interval_end, created_date__lte=interval_end,
)
user_stats["total"].append(interval_queryset.filter().count())
user_stats["active"].append(
interval_queryset.filter(
last_active_date__gt=interval_end - timedelta(days=31),
).count() ).count()
) )
user_stats["labels"].append(interval_end.strftime("%b %d")) user_stats["labels"].append(interval_start.strftime("%b %d"))
status_stats["total"].append( status_stats["total"].append(
status_queryset.filter( status_queryset.filter(
@ -60,20 +64,17 @@ class Dashboard(View):
created_date__lte=interval_end, created_date__lte=interval_end,
).count() ).count()
) )
status_stats["labels"].append(interval_end.strftime("%b %d")) status_stats["labels"].append(interval_start.strftime("%b %d"))
interval_start = interval_end interval_start = interval_end
if interval_end >= end:
break
interval_end += timedelta(days=interval) interval_end += timedelta(days=interval)
interval_end = interval_end if interval_end < end else end
data = { data = {
"start": start.strftime("%Y-%m-%d"), "start": start.strftime("%Y-%m-%d"),
"end": end.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"),
"interval": interval, "interval": interval,
"users": user_queryset.count(), "users": user_queryset.filter(is_active=True).count(),
"active_users": user_queryset.filter( "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(), ).count(),
"statuses": status_queryset.count(), "statuses": status_queryset.count(),
"works": models.Work.objects.count(), "works": models.Work.objects.count(),