mirror of
https://github.com/ae-utbm/sith.git
synced 2026-09-23 20:54:21 +00:00
optimize subscriptions stats
This commit is contained in:
@@ -13,12 +13,6 @@
|
|||||||
|
|
||||||
<canvas id="statsChart" width="400" height="200"></canvas>
|
<canvas id="statsChart" width="400" height="200"></canvas>
|
||||||
|
|
||||||
{% trans %}Total subscriptions{% endtrans %} : {{ subscriptions_total.count() }}<br><br>
|
|
||||||
{% trans %}Subscriptions by type{% endtrans %}<br><br>
|
|
||||||
{% for location in locations %}
|
|
||||||
{{ location[1] }} : <i class="nb">{{ subscriptions_total.filter(location=location[0]).count() }}</i><br>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
@@ -28,20 +22,27 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
<th id="graphLabel">{% trans %}Total{% endtrans %}</th>
|
<th id="graphLabel">{% trans %}Total{% endtrans %}</th>
|
||||||
</thead>
|
</thead>
|
||||||
|
<tr>
|
||||||
|
<td>{% trans %}All subscriptions{% endtrans %}</td>
|
||||||
|
{% for location in locations %}
|
||||||
|
<td><i class="nb">{{ total_location[location[0]] }}</i><br></td>
|
||||||
|
{% endfor %}
|
||||||
|
<td><i class="nb">{{ total_location.values()|sum }}</i></td>
|
||||||
|
</tr>
|
||||||
{% for type in subscriptions_types %}
|
{% for type in subscriptions_types %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><i class="types" >{{ subscriptions_types[type]['name'] }}</i></td>
|
<td><i class="types">{{ subscriptions_types[type]['name'] }}</i></td>
|
||||||
{% set subscriptions_total_type = subscriptions_total.filter(subscription_type=type) %}
|
|
||||||
{% for location in locations %}
|
{% for location in locations %}
|
||||||
<td>
|
<td>
|
||||||
{% set subscriptions_total_type_location = subscriptions_total_type.filter(location=location[0]) %}
|
|
||||||
{% trans %}Total{% endtrans %} : {{ subscriptions_total_type_location.count()}}<br>
|
|
||||||
{% for p_type in payment_types %}
|
{% for p_type in payment_types %}
|
||||||
{{ p_type[1] }} : <i class="nb">{{ subscriptions_total_type_location.filter(payment_method=p_type[0]).count()}}</i><br>
|
{% set subtotal = subscriptions[type][location[0]][p_type[0]] %}
|
||||||
|
{% if subtotal > 0 %}
|
||||||
|
{{ p_type[1] }} : <i class="nb">{{ subtotal }}</i><br>
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</td>
|
</td>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<td class="total"><i class="nb">{{subscriptions_total_type.count()}}</i></td>
|
<td class="total"><i class="nb">{{ total_type[type] }}</i></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
+22
-1
@@ -12,11 +12,13 @@
|
|||||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.forms import PasswordResetForm
|
from django.contrib.auth.forms import PasswordResetForm
|
||||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
|
from django.db.models import Count
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.timezone import localdate
|
from django.utils.timezone import localdate
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
@@ -102,9 +104,28 @@ class SubscriptionsStatsView(TemplateView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
today = localdate()
|
today = localdate()
|
||||||
kwargs["subscriptions_total"] = Subscription.objects.filter(
|
qs = Subscription.objects.filter(
|
||||||
subscription_end__gte=today, subscription_start__lte=today
|
subscription_end__gte=today, subscription_start__lte=today
|
||||||
)
|
)
|
||||||
|
grouped = qs.values("subscription_type", "location", "payment_method").annotate(
|
||||||
|
count=Count("*")
|
||||||
|
)
|
||||||
|
by_location = qs.values("location").annotate(count=Count("*"))
|
||||||
|
by_type = qs.values("subscription_type").annotate(count=Count("*"))
|
||||||
|
kwargs["subscriptions"] = defaultdict(
|
||||||
|
lambda: defaultdict(lambda: defaultdict(int))
|
||||||
|
)
|
||||||
|
for sub in grouped:
|
||||||
|
kwargs["subscriptions"][sub["subscription_type"]][sub["location"]][
|
||||||
|
sub["payment_method"]
|
||||||
|
] = sub["count"]
|
||||||
|
kwargs["total_location"] = defaultdict(
|
||||||
|
int, {i["location"]: i["count"] for i in by_location}
|
||||||
|
)
|
||||||
|
kwargs["total_type"] = defaultdict(
|
||||||
|
int, {i["subscription_type"]: i["count"] for i in by_type}
|
||||||
|
)
|
||||||
|
|
||||||
kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS
|
kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS
|
||||||
kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD
|
kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD
|
||||||
kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS
|
kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS
|
||||||
|
|||||||
Reference in New Issue
Block a user