Skip to content

Commit

Permalink
chore(ImpactCalculator): Polished new service to calculate raw values…
Browse files Browse the repository at this point in the history
… for all sourcing records

The prototpye for for calculating all raw values for all sourcing records has revamped/polished to final status, with approval pending once tested. Also the the feature flag selection has been moved to sourcing data import along with a bit of refactoring.
  • Loading branch information
KevSanchez committed Sep 2, 2022
1 parent 5a12d1b commit 0659af0
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { GeoCodingAbstractClass } from 'modules/geo-coding/geo-coding-abstract-c
import { MissingH3DataError } from 'modules/indicator-records/errors/missing-h3-data.error';
import { TasksService } from 'modules/tasks/tasks.service';
import { IndicatorRecord } from 'modules/indicator-records/indicator-record.entity';
import * as config from 'config';

export interface LocationData {
locationAddressInput?: string;
Expand Down Expand Up @@ -139,7 +140,10 @@ export class SourcingDataImportService {
// Getting H3 data for calculations is done within DB so we need to improve the error handling
// TBD: What to do when there is no H3 for a Material
try {
await this.indicatorRecordsService.createIndicatorRecordsForAllSourcingRecords();
// TODO remove feature flag selection, once the solution has been approved
config.get('featureFlags.simpleImportCalculations')
? await this.indicatorRecordsService.createIndicatorRecordsForAllSourcingRecordsV2()
: await this.indicatorRecordsService.createIndicatorRecordsForAllSourcingRecords();
this.logger.log('Indicator Records generated');
// TODO: Hack to force m.view refresh once Indicator Records are persisted. This should be automagically
// done by the AfterInser() event listener placed in indicator-record.entity.ts
Expand Down
56 changes: 51 additions & 5 deletions api/src/modules/indicator-records/indicator-records.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,8 @@ export class IndicatorRecordsService extends AppBaseService<
INDICATOR_TYPES.BIODIVERSITY_LOSS,
];

const rawData: SourcingRecordsWithIndicatorRawDataDto[] = config.get(
'featureFlags.simpleImportCalculations',
)
? await this.impactCalculatorService.calculateAllSourcingRecords()
: await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords();
const rawData: SourcingRecordsWithIndicatorRawDataDto[] =
await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords();

const calculatedData2: IndicatorRecordCalculatedValuesDto[] = rawData.map(
(sourcingRecordData: SourcingRecordsWithIndicatorRawDataDto) => {
Expand Down Expand Up @@ -226,6 +223,55 @@ export class IndicatorRecordsService extends AppBaseService<
await this.indicatorRecordRepository.saveChunks(indicatorRecords);
}

async createIndicatorRecordsForAllSourcingRecordsV2(): Promise<void> {
//Calculate raw impact Data for all available indicators on the system
const indicators: Indicator[] =
await this.indicatorService.getAllIndicators();

const rawData: SourcingRecordsWithIndicatorRawDataDto[] =
await this.impactCalculatorService.calculateAllRawValuesForAllSourcingRecords(
indicators,
);

const calculatedData: IndicatorRecordCalculatedValuesDto[] = rawData.map(
(sourcingRecordData: SourcingRecordsWithIndicatorRawDataDto) => {
// Small DTO transformation for calculation method
const indicatorComputedRawDataDto: IndicatorComputedRawDataDto = {
harvestedArea: sourcingRecordData.harvestedArea,
production: sourcingRecordData.production,
indicatorValues: sourcingRecordData.indicatorValues,
};

return this.calculateIndicatorValues(
sourcingRecordData.sourcingRecordId,
sourcingRecordData.tonnage,
sourcingRecordData.materialH3DataId,
indicatorComputedRawDataDto,
);
},
);

// Create IndicatorRecord instances
const indicatorRecords: IndicatorRecord[] = [];
for (const calculatedIndicatorRecords of calculatedData) {
for (const indicator of indicators) {
indicatorRecords.push(
IndicatorRecord.merge(new IndicatorRecord(), {
value: calculatedIndicatorRecords.values.get(
indicator.nameCode as INDICATOR_TYPES,
),
indicatorId: indicator.id,
status: INDICATOR_RECORD_STATUS.SUCCESS,
sourcingRecordId: calculatedIndicatorRecords.sourcingRecordId,
scaler: calculatedIndicatorRecords.production,
materialH3DataId: calculatedIndicatorRecords.materialH3DataId,
}),
);
}
}
await this.indicatorRecordRepository.saveChunks(indicatorRecords);
}

/**
* @description Creates Indicator-Records for a single Sourcing-Record, by first retrieving Raw Indicator data from the DB, then applying
* the methodology and persist new Indicator Records
Expand Down
Loading

0 comments on commit 0659af0

Please sign in to comment.