Skip to content

Commit

Permalink
Simplify provided coefficient calculus
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 11, 2023
1 parent cce5c2f commit 1abeb2d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ LANGUAGE plpgsql;

await queryRunner.query(`
CREATE OR REPLACE FUNCTION get_indicator_coefficient_impact(
nameCode text,
adminRegionId uuid,
name_code text,
admin_region_id uuid,
material_id uuid)
RETURNS float AS
$$
Expand All @@ -69,7 +69,7 @@ LANGUAGE plpgsql;
--get indicatorId
SELECT "id" INTO indicator_id FROM "indicator"
WHERE "nameCode" = nameCode;
WHERE "nameCode" = name_code;
-- get water footprint value by location, material and indicator
EXECUTE format(
Expand All @@ -93,7 +93,7 @@ LANGUAGE plpgsql;
)
) AS value;'
)
USING adminRegionId, material_id, indicator_id
USING admin_region_id, material_id, indicator_id
INTO value;
RETURN value;
END;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ export class IndicatorRecordCalculatedValuesDto {
sourcingRecordId: string;
production: number;
materialH3DataId: string;

landPerTon: number;
landUse: number;
values: Map<INDICATOR_NAME_CODES, number>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,59 +268,13 @@ export class ImpactCalculator {
calculatedIndicatorValues.sourcingRecordId = sourcingData.sourcingRecordId;
calculatedIndicatorValues.materialH3DataId = materialH3DataId;
calculatedIndicatorValues.values = new Map<INDICATOR_NAME_CODES, number>();
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.LF,
newIndicatorCoefficients[INDICATOR_NAME_CODES.LF] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.DF_SLUC,
newIndicatorCoefficients[INDICATOR_NAME_CODES.DF_SLUC] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.GHG_DEF_SLUC,
newIndicatorCoefficients[INDICATOR_NAME_CODES.GHG_DEF_SLUC] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.WU,
newIndicatorCoefficients[INDICATOR_NAME_CODES.WU] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.NL,
newIndicatorCoefficients[INDICATOR_NAME_CODES.NL] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.NCE,
newIndicatorCoefficients[INDICATOR_NAME_CODES.NCE] *
sourcingData.tonnage || 0,
);
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.FLIL,
newIndicatorCoefficients[INDICATOR_NAME_CODES.FLIL] *
sourcingData.tonnage || 0,
);
const waterUseValue: number = calculatedIndicatorValues.values.get(
INDICATOR_NAME_CODES.WU,
)!;
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.UWU,
waterUseValue *
newIndicatorCoefficients[INDICATOR_NAME_CODES.UWU] *
sourcingData.tonnage,
);
const nutrientLoad: number = calculatedIndicatorValues.values.get(
INDICATOR_NAME_CODES.NL,
)!;
calculatedIndicatorValues.values.set(
INDICATOR_NAME_CODES.ENL,
nutrientLoad *
newIndicatorCoefficients[INDICATOR_NAME_CODES.ENL] *
sourcingData.tonnage || 0,
);
Object.keys(INDICATOR_NAME_CODES).forEach((nameCode: string) => {
calculatedIndicatorValues.values.set(
nameCode as INDICATOR_NAME_CODES,
newIndicatorCoefficients[nameCode as INDICATOR_NAME_CODES] *
sourcingData.tonnage || 0,
);
});

return calculatedIndicatorValues;
}
Expand Down

0 comments on commit 1abeb2d

Please sign in to comment.