-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate base Impact table csv report
- Loading branch information
Showing
15 changed files
with
323 additions
and
36 deletions.
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,17 +1,102 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { IReportService } from 'modules/reports/report-service.interface'; | ||
import { ICSVReportService } from 'modules/reports/report-service.interface'; | ||
import { ReportServiceToken } from 'modules/reports/reports.module'; | ||
import { | ||
ImpactTableDataByIndicator, | ||
ImpactTableRows, | ||
} from 'modules/impact/dto/response-impact-table.dto'; | ||
import { ImpactTableCSVReport } from 'modules/impact/table-reports/types'; | ||
import { ParserOptions } from '@json2csv/plainjs'; | ||
import { GROUP_BY_VALUES } from 'modules/impact/dto/impact-table.dto'; | ||
|
||
@Injectable() | ||
export class ImpactReportService { | ||
constructor( | ||
@Inject(ReportServiceToken) private reportService: IReportService, | ||
@Inject(ReportServiceToken) private reportService: ICSVReportService, | ||
) {} | ||
|
||
async generateImpactReport(data: any): Promise<string> { | ||
const parserOptions: { fields: ['line', 'error'] } = { | ||
fields: ['line', 'error'], | ||
async generateImpactReport( | ||
data: ImpactTableDataByIndicator[], | ||
): Promise<string> { | ||
const parsedData: ImpactTableCSVReport[] = this.parseToCSVShape(data); | ||
const columnOrder: Pick<ParserOptions, 'fields'> = { | ||
fields: this.getColumnOrder(parsedData[0]), | ||
}; | ||
return this.reportService.generateReport(data, {}); | ||
|
||
return this.reportService.generateCSVReport(parsedData, columnOrder); | ||
} | ||
|
||
private parseToCSVShape( | ||
data: ImpactTableDataByIndicator[], | ||
): ImpactTableCSVReport[] { | ||
const results: ImpactTableCSVReport[] = []; | ||
|
||
data.forEach((indicator: ImpactTableDataByIndicator) => { | ||
const unit: string = indicator.metadata.unit; | ||
indicator.rows.forEach((row: ImpactTableRows) => { | ||
this.processNode( | ||
row, | ||
indicator.indicatorShortName, | ||
indicator.groupBy, | ||
unit, | ||
results, | ||
); | ||
}); | ||
}); | ||
|
||
return results; | ||
} | ||
|
||
private processNode( | ||
node: ImpactTableRows, | ||
indicatorName: | ||
| Pick<ImpactTableDataByIndicator, 'indicatorShortName'> | ||
| string, | ||
groupBy: GROUP_BY_VALUES, | ||
unit: string, | ||
accumulator: ImpactTableCSVReport[], | ||
): void { | ||
const groupName: string = `Group by ${groupBy}`; | ||
const targets: ImpactTableRows[] = | ||
node.children && node.children.length > 0 ? node.children : [node]; | ||
|
||
targets.forEach((target: any) => { | ||
const resultObject: any = { | ||
[`Indicator`]: `${indicatorName} (${unit})`, | ||
[groupName]: target.name, | ||
}; | ||
|
||
if (target.values && target.values.length > 0) { | ||
target.values.forEach((value: any) => { | ||
let yearKey: string = value.year.toString(); | ||
if (value.isProjected) { | ||
yearKey = yearKey.concat(' (projected)'); | ||
} | ||
resultObject[yearKey] = value.value; | ||
}); | ||
|
||
accumulator.push(resultObject); | ||
} | ||
|
||
if (target.children && target.children.length > 0) { | ||
this.processNode(target, indicatorName, groupBy, unit, accumulator); | ||
} | ||
}); | ||
} | ||
|
||
private getColumnOrder(dataSample: ImpactTableCSVReport): string[] { | ||
const indicatorKey: string[] = []; | ||
const groupByKey: string[] = []; | ||
const yearKeys: string[] = []; | ||
Object.keys(dataSample).forEach((key: string) => { | ||
if (key.includes('Indicator')) { | ||
groupByKey.push(key); | ||
} else if (key.includes('Group by')) { | ||
groupByKey.push(key); | ||
} else { | ||
yearKeys.push(key); | ||
} | ||
}); | ||
return [...indicatorKey, ...groupByKey, ...yearKeys]; | ||
} | ||
} |
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
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,14 @@ | ||
type YearData = { | ||
[key: string]: string | number; | ||
}; | ||
|
||
type indicatorField = { | ||
Indicator: string; | ||
}; | ||
type DynamicGroupByField = { | ||
[key: `Group by ${string}`]: string; | ||
}; | ||
|
||
export type ImpactTableCSVReport = DynamicGroupByField & | ||
YearData & | ||
indicatorField; |
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export interface IReportService { | ||
generateReport(data: any, options: object): Promise<string>; | ||
import { ParserOptions } from '@json2csv/plainjs'; | ||
|
||
export interface ICSVReportService { | ||
generateCSVReport(data: any, options: ParserOptions): Promise<string>; | ||
} |
Oops, something went wrong.