Skip to content

Commit

Permalink
Conso by determinant export table mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
smdsgn committed Dec 6, 2024
1 parent 380e86e commit ffe6f88
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion diagnostic_word/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from public_data.domain.impermeabilisation.difference.ImpermeabilisationDifferenceService import (
ImpermeabilisationDifferenceService,
)
from public_data.infra.consommation.progression.export.ConsoByDeterminantExportTableMapper import (
ConsoByDeterminantExportTableMapper,
)
from public_data.infra.consommation.progression.export.ConsoComparisonExportTableMapper import (
ConsoComparisonExportTableMapper,
)
Expand Down Expand Up @@ -197,7 +200,15 @@ def get_context_data(self) -> Dict[str, Any]:
series=annual_total_conso_chart.get_series(), line=False
),
"communes_data_table": add_total_line_column(chart_conso_cities.get_series()),
"determinants_data_table": add_total_line_column(det_chart.get_series()),
"determinants_data_table": ConsoByDeterminantExportTableMapper.map(
consommation_progression=PublicDataContainer.consommation_progression_service()
.get_by_land(
land=diagnostic.land_proxy,
start_date=int(diagnostic.analyse_start_date),
end_date=int(diagnostic.analyse_end_date),
)
.consommation
),
# Target 2031
"target_2031_consumed": target_2031_consumption,
"projection_zan_cumulee_ref": round(objective_chart.total_2020, 1),
Expand Down
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,
}

0 comments on commit ffe6f88

Please sign in to comment.