moviewyrm/bookwyrm/views/admin/dashboard.py

133 lines
4.5 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
2021-09-11 16:31:11 +00:00
from django.db.models import Q
2021-09-11 04:12:08 +00:00
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()
2021-09-11 15:47:16 +00:00
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
2021-09-11 16:31:11 +00:00
start = start.replace(hour=0, minute=0, second=0)
2021-09-11 15:47:16 +00:00
2021-10-04 17:40:39 +00:00
user_queryset = models.User.objects.filter(local=True)
user_chart = Chart(
queryset=user_queryset,
queries={
"total": lambda q, s, e: q.filter(
Q(is_active=True) | Q(deactivation_date__gt=e),
created_date__lte=e,
).count(),
"active": lambda q, s, e: q.filter(
Q(is_active=True) | Q(deactivation_date__gt=e),
created_date__lte=e,
2021-10-04 18:16:02 +00:00
)
.filter(
2021-10-04 17:40:39 +00:00
last_active_date__gt=e - timedelta(days=31),
2021-10-04 18:16:02 +00:00
)
.count(),
},
2021-10-04 17:40:39 +00:00
)
2021-09-11 04:12:08 +00:00
2021-10-04 17:40:39 +00:00
status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
status_chart = Chart(
queryset=status_queryset,
2021-10-04 18:16:02 +00:00
queries={
2021-10-04 17:40:39 +00:00
"total": lambda q, s, e: q.filter(
created_date__gt=s,
created_date__lte=e,
2021-09-11 14:09:05 +00:00
).count()
2021-10-04 18:16:02 +00:00
},
2021-10-04 17:40:39 +00:00
)
2021-09-11 04:12:08 +00:00
register_chart = Chart(
queryset=user_queryset,
2021-10-04 18:16:02 +00:00
queries={
"total": lambda q, s, e: q.filter(
created_date__gt=s,
created_date__lte=e,
).count()
2021-10-04 18:16:02 +00:00
},
)
2021-10-04 18:14:50 +00:00
works_chart = Chart(
queryset=models.Work.objects,
2021-10-04 18:16:02 +00:00
queries={
2021-10-04 18:14:50 +00:00
"total": lambda q, s, e: q.filter(
created_date__gt=s,
created_date__lte=e,
).count()
2021-10-04 18:16:02 +00:00
},
2021-10-04 18:14:50 +00:00
)
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 16:31:11 +00:00
"users": user_queryset.filter(is_active=True).count(),
2021-09-11 04:12:08 +00:00
"active_users": user_queryset.filter(
2021-09-11 16:31:11 +00:00
is_active=True, last_active_date__gte=now - timedelta(days=31)
2021-09-11 04:12:08 +00:00
).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(),
2021-10-04 17:40:39 +00:00
"user_stats": user_chart.get_chart(start, end, interval),
"status_stats": status_chart.get_chart(start, end, interval),
"register_stats": register_chart.get_chart(start, end, interval),
2021-10-04 18:14:50 +00:00
"works_stats": works_chart.get_chart(start, end, interval),
2021-09-11 04:12:08 +00:00
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/dashboard/dashboard.html", data)
2021-10-04 17:40:39 +00:00
class Chart:
2021-10-04 18:16:02 +00:00
"""Data for a chart"""
2021-10-04 17:40:39 +00:00
def __init__(self, queryset, queries: dict):
self.queryset = queryset
self.queries = queries
def get_chart(self, start, end, interval):
2021-10-04 18:16:02 +00:00
"""load the data for the chart given a time scale and interval"""
2021-10-04 17:40:39 +00:00
interval_start = start
interval_end = interval_start + timedelta(days=interval)
chart = {k: [] for k in self.queries.keys()}
chart["labels"] = []
while interval_start <= end:
for (name, query) in self.queries.items():
chart[name].append(query(self.queryset, interval_start, interval_end))
chart["labels"].append(interval_start.strftime("%b %d"))
interval_start = interval_end
interval_end += timedelta(days=interval)
return chart