From feaa3249a99a4db9959128c4d1a39f3b1aff621c Mon Sep 17 00:00:00 2001 From: KevSanchez Date: Tue, 30 Aug 2022 13:22:10 +0200 Subject: [PATCH] chore(indicatorrecord): Adds a feature toggle to indicator record calculation Now, wether the old stored functions, or the new API generated SQL queries are used for the calculations of indicator records at import time, is toggable via environment variable, SIMPLE_IMPORT_CALCULATIONS --- api/config/custom-environment-variables.json | 3 +++ api/config/default.json | 3 +++ api/config/test.json | 3 +++ .../indicator-records/indicator-records.service.ts | 8 +++++--- .../indicator-record/indicator-records.service.spec.ts | 8 +++++--- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api/config/custom-environment-variables.json b/api/config/custom-environment-variables.json index befeab6317..6433676e18 100644 --- a/api/config/custom-environment-variables.json +++ b/api/config/custom-environment-variables.json @@ -48,5 +48,8 @@ }, "map": { "distributed": "DISTRIBUTED_MAP" + }, + "featureFlags": { + "simpleImportCalculations": "SIMPLE_IMPORT_CALCULATIONS" } } diff --git a/api/config/default.json b/api/config/default.json index a23b7f38f5..20febeefc8 100644 --- a/api/config/default.json +++ b/api/config/default.json @@ -57,5 +57,8 @@ "map": { "distributed": true + }, + "featureFlags": { + "simpleImportCalculations": "false" } } diff --git a/api/config/test.json b/api/config/test.json index c0d1e2270d..01612cc7ff 100644 --- a/api/config/test.json +++ b/api/config/test.json @@ -24,5 +24,8 @@ }, "geolocation": { "gmapsApiKey": "myVeryBadJWTSecretForTests" + }, + "featureFlags": { + "simpleImportCalculations": "true" } } diff --git a/api/src/modules/indicator-records/indicator-records.service.ts b/api/src/modules/indicator-records/indicator-records.service.ts index cfb2eae2ad..3ab626b76d 100644 --- a/api/src/modules/indicator-records/indicator-records.service.ts +++ b/api/src/modules/indicator-records/indicator-records.service.ts @@ -166,9 +166,11 @@ export class IndicatorRecordsService extends AppBaseService< INDICATOR_TYPES.BIODIVERSITY_LOSS, ]; - const rawData: SourcingRecordsWithIndicatorRawDataDto[] = - //await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords(); - await this.impactCalculatorService.calculateAllSourcingRecords(); + const rawData: SourcingRecordsWithIndicatorRawDataDto[] = config.get( + 'featureFlags.simpleImportCalculations', + ) + ? await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords() + : await this.impactCalculatorService.calculateAllSourcingRecords(); const calculatedData2: IndicatorRecordCalculatedValuesDto[] = rawData.map( (sourcingRecordData: SourcingRecordsWithIndicatorRawDataDto) => { diff --git a/api/test/integration/indicator-record/indicator-records.service.spec.ts b/api/test/integration/indicator-record/indicator-records.service.spec.ts index 4bce4bea64..12b7f4ba66 100644 --- a/api/test/integration/indicator-record/indicator-records.service.spec.ts +++ b/api/test/integration/indicator-record/indicator-records.service.spec.ts @@ -817,8 +817,10 @@ describe('Indicator Records Service', () => { expect(indicatorRecords.length).toEqual(1); expect(indicatorRecords[0].sourcingRecordId).toEqual(sourcingRecordId); expect(indicatorRecords[0].status).toEqual(INDICATOR_RECORD_STATUS.SUCCESS); - expect(indicatorRecords[0].value).toEqual(recordValue); - expect(indicatorRecords[0].scaler).toEqual(scalerValue); + expect(indicatorRecords[0].value).toBeCloseTo(recordValue, 5); + if (scalerValue) { + expect(indicatorRecords[0].scaler).toBeCloseTo(scalerValue, 5); + } expect(indicatorRecords[0].materialH3DataId).toEqual( materialH3Data.h3DataId, ); @@ -843,7 +845,7 @@ describe('Indicator Records Service', () => { expect(cachedData).toBeDefined(); expect(cachedData?.data).toBeDefined(); expect(cachedData?.type).toEqual(type); - expect((cachedData?.data as CachedRawValue).rawValue).toEqual(value); + expect((cachedData?.data as CachedRawValue).rawValue).toBeCloseTo(value, 5); } async function createPreconditions(): Promise {