mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
Chart class
This commit is contained in:
parent
2fe810ce5c
commit
2a88753e17
1 changed files with 51 additions and 31 deletions
|
@ -25,13 +25,6 @@ class Dashboard(View):
|
|||
"""list of users"""
|
||||
interval = int(request.GET.get("days", 1))
|
||||
now = timezone.now()
|
||||
|
||||
user_queryset = models.User.objects.filter(local=True)
|
||||
user_stats = {"labels": [], "total": [], "active": []}
|
||||
|
||||
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))
|
||||
|
@ -42,31 +35,33 @@ class Dashboard(View):
|
|||
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_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(
|
||||
interval_queryset.filter(
|
||||
last_active_date__gt=interval_end - timedelta(days=31),
|
||||
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,
|
||||
).filter(
|
||||
last_active_date__gt=e - timedelta(days=31),
|
||||
).count()
|
||||
)
|
||||
user_stats["labels"].append(interval_start.strftime("%b %d"))
|
||||
}
|
||||
)
|
||||
|
||||
status_stats["total"].append(
|
||||
status_queryset.filter(
|
||||
created_date__gt=interval_start,
|
||||
created_date__lte=interval_end,
|
||||
status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
|
||||
status_chart = Chart(
|
||||
queryset=status_queryset,
|
||||
queries = {
|
||||
"total": lambda q, s, e: q.filter(
|
||||
created_date__gt=s,
|
||||
created_date__lte=e,
|
||||
).count()
|
||||
)
|
||||
status_stats["labels"].append(interval_start.strftime("%b %d"))
|
||||
interval_start = interval_end
|
||||
interval_end += timedelta(days=interval)
|
||||
}
|
||||
)
|
||||
|
||||
data = {
|
||||
"start": start.strftime("%Y-%m-%d"),
|
||||
|
@ -82,7 +77,32 @@ class Dashboard(View):
|
|||
"invite_requests": models.InviteRequest.objects.filter(
|
||||
ignored=False, invite_sent=False
|
||||
).count(),
|
||||
"user_stats": user_stats,
|
||||
"status_stats": status_stats,
|
||||
"user_stats": user_chart.get_chart(start, end, interval),
|
||||
"status_stats": status_chart.get_chart(start, end, interval),
|
||||
}
|
||||
return TemplateResponse(request, "settings/dashboard/dashboard.html", data)
|
||||
|
||||
|
||||
class Chart:
|
||||
""" Data for a chart """
|
||||
|
||||
def __init__(self, queryset, queries: dict):
|
||||
self.queryset = queryset
|
||||
self.queries = queries
|
||||
|
||||
def get_chart(self, start, end, interval):
|
||||
""" load the data for the chart given a time scale and interval """
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue