Tidies up charts

This commit is contained in:
Mouse Reeve 2021-09-11 07:09:05 -07:00
parent a79fb14686
commit 19cdda372c
4 changed files with 76 additions and 75 deletions

View file

@ -77,63 +77,10 @@
</div> </div>
</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 %} {% endblock %}
{% block scripts %} {% 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 %} {% endblock %}

View 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>

View 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>

View file

@ -30,34 +30,33 @@ class Dashboard(View):
user_stats = {"labels": [], "total": [], "active": []} user_stats = {"labels": [], "total": [], "active": []}
interval_end = now - timedelta(days=buckets * bucket_size) interval_end = now - timedelta(days=buckets * bucket_size)
while interval_end < timezone.now(): while interval_end < timezone.now():
user_stats["total"].append(user_queryset.filter( user_stats["total"].append(
created_date__lte=interval_end user_queryset.filter(created_date__day__lte=interval_end.day).count()
).count()) )
user_stats["active"].append(user_queryset.filter( user_stats["active"].append(
local=True, user_queryset.filter(
is_active=True, last_active_date__gt=interval_end - timedelta(days=31),
last_active_date__gte=interval_end - timedelta(days=31), created_date__day__lte=interval_end.day,
created_date__lte=interval_end ).count()
).count()) )
user_stats["labels"].append(interval_end.strftime("%Y-%m-%d")) user_stats["labels"].append(interval_end.strftime("%b %d"))
interval_end += timedelta(days=bucket_size) interval_end += timedelta(days=bucket_size)
status_queryset = models.Status.objects.filter( status_queryset = models.Status.objects.filter(user__local=True, deleted=False)
user__local=True, deleted=False
)
status_stats = {"labels": [], "total": []} status_stats = {"labels": [], "total": []}
interval_start = now - timedelta(days=buckets * bucket_size) interval_start = now - timedelta(days=buckets * bucket_size)
interval_end = interval_start + timedelta(days=bucket_size) interval_end = interval_start + timedelta(days=bucket_size)
while interval_end < timezone.now(): while interval_end < timezone.now():
status_stats["total"].append(status_queryset.filter( status_stats["total"].append(
created_date__gte=interval_start, status_queryset.filter(
created_date__lte=interval_end, created_date__day__gt=interval_start.day,
).count()) created_date__day__lte=interval_end.day,
status_stats["labels"].append(interval_start.strftime("%Y-%m-%d")) ).count()
)
status_stats["labels"].append(interval_end.strftime("%b %d"))
interval_start = interval_end interval_start = interval_end
interval_end += timedelta(days=bucket_size) interval_end += timedelta(days=bucket_size)
data = { data = {
"users": user_queryset.count(), "users": user_queryset.count(),
"active_users": user_queryset.filter( "active_users": user_queryset.filter(