Skip to content

Commit

Permalink
Refactor Indicator namecodes, delete sat inds.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 28, 2023
1 parent c3c3436 commit 097b9c7
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,69 @@ export class IndicatorCoefficientsDto {
@Max(1000000)
@Min(0)
@IsNumber()
[INDICATOR_NAME_CODES.WATER_USE]: number;
[INDICATOR_NAME_CODES.LF]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE]: number;
[INDICATOR_NAME_CODES.DF_SLUC]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.CLIMATE_RISK]: number;
[INDICATOR_NAME_CODES.GHG_DEF_SLUC]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.DEFORESTATION_RISK]: number;
[INDICATOR_NAME_CODES.GHG_FARM]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.LAND_USE]: number;
[INDICATOR_NAME_CODES.UWU]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION]: number;
[INDICATOR_NAME_CODES.WU]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION_RISK]: number;
[INDICATOR_NAME_CODES.NL]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.NATURAL_ECOSYSTEM_CONVERSION_RISK]: number;
[INDICATOR_NAME_CODES.ENL]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.WATER_QUALITY]: number;
[INDICATOR_NAME_CODES.NCE]: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,48 +60,45 @@ export const INDICATOR_NAME_CODE_TO_QUERY_MAP: {
[key in ImpactQueryPropertyName]?: ImpactPropertyToQueryFunction;
};
} = {
[INDICATOR_NAME_CODES.LAND_USE]: {
[INDICATOR_NAME_CODES.LF]: {
harvest: () =>
`sum_material_over_georegion($1, $2, 'harvest') as "${QueryPropertyNames.harvest}"`,
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
},
[INDICATOR_NAME_CODES.DEFORESTATION_RISK]: {
[INDICATOR_NAME_CODES.DF_SLUC]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
rawDeforestation: (nameCode: INDICATOR_NAME_CODES) =>
`get_annual_landscape_impact_over_georegion($1, '${nameCode}', $2, 'producer') as "${QueryPropertyNames.rawDeforestation}"`,
},
[INDICATOR_NAME_CODES.CLIMATE_RISK]: {
[INDICATOR_NAME_CODES.GHG_DEF_SLUC]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
rawClimateRisk: (nameCode: INDICATOR_NAME_CODES) =>
`get_annual_landscape_impact_over_georegion($1,'${nameCode}', $2, 'producer') as "${QueryPropertyNames.rawClimateRisk}"`,
},
[INDICATOR_NAME_CODES.WATER_QUALITY]: {
[INDICATOR_NAME_CODES.GHG_FARM]: {
rawWaterQuality: (nameCode: INDICATOR_NAME_CODES) =>
`get_indicator_coefficient_impact('${nameCode}', $3, $2) as "${QueryPropertyNames.rawWaterQuality}"`,
},
[INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE]: {
[INDICATOR_NAME_CODES.UWU]: {
rawUnsustainableWaterUse: (nameCode: INDICATOR_NAME_CODES) =>
`get_percentage_water_stress_area($1, '${nameCode}') as "${QueryPropertyNames.rawUnsustainableWaterUse}"`,
},
[INDICATOR_NAME_CODES.NATURAL_ECOSYSTEM_CONVERSION_RISK]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
rawNaturalConversion: (nameCode: INDICATOR_NAME_CODES) =>
`get_annual_landscape_impact_over_georegion($1, '${nameCode}', $2, 'producer') as "${QueryPropertyNames.rawNaturalConversion}"`,
[INDICATOR_NAME_CODES.WU]: {
rawWaterUse: (nameCode: INDICATOR_NAME_CODES) => 'to be implemented',
},
[INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION]: {
satDeforestation: () =>
` sum_satelligence_deforestation_over_georegion($1) as "${QueryPropertyNames.satDeforestation}"`,
[INDICATOR_NAME_CODES.NL]: {
rawNaturalConversion: (nameCode: INDICATOR_NAME_CODES) =>
'to be implemented',
},
[INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION_RISK]: {
satDeforestationRisk: () =>
`sum_satelligence_deforestation_risk_over_georegion($1) as "${QueryPropertyNames.satDeforestationRisk}"`,
[INDICATOR_NAME_CODES.ENL]: {
rawNaturalConversion: (nameCode: INDICATOR_NAME_CODES) =>
'to be implemented',
},
[INDICATOR_NAME_CODES.WATER_USE]: {
rawWaterUse: (nameCode: INDICATOR_NAME_CODES) =>
`get_indicator_coefficient_impact('${nameCode}', $3, $2) as "${QueryPropertyNames.rawWaterUse}"`,
[INDICATOR_NAME_CODES.NCE]: {
rawNaturalConversion: (nameCode: INDICATOR_NAME_CODES) =>
'to be implemented',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -271,38 +271,32 @@ export class ImpactCalculator {
calculatedIndicatorValues.materialH3DataId = materialH3DataId;
calculatedIndicatorValues.values = new Map<INDICATOR_NAME_CODES, number>();
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.LAND_USE,
newIndicatorCoefficients[INDICATOR_NAME_CODES.LAND_USE] *
INDICATOR_NAME_CODES.LF,
newIndicatorCoefficients[INDICATOR_NAME_CODES.LF] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.DEFORESTATION_RISK,
newIndicatorCoefficients[INDICATOR_NAME_CODES.DEFORESTATION_RISK] *
INDICATOR_NAME_CODES.DF_SLUC,
newIndicatorCoefficients[INDICATOR_NAME_CODES.DF_SLUC] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.CLIMATE_RISK,
newIndicatorCoefficients[INDICATOR_NAME_CODES.CLIMATE_RISK] *
INDICATOR_NAME_CODES.GHG_DEF_SLUC,
newIndicatorCoefficients[INDICATOR_NAME_CODES.GHG_DEF_SLUC] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.WATER_USE,
newIndicatorCoefficients[INDICATOR_NAME_CODES.WATER_USE] *
INDICATOR_NAME_CODES.WU,
newIndicatorCoefficients[INDICATOR_NAME_CODES.WU] *
sourcingData.tonnage || 0,
);

// TODO: We need to ignore satelligence indicators from being affected by a coefficient that a user can send
// updating the model will be required for this, as by default any indicator that is active will be shown
// in the UI, also for sending coefficients in intervention calculation

// Depends on water use indicator's final value
const waterUseValue: number = calculatedIndicatorValues.values.get(
INDICATOR_NAME_CODES.WATER_USE,
INDICATOR_NAME_CODES.WU,
)!;
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE,
INDICATOR_NAME_CODES.UWU,
waterUseValue *
newIndicatorCoefficients[INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE] *
newIndicatorCoefficients[INDICATOR_NAME_CODES.UWU] *
sourcingData.tonnage,
);

Expand Down Expand Up @@ -350,27 +344,16 @@ export class ImpactCalculator {
const waterQuality: number = rawData.rawWaterQuality * tonnage;

const map: Map<INDICATOR_NAME_CODES, number> = new Map();
map.set(INDICATOR_NAME_CODES.DEFORESTATION_RISK, deforestation);
map.set(INDICATOR_NAME_CODES.CLIMATE_RISK, climateRisk);
map.set(
INDICATOR_NAME_CODES.NATURAL_ECOSYSTEM_CONVERSION_RISK,
naturalRisk,
);
map.set(
INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE,
unsustainableWaterUse,
);
map.set(INDICATOR_NAME_CODES.WATER_USE, waterUse);
map.set(INDICATOR_NAME_CODES.WATER_QUALITY, waterQuality);
map.set(INDICATOR_NAME_CODES.LAND_USE, landUse);
map.set(
INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION,
rawData.satDeforestation,
);
map.set(
INDICATOR_NAME_CODES.SATELLIGENCE_DEFORESTATION_RISK,
rawData.satDeforestationRisk,
);
map.set(INDICATOR_NAME_CODES.LF, landUse);
map.set(INDICATOR_NAME_CODES.DF_SLUC, deforestation);
map.set(INDICATOR_NAME_CODES.GHG_DEF_SLUC, climateRisk);
map.set(INDICATOR_NAME_CODES.GHG_FARM, naturalRisk);
map.set(INDICATOR_NAME_CODES.UWU, unsustainableWaterUse);
map.set(INDICATOR_NAME_CODES.WU, waterUse);
map.set(INDICATOR_NAME_CODES.WU, waterQuality);
map.set(INDICATOR_NAME_CODES.NL, naturalRisk);
map.set(INDICATOR_NAME_CODES.ENL, naturalRisk);
map.set(INDICATOR_NAME_CODES.NCE, naturalRisk);

return map;
}
Expand Down
18 changes: 9 additions & 9 deletions api/src/modules/indicators/indicator.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export enum INDICATOR_STATUS {
}

export enum INDICATOR_NAME_CODES {
LAND_USE = 'LI',
DEFORESTATION_RISK = 'DF_LUC_T',
CLIMATE_RISK = 'GHG_LUC_T',
WATER_USE = 'UWU_T',
UNSUSTAINABLE_WATER_USE = 'UWUSR_T',
NATURAL_ECOSYSTEM_CONVERSION_RISK = 'NECR',
WATER_QUALITY = 'WQ',
SATELLIGENCE_DEFORESTATION = 'SAT_DF',
SATELLIGENCE_DEFORESTATION_RISK = 'SAT_DF_R',
LF = 'LF',
DF_SLUC = 'DF_SLUC',
GHG_DEF_SLUC = 'GHG_DEF_SLUC',
WU = 'WU',
UWU = 'UWU',
NL = 'NL',
GHG_FARM = 'GHG_FARM',
ENL = 'ENL',
NCE = 'NCE',
}

export const indicatorResource: BaseServiceResource = {
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/indicators/indicators.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class IndicatorsService extends AppBaseService<

const deforestationIndicator: Indicator | null =
await this.indicatorRepository.findOne({
where: { nameCode: INDICATOR_NAME_CODES.DEFORESTATION_RISK },
where: { nameCode: INDICATOR_NAME_CODES.DF_SLUC },
});
if (!deforestationIndicator)
throw new NotFoundException(
Expand Down
4 changes: 2 additions & 2 deletions api/test/e2e/authorization/authorization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Authorization Test (E2E)', () => {
.expect(HttpStatus.OK);

const indicator = await createIndicator({
nameCode: INDICATOR_NAME_CODES.DEFORESTATION_RISK,
nameCode: INDICATOR_NAME_CODES.DF_SLUC,
});

await request(testApplication.getHttpServer())
Expand All @@ -101,7 +101,7 @@ describe('Authorization Test (E2E)', () => {
.expect(HttpStatus.FORBIDDEN);

const indicator = await createIndicator({
nameCode: INDICATOR_NAME_CODES.UNSUSTAINABLE_WATER_USE,
nameCode: INDICATOR_NAME_CODES.UWU,
});

await request(testApplication.getHttpServer())
Expand Down
6 changes: 3 additions & 3 deletions api/test/e2e/impact/impact-chart/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ describe('Impact Chart (Ranking) Test Suite (e2e)', () => {
const indicator: Indicator = await createIndicator({
name: 'Fake Indicator',
unit,
nameCode: INDICATOR_NAME_CODES.DEFORESTATION_RISK,
nameCode: INDICATOR_NAME_CODES.DF_SLUC,
});

const indicator2: Indicator = await createIndicator({
name: 'Fake Indicator 2',
unit,
nameCode: INDICATOR_NAME_CODES.CLIMATE_RISK,
nameCode: INDICATOR_NAME_CODES.GHG_DEF_SLUC,
});

const businessUnit: BusinessUnit = await createBusinessUnit({
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('Impact Chart (Ranking) Test Suite (e2e)', () => {
const indicator: Indicator = await createIndicator({
name: 'Fake Indicator',
unit,
nameCode: INDICATOR_NAME_CODES.DEFORESTATION_RISK,
nameCode: INDICATOR_NAME_CODES.DF_SLUC,
});

const businessUnit: BusinessUnit = await createBusinessUnit({
Expand Down
Loading

0 comments on commit 097b9c7

Please sign in to comment.