diff --git a/mesads/api/views.py b/mesads/api/views.py index 54cfc78..c9601e1 100644 --- a/mesads/api/views.py +++ b/mesads/api/views.py @@ -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") diff --git a/mesads/app/views/public.py b/mesads/app/views/public.py index 6d5d1b7..b1562fd 100644 --- a/mesads/app/views/public.py +++ b/mesads/app/views/public.py @@ -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 @@ -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: diff --git a/mesads/templates/webpack/pages/stats.html b/mesads/templates/webpack/pages/stats.html index 2ced952..d2f728b 100644 --- a/mesads/templates/webpack/pages/stats.html +++ b/mesads/templates/webpack/pages/stats.html @@ -91,6 +91,36 @@

Carte de déploiement

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.

+
+
+
+
+ + + + + + + + + + + + {% for prefecture, stats in stats_by_prefecture %} + + + + + + + {% endfor %} + +
Statistiques par préfecture
PréfectureNombre d'ADSNombre total estimé d'ADSNombre de véhicules relais
{{ prefecture.libelle }}{{ stats.ads_count|default:"" }}{{ stats.expected_ads_count|default:"" }}{{ stats.vehicules_relais_count|default:"" }}
+
+
+
+
+

Utilisation du registre ADS