Skip to content

Commit

Permalink
Fix bug capacity null or 0
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Jun 29, 2023
1 parent 8ec1c56 commit 68e3aca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
24 changes: 11 additions & 13 deletions backend/core/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,40 @@ 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
taux_remplissage_passe = (
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
)
]
)
/ 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"]
Expand Down
23 changes: 22 additions & 1 deletion backend/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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

0 comments on commit 68e3aca

Please sign in to comment.