-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conso by determinant export table mapper
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
public_data/infra/consommation/progression/export/ConsoByDeterminantExportTableMapper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from public_data.domain.consommation.entity import ConsommationCollection | ||
|
||
|
||
class ConsoByDeterminantExportTableMapper: | ||
@staticmethod | ||
def map(consommation_progression: list[ConsommationCollection]): | ||
category_to_attr = { | ||
"Habitat": "habitat", | ||
"Activité": "activite", | ||
"Mixte": "mixte", | ||
"Route": "route", | ||
"Ferré": "ferre", | ||
"Inconnu": "non_reseigne", | ||
"Total": "total", | ||
} | ||
|
||
headers = [str(conso.year) for conso in consommation_progression] + ["Total"] | ||
|
||
rows = [] | ||
for category in category_to_attr: | ||
category_values = [getattr(conso, category_to_attr[category]) for conso in consommation_progression] | ||
category_total = sum(category_values) | ||
# On arrondit ensuite pour ne pas fausser le total | ||
category_values_rounded = [round(value, 2) for value in category_values] | ||
category_total_rounded = round(category_total, 2) | ||
rows.append( | ||
{ | ||
"name": category, | ||
"data": category_values_rounded + [category_total_rounded], | ||
} | ||
) | ||
|
||
return { | ||
"headers": headers, | ||
"rows": rows, | ||
} |