Skip to content

Commit

Permalink
Add ghg farm indicator calc.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 16, 2023
1 parent 5559b1a commit a50f5b7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ export class IndicatorCoefficientsDto {
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.ENL]: number;

@ApiPropertyOptional()
@IsOptional()
@Max(1000000)
@Min(0)
@IsNotEmpty()
@IsNumber()
[INDICATOR_NAME_CODES.GHG_FARM]: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class QueryPropertyNamesType {
[INDICATOR_NAME_CODES.NL]: INDICATOR_NAME_CODES.NL;
[INDICATOR_NAME_CODES.NCE]: INDICATOR_NAME_CODES.NCE;
[INDICATOR_NAME_CODES.FLIL]: INDICATOR_NAME_CODES.FLIL;
[INDICATOR_NAME_CODES.GHG_FARM]: INDICATOR_NAME_CODES.GHG_FARM;
}

export class QueryPropertyTypes {
Expand All @@ -36,6 +37,7 @@ export class QueryPropertyTypes {
[INDICATOR_NAME_CODES.FLIL]: number;
[INDICATOR_NAME_CODES.NCE]: number;
[INDICATOR_NAME_CODES.ENL]: number;
[INDICATOR_NAME_CODES.GHG_FARM]: number;
}

export const QueryPropertyNames: QueryPropertyNamesType = {
Expand All @@ -49,16 +51,15 @@ export const QueryPropertyNames: QueryPropertyNamesType = {
[INDICATOR_NAME_CODES.NL]: INDICATOR_NAME_CODES.NL,
[INDICATOR_NAME_CODES.NCE]: INDICATOR_NAME_CODES.NCE,
[INDICATOR_NAME_CODES.FLIL]: INDICATOR_NAME_CODES.FLIL,
[INDICATOR_NAME_CODES.GHG_FARM]: INDICATOR_NAME_CODES.GHG_FARM,
} as const;

export type ImpactQueryPropertyName =
typeof QueryPropertyNames[keyof typeof QueryPropertyNames];

export type ImpactQueryExpression = string;

export type ImpactPropertyToQueryFunction =
| ((nameCode: INDICATOR_NAME_CODES) => ImpactQueryExpression)
| (() => ImpactQueryExpression);
export type ImpactPropertyToQueryFunction = () => ImpactQueryExpression;

export const INDICATOR_NAME_CODE_TO_QUERY_MAP: {
[key in INDICATOR_NAME_CODES]: {
Expand All @@ -74,48 +75,56 @@ export const INDICATOR_NAME_CODE_TO_QUERY_MAP: {
[INDICATOR_NAME_CODES.DF_SLUC]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
[INDICATOR_NAME_CODES.DF_SLUC]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.DF_SLUC]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1, '${INDICATOR_NAME_CODES.DF_SLUC}', $2, 'producer') as "${INDICATOR_NAME_CODES.DF_SLUC}"`,
},
[INDICATOR_NAME_CODES.GHG_DEF_SLUC]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
[INDICATOR_NAME_CODES.GHG_DEF_SLUC]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.GHG_DEF_SLUC]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1,'${INDICATOR_NAME_CODES.GHG_DEF_SLUC}', $2, 'producer') as "${INDICATOR_NAME_CODES.GHG_DEF_SLUC}"`,
},
[INDICATOR_NAME_CODES.UWU]: {
[INDICATOR_NAME_CODES.UWU]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.UWU]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1,'${INDICATOR_NAME_CODES.UWU}', $2, 'producer') as "${INDICATOR_NAME_CODES.UWU}"`,
},
[INDICATOR_NAME_CODES.WU]: {
[INDICATOR_NAME_CODES.WU]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.WU]: () =>
`${get_indicator_coefficient_impact}('${INDICATOR_NAME_CODES.WU}', $3, $2) as "${INDICATOR_NAME_CODES.WU}"`,
},
[INDICATOR_NAME_CODES.NL]: {
[INDICATOR_NAME_CODES.NL]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.NL]: () =>
`${get_indicator_coefficient_impact}('${INDICATOR_NAME_CODES.NL}', $3, $2) as "${INDICATOR_NAME_CODES.NL}"`,
},

[INDICATOR_NAME_CODES.NCE]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
[INDICATOR_NAME_CODES.NCE]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.NCE]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1, '${INDICATOR_NAME_CODES.NCE}', $2, 'producer') as "${INDICATOR_NAME_CODES.NCE}"`,
},
[INDICATOR_NAME_CODES.FLIL]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
harvest: () =>
`sum_material_over_georegion($1, $2, 'harvest') as "${QueryPropertyNames.harvest}"`,
[INDICATOR_NAME_CODES.FLIL]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.FLIL]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1, '${INDICATOR_NAME_CODES.FLIL}', $2, 'producer') as "${INDICATOR_NAME_CODES.FLIL}"`,
},
[INDICATOR_NAME_CODES.ENL]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
[INDICATOR_NAME_CODES.ENL]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.ENL]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1, '${INDICATOR_NAME_CODES.ENL}', $2, 'producer') as "${INDICATOR_NAME_CODES.ENL}"`,
[INDICATOR_NAME_CODES.NL]: (nameCode: INDICATOR_NAME_CODES) =>
[INDICATOR_NAME_CODES.NL]: () =>
`${get_indicator_coefficient_impact}('${INDICATOR_NAME_CODES.NL}', $3, $2) as "${INDICATOR_NAME_CODES.NL}"`,
},
[INDICATOR_NAME_CODES.GHG_FARM]: {
production: () =>
`sum_material_over_georegion($1, $2, 'producer') as "${QueryPropertyNames.production}"`,
harvest: () =>
`sum_material_over_georegion($1, $2, 'harvest') as "${QueryPropertyNames.harvest}"`,
[INDICATOR_NAME_CODES.GHG_FARM]: () =>
`${get_annual_commodity_weighted_impact_over_georegion}($1, '${INDICATOR_NAME_CODES.GHG_FARM}', $2, 'producer') as "${INDICATOR_NAME_CODES.GHG_FARM}"`,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ export class ImpactCalculator {
const preProcessed: number = getPreprocessed(INDICATOR_NAME_CODES.FLIL);
return preProcessed * getLF();
},
[INDICATOR_NAME_CODES.GHG_FARM]: () => {
const preProcessed: number = getPreprocessed(
INDICATOR_NAME_CODES.GHG_FARM,
);
return preProcessed * getLF();
},
[INDICATOR_NAME_CODES.WU]: () => {
return rawData[INDICATOR_NAME_CODES.WU] * tonnage || 0;
},
Expand Down
3 changes: 1 addition & 2 deletions api/src/modules/indicators/indicator.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export enum INDICATOR_NAME_CODES {
WU = 'WU',
UWU = 'UWU',
NL = 'NL',
// GHG_FARM pending implementation
// GHG_FARM = 'GHG_FARM',
GHG_FARM = 'GHG_FARM',
ENL = 'ENL',
NCE = 'NCE',
FLIL = 'FLIL',
Expand Down

0 comments on commit a50f5b7

Please sign in to comment.