From ba6b1c82ac039964b7a88a4ab93ebcb52116d2ca Mon Sep 17 00:00:00 2001 From: manushak Date: Mon, 23 Sep 2024 10:55:20 +0400 Subject: [PATCH] feat(builtins): refactor sum plugin --- src/if-run/builtins/sum/index.ts | 155 +++++++++---------------------- 1 file changed, 42 insertions(+), 113 deletions(-) diff --git a/src/if-run/builtins/sum/index.ts b/src/if-run/builtins/sum/index.ts index e111bce4..23ed71c9 100644 --- a/src/if-run/builtins/sum/index.ts +++ b/src/if-run/builtins/sum/index.ts @@ -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 + ); + 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>(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 - ); - 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 + );