Skip to content

Commit

Permalink
feat(builtins): refactor sum plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Sep 23, 2024
1 parent 17f823b commit ba6b1c8
Showing 1 changed file with 42 additions and 113 deletions.
155 changes: 42 additions & 113 deletions src/if-run/builtins/sum/index.ts
Original file line number Diff line number Diff line change
@@ -1,129 +1,58 @@
import {z} from 'zod';
import {
ERRORS,
evaluateInput,
evaluateConfig,
evaluateArithmeticOutput,
validateArithmeticExpression,
} from '@grnsft/if-core/utils';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
ExecutePlugin,
PluginParams,
SumConfig,
PluginParametersMetadata,
MappingParams,
} from '@grnsft/if-core/types';

import {validate} from '../../../common/util/validations';

import {STRINGS} from '../../config';
import {PluginFactory} from '@grnsft/if-core/interfaces';
import {PluginParams, ConfigParams} from '@grnsft/if-core/types';

const {ConfigError} = ERRORS;
const {MISSING_CONFIG} = STRINGS;
import {validate} from '../../../common/util/validations';

export const Sum = (
config: SumConfig,
parametersMetadata: PluginParametersMetadata,
mapping: MappingParams
): ExecutePlugin => {
const metadata = {
kind: 'execute',
inputs: parametersMetadata?.inputs,
outputs: parametersMetadata?.outputs,
};
export const Sum = PluginFactory({
metadata: {
inputs: {},
outputs: {},
},
configValidation: z.object({
'input-parameters': z.array(z.string()),
'output-parameter': z.string().min(1),
}),
inputValidation: (input: PluginParams, config: ConfigParams) => {
const inputParameters = config['input-parameters'];
const inputData = inputParameters.reduce(
(acc: {[x: string]: any}, param: string | number) => {
acc[param] = input[param];

/**
* Calculate the sum of each input-paramters.
*/
const execute = (inputs: PluginParams[]) => {
const safeConfig = validateConfig();
return acc;
},
{} as Record<string, number>
);
const validationSchema = z.record(z.string(), z.number());
return validate(validationSchema, inputData);
},
implementation: async (inputs: PluginParams[], config: ConfigParams = {}) => {
const {
'input-parameters': inputParameters,
'output-parameter': outputParameter,
} = safeConfig;
} = config;

return inputs.map(input => {
const safeInput = validateSingleInput(input, inputParameters);

const calculatedConfig = evaluateConfig({
config: safeConfig,
input,
parametersToEvaluate: config['input-parameters'],
});

const calculatedResult = calculateSum(
safeInput,
calculatedConfig['input-parameters'] || inputParameters
input,
config['input-parameters'] || inputParameters
);

const result = {
return {
...input,
...safeInput,
...evaluateArithmeticOutput(outputParameter, calculatedResult),
[outputParameter]: calculatedResult,
};

return mapOutputIfNeeded(result, mapping);
});
};

/**
* Checks config value are valid.
*/
const validateConfig = () => {
if (!config) {
throw new ConfigError(MISSING_CONFIG);
}

const mappedConfig = mapConfigIfNeeded(config, mapping);

const configSchema = z.object({
'input-parameters': z.array(z.string()),
'output-parameter': z
.string()
.min(1)
.refine(value =>
validateArithmeticExpression('output-parameter', value)
),
});

return validate<z.infer<typeof configSchema>>(configSchema, mappedConfig);
};

/**
* Checks for required fields in input.
*/
const validateSingleInput = (
input: PluginParams,
inputParameters: string[]
) => {
const evaluatedInput = evaluateInput(input);
const inputData = inputParameters.reduce(
(acc, param) => {
acc[param] = evaluatedInput[param];

return acc;
},
{} as Record<string, number>
);
const validationSchema = z.record(z.string(), z.number());
return validate(validationSchema, inputData);
};

/**
* Calculates the sum of the energy components.
*/
const calculateSum = (input: PluginParams, inputParameters: string[]) =>
inputParameters.reduce(
(accumulator, metricToSum) => accumulator + input[metricToSum],
0
);

return {
metadata,
execute,
};
};
},
allowArithmeticExpressions: [],
});

/**
* Calculates the sum of the energy components.
*/
const calculateSum = (input: PluginParams, inputParameters: string[]) =>
inputParameters.reduce(
(accumulator, metricToSum) => accumulator + input[metricToSum],
0
);

0 comments on commit ba6b1c8

Please sign in to comment.