diff --git a/backend/core/repository.py b/backend/core/repository.py index 86999cf8..9a70223e 100644 --- a/backend/core/repository.py +++ b/backend/core/repository.py @@ -40,19 +40,21 @@ def query_stats_animations_per_month(params): def query_stats_bilan(params): - query = GTEvents.query + query = GTEvents.query.filter(GTEvents.deleted != True) if "year" in params: query = query.filter( func.date_part("year", GTEvents.begin_date) == params["year"] ) nb_events = query.count() events = query.all() - sum_nb_participant = sum([d.sum_participants for d in events]) - sum_clean_nb_participants = sum([d.capacity for d in events]) + + # events with capacity + events_capacity = [e for e in events if e.capacity and e.capacity > 0] + sum_nb_participant = sum([d.sum_participants for d in events_capacity]) + sum_clean_nb_participants = sum([d.capacity for d in events_capacity]) # Taux de remplissage de toutes les animations taux_remplissage = ( - sum([d.sum_participants / d.capacity for d in events if d.capacity > 0]) - / nb_events + sum([d.sum_participants / d.capacity for d in events_capacity]) / nb_events ) # Taux de remplissage des animations passées @@ -60,10 +62,10 @@ def query_stats_bilan(params): sum( [ d.sum_participants / d.capacity - for d in events + for d in events_capacity if ( - (d.end_date or datetime.now().date()) < datetime.now().date() - and d.capacity > 0 + (d.end_date or datetime.now().date()) + < datetime.now().date() # and not getattr(d, "bilan", {}).annulation ) ] @@ -71,11 +73,7 @@ def query_stats_bilan(params): / nb_events ) - query = ( - db.session.query(func.count(TAnimationsBilans.id_bilan)) - .filter(TAnimationsBilans.annulation == True) - .join(GTEvents, GTEvents.id == TAnimationsBilans.id_event) - ) + query = db.session.query(func.count(GTEvents.id)).filter(GTEvents.cancelled == True) if "year" in params: query = query.filter( func.date_part("year", GTEvents.begin_date) == params["year"] diff --git a/backend/test/test_api.py b/backend/test/test_api.py index 21b59f7b..5d69e162 100644 --- a/backend/test/test_api.py +++ b/backend/test/test_api.py @@ -4,7 +4,7 @@ import json import logging -from core.models import GTEvents, TReservations, TAnimationsBilans +from core.models import GTEvents, TReservations from core.models import TTokens from .fixtures import ( @@ -154,3 +154,24 @@ def test_post_export_and_cancel_one_reservation(self, events): # data_bilan["id_event"] = event.id # resp = post_json(self.client, url_for("app_routes.post_bilans"), data_bilan) # assert resp == 200 + + def test_bilan_global(self): + login(self.client) + donnees_exemple = { + "nb_animations": 1, + "nb_annulation": 0, + "sum_nb_inscriptions": 2, + "sum_nb_participants_possible": 2089, + "taux_remplissage": 0.0007017543859649122, + "taux_remplissage_passe": 0.0007017543859649122, + } + response = self.client.get(url_for("app_routes.get_stats_global")) + assert response.status_code == 200 + + data = json_of_response(response) + + assert set(data.keys()) == set(donnees_exemple.keys()) + # Todo test return value + + response = self.client.get(url_for("app_routes.get_stats_global", year="2023")) + assert response.status_code == 200