moviewyrm/bookwyrm/views/admin/dashboard.py

88 lines
3.2 KiB
Python
Raw Normal View History

2021-09-11 04:12:08 +00:00
""" instance overview """
from datetime import timedelta
2021-09-11 15:47:16 +00:00
from dateutil.parser import parse
2021-09-11 04:12:08 +00:00
from django.contrib.auth.decorators import login_required, permission_required
from django.template.response import TemplateResponse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import models
# pylint: disable= no-self-use
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.moderate_user", raise_exception=True),
name="dispatch",
)
class Dashboard(View):
"""admin overview"""
def get(self, request):
"""list of users"""
2021-09-11 15:47:16 +00:00
interval = int(request.GET.get("days", 1))
2021-09-11 04:12:08 +00:00
now = timezone.now()
user_queryset = models.User.objects.filter(local=True, is_active=True)
user_stats = {"labels": [], "total": [], "active": []}
2021-09-11 15:47:16 +00:00
status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
status_stats = {"labels": [], "total": []}
start = request.GET.get("start")
if start:
start = timezone.make_aware(parse(start))
else:
start = now - timedelta(days=6 * interval)
end = request.GET.get("end")
end = timezone.make_aware(parse(end)) if end else now
interval_start = start
interval_end = interval_start + timedelta(days=interval)
while interval_end <= end:
2021-09-11 14:09:05 +00:00
user_stats["total"].append(
2021-09-11 15:47:16 +00:00
user_queryset.filter(created_date__lte=interval_end).count()
2021-09-11 14:09:05 +00:00
)
user_stats["active"].append(
user_queryset.filter(
last_active_date__gt=interval_end - timedelta(days=31),
2021-09-11 15:47:16 +00:00
created_date__lte=interval_end,
2021-09-11 14:09:05 +00:00
).count()
)
user_stats["labels"].append(interval_end.strftime("%b %d"))
2021-09-11 04:12:08 +00:00
2021-09-11 14:09:05 +00:00
status_stats["total"].append(
status_queryset.filter(
2021-09-11 15:47:16 +00:00
created_date__gt=interval_start,
created_date__lte=interval_end,
2021-09-11 14:09:05 +00:00
).count()
)
status_stats["labels"].append(interval_end.strftime("%b %d"))
2021-09-11 04:12:08 +00:00
interval_start = interval_end
2021-09-11 15:47:16 +00:00
if interval_end >= end:
break
interval_end += timedelta(days=interval)
interval_end = interval_end if interval_end < end else end
2021-09-11 04:12:08 +00:00
data = {
2021-09-11 15:47:16 +00:00
"start": start.strftime("%Y-%m-%d"),
"end": end.strftime("%Y-%m-%d"),
"interval": interval,
2021-09-11 04:12:08 +00:00
"users": user_queryset.count(),
"active_users": user_queryset.filter(
last_active_date__gte=now - timedelta(days=31)
).count(),
"statuses": status_queryset.count(),
"works": models.Work.objects.count(),
"reports": models.Report.objects.filter(resolved=False).count(),
"invite_requests": models.InviteRequest.objects.filter(
ignored=False, invite_sent=False
).count(),
"user_stats": user_stats,
"status_stats": status_stats,
}
return TemplateResponse(request, "settings/dashboard.html", data)