Skip to content

Commit

Permalink
Refactor and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 13, 2023
1 parent 687025d commit cf2d56d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 91 deletions.
177 changes: 86 additions & 91 deletions api/src/modules/h3-data/h3-data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
import { SourcingRecord } from 'modules/sourcing-records/sourcing-record.entity';
import { IndicatorRecord } from 'modules/indicator-records/indicator-record.entity';
import { ImpactMaterializedView } from 'modules/impact/views/impact.materialized-view.entity';
import { BaseImpactMap } from './types/base-impact-map.type';
import { BaseImpactMapFiltersType } from './types/base-impact-map.filters.type';

export enum IMPACT_MAP_TYPE {
IMPACT_MAP = 'impact-map',
Expand Down Expand Up @@ -394,21 +396,20 @@ export class H3DataRepository extends Repository<H3Data> {
baseQuery.addSelect('sum(ir.value/ir.scaler)', 'scaled_value');
};

return await this.baseGetImpactMap(
dto.indicatorId,
dto.resolution,
dto.year,
IMPACT_MAP_TYPE.IMPACT_MAP,
false,
dto.materialIds,
dto.originIds,
dto.t1SupplierIds,
dto.producerIds,
dto.businessUnitIds,
dto.locationTypes,
return this.baseGetImpactMap({
indicatorId: dto.indicatorId,
resolution: dto.resolution,
year: dto.year,
mapType: IMPACT_MAP_TYPE.IMPACT_MAP,
isRelative: false,
materialIds: dto.materialIds,
originIds: dto.originIds,
t1SupplierIds: dto.t1SupplierIds,
producerIds: dto.producerIds,
businessUnitIds: dto.businessUnitIds,
locationTypes: dto.locationTypes,
baseQueryExtend,
false,
);
});
}

async getActualVsScenarioImpactMap(
Expand Down Expand Up @@ -450,21 +451,21 @@ export class H3DataRepository extends Repository<H3Data> {
baseQuery.addSelect(sumOnlyActualData, 'sum_actual_data');
};

return await this.baseGetImpactMap(
dto.indicatorId,
dto.resolution,
dto.year,
IMPACT_MAP_TYPE.ACTUAL_VS_SCENARIO,
dto.relative,
dto.materialIds,
dto.originIds,
dto.t1SupplierIds,
dto.producerIds,
dto.businessUnitIds,
dto.locationTypes,
return this.baseGetImpactMap({
indicatorId: dto.indicatorId,
resolution: dto.resolution,
year: dto.year,
mapType: IMPACT_MAP_TYPE.ACTUAL_VS_SCENARIO,
isRelative: dto.relative,
materialIds: dto.materialIds,
originIds: dto.originIds,
t1SupplierIds: dto.t1SupplierIds,
producerIds: dto.producerIds,
businessUnitIds: dto.businessUnitIds,
locationTypes: dto.locationTypes,
baseQueryExtend,
true,
);
scenarioComparisonQuantiles: true,
});
}

async getScenarioVsScenarioImpactMap(
Expand Down Expand Up @@ -505,69 +506,59 @@ export class H3DataRepository extends Repository<H3Data> {
baseQuery.addSelect(sumDataWitComparedScenario, 'sum_compared_scenario');
};

return await this.baseGetImpactMap(
dto.indicatorId,
dto.resolution,
dto.year,
IMPACT_MAP_TYPE.SCENARIO_VS_SCENARIO,
dto.relative,
dto.materialIds,
dto.originIds,
dto.t1SupplierIds,
dto.producerIds,
dto.businessUnitIds,
dto.locationTypes,
return this.baseGetImpactMap({
indicatorId: dto.indicatorId,
resolution: dto.resolution,
year: dto.year,
mapType: IMPACT_MAP_TYPE.SCENARIO_VS_SCENARIO,
isRelative: dto.relative,
materialIds: dto.materialIds,
originIds: dto.originIds,
t1SupplierIds: dto.t1SupplierIds,
producerIds: dto.producerIds,
businessUnitIds: dto.businessUnitIds,
locationTypes: dto.locationTypes,
baseQueryExtend,
true,
);
scenarioComparisonQuantiles: true,
});
}

//TODO Pending refactoring of Quantiles temp table, and aggregation formulas
private async baseGetImpactMap(
indicatorId: string,
resolution: number,
year: number,
mapType: IMPACT_MAP_TYPE,
isRelative?: boolean,
materialIds?: string[],
originIds?: string[],
t1SupplierIds?: string[],
producerIds?: string[],
businessUnitIds?: string[],
locationTypes?: LOCATION_TYPES[],
baseQueryExtend?: (baseQuery: SelectQueryBuilder<any>) => void,
scenarioComparisonQuantiles?: boolean,
baseImpactMap: BaseImpactMap,
): Promise<{ impactMap: H3IndexValueData[]; quantiles: number[] }> {
let baseMapQuery: SelectQueryBuilder<any> = this.baseMapQuery(
indicatorId,
year,
baseImpactMap.indicatorId,
baseImpactMap.year,
);

baseMapQuery = this.addFiltering(
baseMapQuery,
materialIds,
t1SupplierIds,
producerIds,
originIds,
businessUnitIds,
locationTypes,
baseMapQuery = this.addOrganisationalEntityFilters(
{
materialIds: baseImpactMap.materialIds,
originIds: baseImpactMap.originIds,
t1SupplierIds: baseImpactMap.t1SupplierIds,
producerIds: baseImpactMap.producerIds,
businessUnitIds: baseImpactMap.businessUnitIds,
locationTypes: baseImpactMap.locationTypes,
},
{ subQueryBuilder: baseMapQuery },
);

if (baseQueryExtend) {
baseQueryExtend(baseMapQuery);
if (baseImpactMap.baseQueryExtend) {
baseImpactMap.baseQueryExtend(baseMapQuery);
}

const aggregatedResultQuery: SelectQueryBuilder<any> =
this.getAggregatedValuedByH3IndexAndResolution(
baseMapQuery,
resolution,
mapType,
baseImpactMap.resolution,
baseImpactMap.mapType,
);
const finalQueryBuiler: SelectQueryBuilder<any> =
this.dataSource.createQueryBuilder();
if (mapType !== IMPACT_MAP_TYPE.IMPACT_MAP) {
if (mapType === IMPACT_MAP_TYPE.ACTUAL_VS_SCENARIO) {
if (isRelative) {
if (baseImpactMap.mapType !== IMPACT_MAP_TYPE.IMPACT_MAP) {
if (baseImpactMap.mapType === IMPACT_MAP_TYPE.ACTUAL_VS_SCENARIO) {
if (baseImpactMap.isRelative) {
finalQueryBuiler.select(
'100 * (ABS(q.aggregated_scenario_data) - ABS(q.aggregated_actual_data)) / NULLIF(((ABS(q.aggregated_scenario_data) + ABS(q.aggregated_actual_data)) / 2), 0)',
'v',
Expand All @@ -579,8 +570,8 @@ export class H3DataRepository extends Repository<H3Data> {
);
}
}
if (mapType === IMPACT_MAP_TYPE.SCENARIO_VS_SCENARIO) {
if (isRelative) {
if (baseImpactMap.mapType === IMPACT_MAP_TYPE.SCENARIO_VS_SCENARIO) {
if (baseImpactMap.isRelative) {
finalQueryBuiler.select(
'100 * (ABS(q.aggregated_compared) - ABS(q.aggregated_base)) / NULLIF(((ABS(q.aggregated_compared) + ABS(q.aggregated_base)) / 2), 0)',
'v',
Expand All @@ -599,11 +590,11 @@ export class H3DataRepository extends Repository<H3Data> {
const [queryString, params] = baseMapQuery.getQueryAndParameters();

return this.executeQueryAndQuantiles(
mapType === IMPACT_MAP_TYPE.IMPACT_MAP
baseImpactMap.mapType === IMPACT_MAP_TYPE.IMPACT_MAP
? aggregatedResultQuery
: finalQueryBuiler,
params,
scenarioComparisonQuantiles,
baseImpactMap.scenarioComparisonQuantiles,
);
}

Expand Down Expand Up @@ -668,46 +659,50 @@ export class H3DataRepository extends Repository<H3Data> {
);
}

private addFiltering(
subqueryBuilder: SelectQueryBuilder<any>,
materialIds?: string[],
t1SupplierIds?: string[],
producerIds?: string[],
originIds?: string[],
businessUnitIds?: string[],
locationTypes?: string[],
private addOrganisationalEntityFilters(
baseImpactMapFilters: BaseImpactMapFiltersType,
queryBuilder: { subQueryBuilder: SelectQueryBuilder<any> },
): SelectQueryBuilder<any> {
const {
materialIds,
originIds,
businessUnitIds,
producerIds,
t1SupplierIds,
locationTypes,
} = baseImpactMapFilters;
const { subQueryBuilder } = queryBuilder;
if (materialIds) {
subqueryBuilder.andWhere('sl.material IN (:...materialIds)', {
subQueryBuilder.andWhere('sl.material IN (:...materialIds)', {
materialIds,
});
}
if (t1SupplierIds) {
subqueryBuilder.andWhere('sl.t1SupplierId IN (:...t1SupplierIds)', {
subQueryBuilder.andWhere('sl.t1SupplierId IN (:...t1SupplierIds)', {
t1SupplierIds,
});
}
if (producerIds) {
subqueryBuilder.andWhere('sl.producerId IN (:...producerIds)', {
subQueryBuilder.andWhere('sl.producerId IN (:...producerIds)', {
producerIds,
});
}
if (originIds) {
subqueryBuilder.andWhere('sl.adminRegionId IN (:...originIds)', {
subQueryBuilder.andWhere('sl.adminRegionId IN (:...originIds)', {
originIds,
});
}
if (businessUnitIds) {
subqueryBuilder.andWhere('sl.businessUnitId IN (:...businessUnitIds)', {
subQueryBuilder.andWhere('sl.businessUnitId IN (:...businessUnitIds)', {
businessUnitIds,
});
}
if (locationTypes) {
subqueryBuilder.andWhere('sl.locationType IN (:...locationTypes)', {
subQueryBuilder.andWhere('sl.locationType IN (:...locationTypes)', {
locationTypes,
});
}
return subqueryBuilder;
return subQueryBuilder;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions api/src/modules/h3-data/types/base-impact-map.filters.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type BaseImpactMapFiltersType = {
materialIds?: string[];
t1SupplierIds?: string[];
producerIds?: string[];
originIds?: string[];
businessUnitIds?: string[];
locationTypes?: string[];
};
19 changes: 19 additions & 0 deletions api/src/modules/h3-data/types/base-impact-map.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LOCATION_TYPES } from 'modules/sourcing-locations/sourcing-location.entity';
import { SelectQueryBuilder } from 'typeorm';
import { IMPACT_MAP_TYPE } from 'modules/h3-data/h3-data.repository';

export type BaseImpactMap = {
indicatorId: string;
resolution: number;
year: number;
mapType: IMPACT_MAP_TYPE;
isRelative?: boolean;
materialIds?: string[];
originIds?: string[];
t1SupplierIds?: string[];
producerIds?: string[];
businessUnitIds?: string[];
locationTypes?: LOCATION_TYPES[];
baseQueryExtend?: (baseQuery: SelectQueryBuilder<any>) => void;
scenarioComparisonQuantiles?: boolean;
};

0 comments on commit cf2d56d

Please sign in to comment.