Skip to content

Commit

Permalink
WIP - Generate comparison reports, add endpoint for actual vs scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 20, 2023
1 parent acd0f81 commit 3a8e847
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
33 changes: 33 additions & 0 deletions api/src/modules/impact/impact-report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { SetScenarioIdsInterceptor } from 'modules/impact/set-scenario-ids.inter
import { ImpactReportService } from 'modules/impact/reports/impact.report';
import { Response } from 'express';
import { GetImpactTableDto } from 'modules/impact/dto/impact-table.dto';
import { CheckUserOwnsScenario } from 'modules/authorization/formodule/scenario-ownership.interceptor';
import { actualVsScenario } from './reports/mocks';

@Controller('/api/v1/impact')
@ApiTags('Impact')
Expand Down Expand Up @@ -47,4 +49,35 @@ export class ImpactReportController {
);
res.send(report);
}

@ApiOperation({
description:
'Get a Actual Vs Scenario Impact Table CSV Report for a given scenario',
})
@CheckUserOwnsScenario({
bypassIfScenarioIsPublic: true,
isComparisonMode: true,
})
@UseInterceptors(SetScenarioIdsInterceptor)
@Get('compare/scenario/vs/actual/report')
async getActualVsScenarioImpactTable(
@Query(ValidationPipe)
actualVsScenarioImpactTableDto: any,
@Res() res: Response,
): Promise<void> {
// const { data } =
// await this.actualVsScenarioImpactService.getActualVsScenarioImpactTable(
// actualVsScenarioImpactTableDto,
// { disablePagination: true },
// );
const report: string = await this.impactReports.generateImpactReport(
actualVsScenario,
);
res.setHeader('Content-Type', 'text/csv');
res.setHeader(
'Content-Disposition',
'attachment; filename=impact_report.csv',
);
res.send(report);
}
}
48 changes: 39 additions & 9 deletions api/src/modules/impact/reports/impact.report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { Inject, Injectable } from '@nestjs/common';
import { ICSVReportService } from 'modules/reports/report-service.interface';
import { ReportServiceToken } from 'modules/reports/reports.module';
import {
AnyImpactTableRows,
AnyImpactTableRowsValues,
ImpactTableDataByIndicator,
ImpactTableRows,
ImpactTableRowsValues,
} from 'modules/impact/dto/response-impact-table.dto';
import { ImpactTableCSVReport } from 'modules/impact/reports/types';
import { ParserOptions } from '@json2csv/plainjs';
import { GROUP_BY_VALUES } from 'modules/impact/dto/impact-table.dto';

const IndicatorColumnKey: string = 'Indicator';
const GroupByColumnKey: string = 'Group by';
const AbsoluteDifferenceColumnKey: string = 'Absolute Difference';
const PercentageDifferenceColumnKey: string = 'Percentage Difference';

@Injectable()
export class ImpactReportService {
constructor(
Expand Down Expand Up @@ -49,30 +55,49 @@ export class ImpactReportService {
}

private processNode(
node: ImpactTableRows,
node: AnyImpactTableRows,
indicatorName:
| Pick<ImpactTableDataByIndicator, 'indicatorShortName'>
| string,
groupBy: GROUP_BY_VALUES,
unit: string,
accumulator: ImpactTableCSVReport[],
): void {
const groupName: string = `Group by ${groupBy}`;
const groupName: string = `${GroupByColumnKey} ${groupBy}`;
const resultObject: ImpactTableCSVReport = {
[`Indicator`]: `${indicatorName} (${unit})`,
[groupName]: node.name,
};
node.values.forEach((nodeValue: ImpactTableRowsValues) => {
node.values.forEach((nodeValue: AnyImpactTableRowsValues) => {
let yearKey: string = nodeValue.year.toString();
if (nodeValue.isProjected) {
yearKey = yearKey.concat(' (projected)');
}
resultObject[yearKey] = nodeValue.value;
if ('baseScenarioValue' in nodeValue) {
resultObject[`${yearKey} (base Scenario)`] =
nodeValue.baseScenarioValue;
resultObject[`${yearKey} (compared Scenario)`] =
nodeValue.comparedScenarioValue;
} else if ('comparedScenarioValue' in nodeValue) {
resultObject[yearKey] = nodeValue.value;
resultObject[`${yearKey} (compared Scenario)`] =
nodeValue.comparedScenarioValue;
} else {
resultObject[yearKey] = nodeValue.value;
}
if ('absoluteDifference' in nodeValue) {
resultObject[AbsoluteDifferenceColumnKey] =
nodeValue.absoluteDifference;
}
if ('percentageDifference' in nodeValue) {
resultObject[PercentageDifferenceColumnKey] =
nodeValue.percentageDifference;
}
});
accumulator.push(resultObject);

if (node.children && node.children.length) {
node.children.forEach((children: ImpactTableRows) => {
node.children.forEach((children: AnyImpactTableRows) => {
this.processNode(children, indicatorName, groupBy, unit, accumulator);
});
}
Expand All @@ -82,15 +107,20 @@ export class ImpactReportService {
const indicatorKey: string[] = [];
const groupByKey: string[] = [];
const yearKeys: string[] = [];
const differenceKeys: string[] = [];
Object.keys(dataSample).forEach((key: string) => {
if (key.includes('Indicator')) {
if (key.includes(IndicatorColumnKey)) {
groupByKey.push(key);
} else if (key.includes('Group by')) {
} else if (key.includes(GroupByColumnKey)) {
groupByKey.push(key);
} else if (key.includes(AbsoluteDifferenceColumnKey)) {
differenceKeys.push(key);
} else if (key.includes(PercentageDifferenceColumnKey)) {
differenceKeys.push(key);
} else {
yearKeys.push(key);
}
});
return [...indicatorKey, ...groupByKey, ...yearKeys];
return [...indicatorKey, ...groupByKey, ...yearKeys, ...differenceKeys];
}
}

0 comments on commit 3a8e847

Please sign in to comment.