Skip to content

Commit

Permalink
chore(indicatorrecord): Create all sourcing records refactor 1st prot…
Browse files Browse the repository at this point in the history
…otype
  • Loading branch information
KevSanchez committed Aug 5, 2022
1 parent 26a6de4 commit 4b15ffa
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export class IndicatorRecordsService extends AppBaseService<
];

const rawData: SourcingRecordsWithIndicatorRawDataDto[] =
await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords();
//await this.indicatorRecordRepository.getIndicatorRawDataForAllSourcingRecords();
await this.impactCalculatorService.calculateAllSourcingRecords();

const calculatedData2: IndicatorRecordCalculatedValuesDto[] = rawData.map(
(sourcingRecordData: SourcingRecordsWithIndicatorRawDataDto) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import {
Connection,
EntityRepository,
getConnection,
QueryRunner,
Repository,
SelectQueryBuilder,
} from 'typeorm';
import { Injectable, Logger } from '@nestjs/common';
import { SaveOptions } from 'typeorm/repository/SaveOptions';
import { MATERIAL_TO_H3_TYPE } from 'modules/materials/material-to-h3.entity';
import { H3DataService } from 'modules/h3-data/h3-data.service';
import { H3Data } from 'modules/h3-data/h3-data.entity';
import { INDICATOR_TYPES } from 'modules/indicators/indicator.entity';
import { SourcingRecordsWithIndicatorRawDataDto } from '../../sourcing-records/dto/sourcing-records-with-indicator-raw-data.dto';
import { SourcingRecordsWithIndicatorRawDataDto } from 'modules/sourcing-records/dto/sourcing-records-with-indicator-raw-data.dto';

@Injectable()
export class ImpactCalculatorService {
logger: Logger = new Logger(this.constructor.name);

constructor(private readonly h3DataService: H3DataService) {}

async calculateImpact(
connection: Connection,
queryRunner: QueryRunner,
georegionId: string,
materialId: string,
year: number,
options?: SaveOptions,
): Promise<any[]> {
// MAIN LOGIC

Expand Down Expand Up @@ -70,9 +63,16 @@ export class ImpactCalculatorService {
*/

const materialH3s: Map<MATERIAL_TO_H3_TYPE, H3Data> =
await this.h3DataService.getAllMaterialH3sByClosestYear(materialId, year);
await this.getAllMaterialH3sByClosestYear(
connection,
queryRunner,
materialId,
year,
);
const indicatorH3s: Map<INDICATOR_TYPES, H3Data> =
await this.h3DataService.getIndicatorH3sByTypeAndClosestYear(
await this.getIndicatorH3sByTypeAndClosestYear(
connection,
queryRunner,
Object.values(INDICATOR_TYPES),
year,
);
Expand Down Expand Up @@ -241,4 +241,88 @@ export class ImpactCalculatorService {

return result;
}

getIndicatorH3sByTypeAndClosestYear(
connecttion: Connection,
queryRunner: QueryRunner,
indicatorTypes: INDICATOR_TYPES[],
year: number,
): Promise<Map<INDICATOR_TYPES, H3Data>> {
return indicatorTypes.reduce(
async (
previousValue: Promise<Map<INDICATOR_TYPES, H3Data>>,
currentIndicatorType: INDICATOR_TYPES,
) => {
const queryBuilder: SelectQueryBuilder<H3Data> = connecttion
.createQueryBuilder()
.select(' h3data.*')
.from(H3Data, 'h3data')
.leftJoin(
'indicator',
'indicator',
'h3data.indicatorId = indicator.id',
)
.where(`indicator.nameCode = '${currentIndicatorType}'`)
.orderBy(`ABS(h3data.year - ${year})`, 'ASC')
.limit(1);

const map: Map<INDICATOR_TYPES, H3Data> = await previousValue;

try {
const result: any = await queryRunner.query(queryBuilder.getQuery());

if (result.length) {
map.set(currentIndicatorType, result[0]);
}
} catch (err) {
console.error(err);
}

return map;
},
Promise.resolve(new Map()),
);
}

getAllMaterialH3sByClosestYear(
connecttion: Connection,
queryRunner: QueryRunner,
materialId: string,
year: number,
): Promise<Map<MATERIAL_TO_H3_TYPE, H3Data>> {
return Object.values(MATERIAL_TO_H3_TYPE).reduce(
async (
previousValue: Promise<Map<MATERIAL_TO_H3_TYPE, H3Data>>,
currentMaterialToH3Type: MATERIAL_TO_H3_TYPE,
) => {
const queryBuilder: SelectQueryBuilder<H3Data> = connecttion
.createQueryBuilder()
.select('h3data.*')
.from(H3Data, 'h3data')
.leftJoin(
'material_to_h3',
'materialsToH3s',
'materialsToH3s.h3DataId = h3data.id',
)
.where(`materialsToH3s.materialId = '${materialId}'`)
.andWhere(`materialsToH3s.type = '${currentMaterialToH3Type}'`)
.orderBy(`ABS(h3data.year - ${year})`, 'ASC')
.limit(1);

const map: Map<MATERIAL_TO_H3_TYPE, H3Data> = await previousValue;
try {
const result: any = await queryRunner.query(queryBuilder.getQuery());

if (result.length) {
map.set(currentMaterialToH3Type, result[0]);
}
} catch (err) {
console.error(err);
}

return map;
},
Promise.resolve(new Map()),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ describe('Indicator Records Service', () => {
indicatorPreconditions.carbonEmissions,
materialH3DataProducer1,
indicatorPreconditions.sourcingRecord1.id,
29.788819307125873,
29.788819875776397,
1610,
);
await checkCreatedIndicatorRecord(
Expand Down Expand Up @@ -527,7 +527,7 @@ describe('Indicator Records Service', () => {
indicatorPreconditions.carbonEmissions,
materialH3DataProducer2,
indicatorPreconditions.sourcingRecord2.id,
14.894409653562937,
14.894409937888199,
1610,
);
await checkCreatedIndicatorRecord(
Expand All @@ -538,7 +538,7 @@ describe('Indicator Records Service', () => {
0.7700000181794167,
1610,
);
});
}, 100000000);

test("When creating indicators without provided coefficients and the material has H3 data, the raw values for the calculations should be read from the cache if they're already present on the CachedData", async () => {
//ARRANGE
Expand Down

0 comments on commit 4b15ffa

Please sign in to comment.