Natixar / natixar-frontend

The static front end of the Natixar SaaS platform
0 stars 5 forks source link

Wrong Result in Bar Chart and Stacked Areas Chart #87

Closed lepeuvedic-natixar closed 2 weeks ago

lepeuvedic-natixar commented 1 month ago

https://github.com/Natixar/natixar-frontend/blob/ead8cd58e471085638f3ef84d520788ce85e8a27/src/data/domain/transformers/EmissionTransformers.ts#L220

Problem

Similar code was used in the function calculateTotalAmount(). This code is not correct and gives wrong results when the startTimeSlot is also the endTimeSlot.

Steps to Reproduce

Just observe the Dashboard. The total on the donut, 110,5 tCO2eq is supposedly equal to sum of the 24 monthly consumptions on the bar chart. But even a single bar is labeled in ktCO2eq. Considering emission flows of roughly 0.002 kgCO2eq/s and 2600000s/month the correct value is around 5200 kgCO2eq/month, which gives 124800 kgCO2eq for 24 months (the period displayed). This approximation is close enough to the real value of 110,5 tCO2eq which takes into account the decrease of emissions with time.

Therefore the absolute value of bar height roughly 2000000 kgCO2eq (2 ktCO2eq) for one month is completely through the roof and wrong.

Expected Behavior

In that case, the correct calculation is to assert that endEmissionPercentage is greater than (1 - startEmissionPercentage), and if the test passes, to compute the difference : endEmissionPercentage - (1 - startEmissionPercentage), and to multiply that percentage difference by the emissionIntensity * currentSlotDelta

The most likely culprit for the huge error is currentSlotDelta because the value is returned in milliseconds when emissionIntensity is in kgCO2eq per second, but dividing the values by 1000 (tons instead of kilotons) yields monthly totals on the order to 2 tons, and this is less than one half of the expected value, so there is another error.

lepeuvedic commented 3 weeks ago

Also fixes the backend categories... The official "dev" backend runs this branch in a container.

ChrisNatixar commented 2 weeks ago

Replaced by #116 and #117 with more specific details based on latest version