|
| 1 | +import {ParameterMetadata} from '@grnsft/if-core/types'; |
1 | 2 | import {ERRORS} from '@grnsft/if-core/utils'; |
2 | 3 |
|
3 | 4 | import {STRINGS} from '../../common/config'; |
4 | 5 |
|
| 6 | +import {logger} from '../../common/util/logger'; |
| 7 | + |
5 | 8 | import {ExplainParams, ExplainStorageType} from '../types/explain'; |
6 | 9 |
|
7 | 10 | const {ManifestValidationError} = ERRORS; |
8 | | -const {AGGREGATION_UNITS_NOT_MATCH, AGGREGATION_METHODS_NOT_MATCH} = STRINGS; |
| 11 | +const { |
| 12 | + AGGREGATION_UNITS_NOT_MATCH, |
| 13 | + AGGREGATION_METHODS_NOT_MATCH, |
| 14 | + MISSING_INPUTS_PARAMETER, |
| 15 | + MISSING_OUTPUTS_PARAMETER, |
| 16 | +} = STRINGS; |
9 | 17 |
|
10 | 18 | /** |
11 | 19 | * Retrieves stored explain data. |
12 | 20 | */ |
13 | | -export const explain = () => storeExplainData.parameters; |
| 21 | +export const explain = () => storeExplainData.plugins; |
14 | 22 |
|
15 | 23 | /** |
16 | 24 | * Manages the storage of explain data. |
17 | 25 | */ |
18 | 26 | const storeExplainData = (() => { |
19 | | - let parameter: ExplainStorageType = {}; |
| 27 | + let plugins: ExplainStorageType = {}; |
20 | 28 |
|
21 | | - const parameterManager = { |
22 | | - get parameters() { |
23 | | - return parameter; |
| 29 | + const pluginManager = { |
| 30 | + get plugins() { |
| 31 | + return plugins; |
24 | 32 | }, |
25 | | - set parameters(value: ExplainStorageType) { |
26 | | - parameter = value; |
| 33 | + set plugins(value: ExplainStorageType) { |
| 34 | + plugins = value; |
27 | 35 | }, |
28 | 36 | }; |
29 | 37 |
|
30 | | - return parameterManager; |
| 38 | + return pluginManager; |
31 | 39 | })(); |
32 | 40 |
|
33 | 41 | /** |
34 | 42 | * Adds new explain data to the stored explain data. |
35 | 43 | */ |
36 | 44 | export const addExplainData = (params: ExplainParams) => { |
37 | | - const {pluginName, pluginData, metadata} = params; |
38 | | - const parameterMetadata = pluginData?.['parameter-metadata'] || metadata; |
39 | | - const parameters = storeExplainData.parameters; |
40 | | - const allParameters = { |
41 | | - ...parameterMetadata?.inputs, |
42 | | - ...parameterMetadata?.outputs, |
43 | | - } as ExplainStorageType; |
44 | | - |
45 | | - Object.entries(allParameters).forEach(([name, meta]) => { |
46 | | - const existingParameter = parameters[name]; |
47 | | - |
48 | | - if (parameters[name]?.plugins?.includes(pluginName)) { |
49 | | - return; |
50 | | - } |
| 45 | + const {pluginName, metadata} = params; |
| 46 | + const plugin: ExplainStorageType = { |
| 47 | + [pluginName]: { |
| 48 | + inputs: metadata?.inputs ?? {}, |
| 49 | + outputs: metadata?.outputs ?? {}, |
| 50 | + }, |
| 51 | + }; |
51 | 52 |
|
52 | | - if (existingParameter) { |
53 | | - if (meta.unit !== existingParameter.unit) { |
54 | | - throw new ManifestValidationError(AGGREGATION_UNITS_NOT_MATCH(name)); |
55 | | - } |
| 53 | + const isInputsMissing = !Object.keys(plugin[pluginName].inputs || {}).length; |
| 54 | + const isOutputsMissing = !Object.keys(plugin[pluginName].outputs || {}) |
| 55 | + .length; |
| 56 | + |
| 57 | + if (isInputsMissing) { |
| 58 | + delete plugin[pluginName].inputs; |
| 59 | + |
| 60 | + logger.warn(MISSING_INPUTS_PARAMETER(pluginName)); |
| 61 | + } |
| 62 | + |
| 63 | + if (isOutputsMissing) { |
| 64 | + delete plugin[pluginName].outputs; |
| 65 | + |
| 66 | + logger.warn(MISSING_OUTPUTS_PARAMETER(pluginName)); |
| 67 | + } |
| 68 | + |
| 69 | + checkMetadatas(metadata); |
| 70 | + |
| 71 | + if (!isInputsMissing || !isOutputsMissing) { |
| 72 | + storeExplainData.plugins = { |
| 73 | + ...storeExplainData.plugins, |
| 74 | + ...plugin, |
| 75 | + }; |
| 76 | + } |
| 77 | +}; |
56 | 78 |
|
57 | | - if ( |
58 | | - meta['aggregation-method'].component !== |
59 | | - existingParameter['aggregation-method'].component || |
60 | | - meta['aggregation-method'].time !== |
61 | | - existingParameter['aggregation-method'].time |
62 | | - ) { |
63 | | - throw new ManifestValidationError(AGGREGATION_METHODS_NOT_MATCH(name)); |
| 79 | +/** |
| 80 | + * Checks if the 'unit' and 'aggregation-method' of the parameter are the same throughout the manifest |
| 81 | + */ |
| 82 | +const checkMetadatas = (metadata: { |
| 83 | + inputs?: ParameterMetadata; |
| 84 | + outputs?: ParameterMetadata; |
| 85 | +}) => { |
| 86 | + const inputsOutputsMetadata = {...metadata?.inputs, ...metadata?.outputs}; |
| 87 | + const storedParameters: any = {}; |
| 88 | + |
| 89 | + // Populate stored parameters with metadata from each plugin |
| 90 | + Object.values(storeExplainData.plugins).forEach(plugin => { |
| 91 | + const storedInputOutputMetadata = {...plugin.inputs, ...plugin.outputs}; |
| 92 | + |
| 93 | + Object.keys(storedInputOutputMetadata).forEach(parameter => { |
| 94 | + if (!storedParameters[parameter]) { |
| 95 | + storedParameters[parameter] = { |
| 96 | + unit: storedInputOutputMetadata[parameter].unit, |
| 97 | + 'aggregation-method': |
| 98 | + storedInputOutputMetadata[parameter]['aggregation-method'], |
| 99 | + }; |
64 | 100 | } |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + // Validate input-output metadata against stored parameters |
| 105 | + Object.keys(inputsOutputsMetadata).forEach(parameterName => { |
| 106 | + const parameter = inputsOutputsMetadata[parameterName]; |
| 107 | + const storedParameter = storedParameters[parameterName]; |
65 | 108 |
|
66 | | - existingParameter.plugins.push(pluginName); |
67 | | - existingParameter.description = |
68 | | - meta.description || existingParameter.description; |
69 | | - } else { |
70 | | - parameters[name] = { |
71 | | - plugins: [pluginName], |
72 | | - unit: meta.unit, |
73 | | - description: meta.description, |
74 | | - 'aggregation-method': meta['aggregation-method'], |
75 | | - }; |
| 109 | + if ( |
| 110 | + parameter && |
| 111 | + Object.keys(storedParameters).includes(parameterName) && |
| 112 | + storedParameter.unit !== parameter.unit |
| 113 | + ) { |
| 114 | + throw new ManifestValidationError( |
| 115 | + AGGREGATION_UNITS_NOT_MATCH(parameterName) |
| 116 | + ); |
76 | 117 | } |
77 | | - }); |
78 | 118 |
|
79 | | - storeExplainData.parameters = { |
80 | | - ...parameters, |
81 | | - }; |
| 119 | + // Check for aggregation-method mismatch |
| 120 | + const inputAggregation = parameter['aggregation-method']; |
| 121 | + |
| 122 | + if ( |
| 123 | + storedParameter && |
| 124 | + (storedParameter['aggregation-method']?.component !== |
| 125 | + inputAggregation?.component || |
| 126 | + storedParameter['aggregation-method']?.time !== inputAggregation?.time) |
| 127 | + ) { |
| 128 | + throw new ManifestValidationError( |
| 129 | + AGGREGATION_METHODS_NOT_MATCH(parameterName) |
| 130 | + ); |
| 131 | + } |
| 132 | + }); |
82 | 133 | }; |
0 commit comments