forked from mirrors/bookwyrm
Tidies up charts
This commit is contained in:
parent
a79fb14686
commit
19cdda372c
4 changed files with 76 additions and 75 deletions
|
@ -77,63 +77,10 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.5.1/dist/chart.min.js"></script>
|
||||
<script>
|
||||
const labels = [{% for label in user_stats.labels %}"{{ label }}",{% endfor %}];
|
||||
const data = {
|
||||
labels: labels,
|
||||
datasets: [{
|
||||
label: '{% trans "Total" %}',
|
||||
backgroundColor: 'rgb(255, 99, 132)',
|
||||
borderColor: 'rgb(255, 99, 132)',
|
||||
data: {{ user_stats.total }},
|
||||
}, {
|
||||
label: '{% trans "Active this month" %}',
|
||||
backgroundColor: 'rgb(75, 192, 192)',
|
||||
borderColor: 'rgb(75, 192, 192)',
|
||||
data: {{ user_stats.active }},
|
||||
}]
|
||||
};
|
||||
// === include 'setup' then 'config' above ===
|
||||
|
||||
const config = {
|
||||
type: 'line',
|
||||
data: data,
|
||||
options: {}
|
||||
};
|
||||
|
||||
var userStats = new Chart(
|
||||
document.getElementById('user_stats'),
|
||||
config
|
||||
);
|
||||
</script>
|
||||
|
||||
<script>
|
||||
const status_labels = [{% for label in status_stats.labels %}"{{ label }}",{% endfor %}];
|
||||
const status_data = {
|
||||
labels: status_labels,
|
||||
datasets: [{
|
||||
label: '{% trans "Statuses posted" %}',
|
||||
backgroundColor: 'rgb(255, 99, 132)',
|
||||
borderColor: 'rgb(255, 99, 132)',
|
||||
data: {{ status_stats.total }},
|
||||
}]
|
||||
};
|
||||
// === include 'setup' then 'config' above ===
|
||||
|
||||
const status_config = {
|
||||
type: 'bar',
|
||||
data: status_data,
|
||||
options: {}
|
||||
};
|
||||
|
||||
var statusStats = new Chart(
|
||||
document.getElementById('status_stats'),
|
||||
status_config
|
||||
);
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{% static 'js/vendor/chart.min.js' %}?v={{ js_cache }}"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.5.1/dist/chart.min.js"></script>
|
||||
{% include 'settings/dashboard_user_chart.html' %}
|
||||
{% include 'settings/dashboard_status_chart.html' %}
|
||||
{% endblock %}
|
||||
|
|
26
bookwyrm/templates/settings/dashboard_status_chart.html
Normal file
26
bookwyrm/templates/settings/dashboard_status_chart.html
Normal file
|
@ -0,0 +1,26 @@
|
|||
{% load i18n %}
|
||||
<script>
|
||||
const status_labels = [{% for label in status_stats.labels %}"{{ label }}",{% endfor %}];
|
||||
const status_data = {
|
||||
labels: status_labels,
|
||||
datasets: [{
|
||||
label: '{% trans "Statuses posted" %}',
|
||||
backgroundColor: 'rgb(255, 99, 132)',
|
||||
borderColor: 'rgb(255, 99, 132)',
|
||||
data: {{ status_stats.total }},
|
||||
}]
|
||||
};
|
||||
// === include 'setup' then 'config' above ===
|
||||
|
||||
const status_config = {
|
||||
type: 'bar',
|
||||
data: status_data,
|
||||
options: {}
|
||||
};
|
||||
|
||||
var statusStats = new Chart(
|
||||
document.getElementById('status_stats'),
|
||||
status_config
|
||||
);
|
||||
</script>
|
||||
|
29
bookwyrm/templates/settings/dashboard_user_chart.html
Normal file
29
bookwyrm/templates/settings/dashboard_user_chart.html
Normal file
|
@ -0,0 +1,29 @@
|
|||
{% load i18n %}
|
||||
<script>
|
||||
const labels = [{% for label in user_stats.labels %}"{{ label }}",{% endfor %}];
|
||||
const data = {
|
||||
labels: labels,
|
||||
datasets: [{
|
||||
label: '{% trans "Total" %}',
|
||||
backgroundColor: 'rgb(255, 99, 132)',
|
||||
borderColor: 'rgb(255, 99, 132)',
|
||||
data: {{ user_stats.total }},
|
||||
}, {
|
||||
label: '{% trans "Active this month" %}',
|
||||
backgroundColor: 'rgb(75, 192, 192)',
|
||||
borderColor: 'rgb(75, 192, 192)',
|
||||
data: {{ user_stats.active }},
|
||||
}]
|
||||
};
|
||||
|
||||
const config = {
|
||||
type: 'line',
|
||||
data: data,
|
||||
options: {}
|
||||
};
|
||||
|
||||
var userStats = new Chart(
|
||||
document.getElementById('user_stats'),
|
||||
config
|
||||
);
|
||||
</script>
|
|
@ -30,34 +30,33 @@ class Dashboard(View):
|
|||
user_stats = {"labels": [], "total": [], "active": []}
|
||||
interval_end = now - timedelta(days=buckets * bucket_size)
|
||||
while interval_end < timezone.now():
|
||||
user_stats["total"].append(user_queryset.filter(
|
||||
created_date__lte=interval_end
|
||||
).count())
|
||||
user_stats["active"].append(user_queryset.filter(
|
||||
local=True,
|
||||
is_active=True,
|
||||
last_active_date__gte=interval_end - timedelta(days=31),
|
||||
created_date__lte=interval_end
|
||||
).count())
|
||||
user_stats["labels"].append(interval_end.strftime("%Y-%m-%d"))
|
||||
user_stats["total"].append(
|
||||
user_queryset.filter(created_date__day__lte=interval_end.day).count()
|
||||
)
|
||||
user_stats["active"].append(
|
||||
user_queryset.filter(
|
||||
last_active_date__gt=interval_end - timedelta(days=31),
|
||||
created_date__day__lte=interval_end.day,
|
||||
).count()
|
||||
)
|
||||
user_stats["labels"].append(interval_end.strftime("%b %d"))
|
||||
interval_end += timedelta(days=bucket_size)
|
||||
|
||||
status_queryset = models.Status.objects.filter(
|
||||
user__local=True, deleted=False
|
||||
)
|
||||
status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
|
||||
status_stats = {"labels": [], "total": []}
|
||||
interval_start = now - timedelta(days=buckets * bucket_size)
|
||||
interval_end = interval_start + timedelta(days=bucket_size)
|
||||
while interval_end < timezone.now():
|
||||
status_stats["total"].append(status_queryset.filter(
|
||||
created_date__gte=interval_start,
|
||||
created_date__lte=interval_end,
|
||||
).count())
|
||||
status_stats["labels"].append(interval_start.strftime("%Y-%m-%d"))
|
||||
status_stats["total"].append(
|
||||
status_queryset.filter(
|
||||
created_date__day__gt=interval_start.day,
|
||||
created_date__day__lte=interval_end.day,
|
||||
).count()
|
||||
)
|
||||
status_stats["labels"].append(interval_end.strftime("%b %d"))
|
||||
interval_start = interval_end
|
||||
interval_end += timedelta(days=bucket_size)
|
||||
|
||||
|
||||
data = {
|
||||
"users": user_queryset.count(),
|
||||
"active_users": user_queryset.filter(
|
||||
|
|
Loading…
Reference in a new issue