Skip to content

Commit

Permalink
stats: add table with stats by pref
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Feb 23, 2025
1 parent dd0ab27 commit 9f2a4d4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
38 changes: 22 additions & 16 deletions mesads/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,32 @@ def perform_create(self, serializer):
serializer.save(user=self.request.user)


def get_stats_by_prefecture():
ads_stats = ADSManagerAdministrator.objects.select_related("prefecture").annotate(
ads_count=Count("adsmanager__ads")
)

stats = {
ads_manager_administrator.prefecture.numero: {
"ads_count": ads_manager_administrator.ads_count,
"expected_ads_count": ads_manager_administrator.expected_ads_count,
}
for ads_manager_administrator in ads_stats.all()
}

vehicules_relais_stats = Prefecture.objects.annotate(count=Count("vehicule"))

for row in vehicules_relais_stats:
stats[row.numero]["vehicules_relais_count"] = row.count

return stats


class StatsGeoJSONPerPrefecture(views.APIView):
"""Exposes a GeoJSON with statistics for each prefecture."""

def get(self, request):
ads_stats = ADSManagerAdministrator.objects.select_related(
"prefecture"
).annotate(ads_count=Count("adsmanager__ads"))

stats = {
ads_manager_administrator.prefecture.numero: {
"ads_count": ads_manager_administrator.ads_count,
"expected_ads_count": ads_manager_administrator.expected_ads_count,
}
for ads_manager_administrator in ads_stats.all()
}

vehicules_relais_stats = Prefecture.objects.annotate(count=Count("vehicule"))

for row in vehicules_relais_stats:
stats[row.numero]["vehicules_relais_count"] = row.count
stats = get_stats_by_prefecture()

departements_shpfile = (
importlib.resources.files("mesads.api.resources")
Expand Down
10 changes: 10 additions & 0 deletions mesads/app/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
ADSManager,
ADSManagerRequest,
)
from mesads.api.views import get_stats_by_prefecture
from mesads.fradm.models import Prefecture
from mesads.vehicules_relais.models import Proprietaire, Vehicule


Expand Down Expand Up @@ -40,6 +42,14 @@ class StatsView(TemplateView):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)

stats_by_pref = get_stats_by_prefecture()
ctx["stats_by_prefecture"] = [
(prefecture, stats_by_pref.get(prefecture.numero))
for prefecture in Prefecture.objects.order_by("numero").exclude(
libelle__icontains="test"
)
]

if len(self.request.GET.getlist("q")) == 0:
ads_managers_select_form = ADSManagerAutocompleteForm()
else:
Expand Down
30 changes: 30 additions & 0 deletions mesads/templates/webpack/pages/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ <h2>Carte de déploiement</h2>
Cette carte présente le nombre d'ADS enregistrées dans chaque département jusqu'à ce jour. Le nombre d'ADS estimé peut ne pas être exact, mais permet d'avoir une vue d'ensemble du déploiement du registre ADS au niveau national.
</p>

<div class="fr-table" id="table-md-component">
<div class="fr-table__wrapper">
<div class="fr-table__container">
<div class="fr-table__content">
<table id="table-md">
<caption>Statistiques par préfecture</caption>
<thead>
<tr>
<th scope="col">Préfecture</th>
<th scope="col">Nombre d'ADS</th>
<th scope="col">Nombre total estimé d'ADS</th>
<th scope="col">Nombre de véhicules relais</th>
</tr>
</thead>
<tbody>
{% for prefecture, stats in stats_by_prefecture %}
<tr>
<td>{{ prefecture.libelle }}</td>
<td>{{ stats.ads_count|default:"" }}</td>
<td>{{ stats.expected_ads_count|default:"" }}</td>
<td>{{ stats.vehicules_relais_count|default:"" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>

<hr class="fr-mt-3w" />

<h2>Utilisation du registre ADS</h2>
Expand Down

0 comments on commit 9f2a4d4

Please sign in to comment.