Skip to content

Commit

Permalink
Fix percentage difference nan values, update wrong null expects
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Jan 1, 2024
1 parent 38169aa commit ebb4359
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
26 changes: 19 additions & 7 deletions api/src/modules/impact/comparison/actual-vs-scenario.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,21 @@ export class ActualVsScenarioImpactService {
return rangeOfYears.map((year: number) => {
const totalSumByYear: number = yearSumMap.get(year) || 0;
const totalScenarioSumByYear: number = yearScenarioSumMap.get(year) || 0;
const absoluteDifference: number =
totalScenarioSumByYear - totalSumByYear;
const percentageDifference: number =
((totalScenarioSumByYear - totalSumByYear) /
((totalScenarioSumByYear + totalSumByYear) / 2)) *
100;

return {
year,
value: totalSumByYear,
comparedScenarioValue: totalScenarioSumByYear,
absoluteDifference: totalScenarioSumByYear - totalSumByYear,
percentageDifference:
((totalScenarioSumByYear - totalSumByYear) /
((totalScenarioSumByYear + totalSumByYear) / 2)) *
100,
absoluteDifference,
percentageDifference: isNaN(percentageDifference)
? 0
: percentageDifference,
isProjected: year > lastYearWithData,
};
});
Expand Down Expand Up @@ -319,13 +324,20 @@ export class ActualVsScenarioImpactService {
valueToAggregate[valueIndex].isProjected ||
entityRowValue.isProjected;

entityRowValue.absoluteDifference =
const absoluteDifference: number =
entityRowValue.comparedScenarioValue - entityRowValue.value;
entityRowValue.percentageDifference =
const percentageDifference: number =
((entityRowValue.comparedScenarioValue - entityRowValue.value) /
((entityRowValue.comparedScenarioValue + entityRowValue.value) /
2)) *
100;

entityRowValue.absoluteDifference = isNaN(absoluteDifference)
? 0
: absoluteDifference;
entityRowValue.percentageDifference = isNaN(percentageDifference)
? 0
: percentageDifference;
}
}
return entity.values;
Expand Down
33 changes: 22 additions & 11 deletions api/src/modules/impact/comparison/scenario-vs-scenario.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,23 @@ export class ScenarioVsScenarioImpactService {
valueToAggregate[valueIndex].isProjected ||
entityRowValue.isProjected;

entityRowValue.absoluteDifference =
const absoluteDifference: number =
entityRowValue.comparedScenarioValue -
entityRowValue.baseScenarioValue;
entityRowValue.percentageDifference =
const percentageDifference: number =
((entityRowValue.comparedScenarioValue -
entityRowValue.baseScenarioValue) /
((entityRowValue.comparedScenarioValue +
entityRowValue.baseScenarioValue) /
2)) *
100;

entityRowValue.absoluteDifference = isNaN(absoluteDifference)
? 0
: absoluteDifference;
entityRowValue.percentageDifference = isNaN(percentageDifference)
? 0
: percentageDifference;
}
}
return entity.values;
Expand Down Expand Up @@ -423,19 +430,23 @@ export class ScenarioVsScenarioImpactService {
const comparedScenarioTotalSumByYear: number =
comparedScenarioYearSumMap.get(year) || 0;

const absoluteDifference: number =
comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0);
const percentageDifference: number =
((comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0)) /
((comparedScenarioTotalSumByYear +
(baseScenarioTotalSumByYear || 0)) /
2)) *
100;

return {
year,
baseScenarioValue: baseScenarioTotalSumByYear,
comparedScenarioValue: comparedScenarioTotalSumByYear,
absoluteDifference:
comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0),
percentageDifference:
((comparedScenarioTotalSumByYear -
(baseScenarioTotalSumByYear || 0)) /
((comparedScenarioTotalSumByYear +
(baseScenarioTotalSumByYear || 0)) /
2)) *
100,
absoluteDifference: isNaN(absoluteDifference) ? 0 : absoluteDifference,
percentageDifference: isNaN(percentageDifference)
? 0
: percentageDifference,
isProjected: year > lastYearWithData,
};
});
Expand Down
2 changes: 1 addition & 1 deletion api/test/e2e/impact/impact-reports/impact-reports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Impact Reports', () => {
});
it('should create a scenario vs scenario impact report', async () => {
const { baseScenario, comparedScenario, indicator } =
await fixtures.GivenTwoScenarios();
await fixtures.GivenTwoScenarioWithInterventions();
const response = await fixtures.WhenIRequestAScenarioVsScenarioImpactReport(
{
app: testApplication,
Expand Down
2 changes: 1 addition & 1 deletion api/test/e2e/impact/impact-reports/impactReportFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const impactReportFixtures = () => ({
scenarioIntervention,
};
},
GivenTwoScenarios: async (): Promise<any> => {
GivenTwoScenarioWithInterventions: async (): Promise<any> => {
const {
newScenarioChangeSupplier: baseScenario,
newScenarioChangeMaterial: comparedScenario,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const mixedInterventionsScenarioTable2019 = {
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -262,7 +262,7 @@ export const mixedInterventionsScenarioTable2019 = {
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -308,7 +308,7 @@ export const mixedInterventionsScenarioTable2019 = {
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -352,7 +352,7 @@ export const mixedInterventionsScenarioTable2019 = {
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -395,7 +395,7 @@ export const mixedInterventionsScenarioTable2019 = {
value: 0,
comparedScenarioValue: 0,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ export function getComparisonResponseWithProjectedYears(
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2019,
baseScenarioValue: 0,
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -298,15 +298,15 @@ export function getComparisonResponseWithProjectedYears(
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2019,
baseScenarioValue: 0,
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -344,15 +344,15 @@ export function getComparisonResponseWithProjectedYears(
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2019,
baseScenarioValue: 0,
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -388,15 +388,15 @@ export function getComparisonResponseWithProjectedYears(
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2019,
baseScenarioValue: 0,
comparedScenarioValue: 0,
isProjected: false,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
},
{
year: 2020,
Expand Down Expand Up @@ -431,15 +431,15 @@ export function getComparisonResponseWithProjectedYears(
baseScenarioValue: 0,
comparedScenarioValue: 0,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
isProjected: false,
},
{
year: 2019,
baseScenarioValue: 0,
comparedScenarioValue: 0,
absoluteDifference: 0,
percentageDifference: null,
percentageDifference: 0,
isProjected: false,
},
{
Expand Down

0 comments on commit ebb4359

Please sign in to comment.