-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1065 from Green-Software-Foundation/update-explai…
…n-feature Update explain feature
- Loading branch information
Showing
5 changed files
with
169 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,133 @@ | ||
import {ParameterMetadata} from '@grnsft/if-core/types'; | ||
import {ERRORS} from '@grnsft/if-core/utils'; | ||
|
||
import {STRINGS} from '../../common/config'; | ||
|
||
import {logger} from '../../common/util/logger'; | ||
|
||
import {ExplainParams, ExplainStorageType} from '../types/explain'; | ||
|
||
const {ManifestValidationError} = ERRORS; | ||
const {AGGREGATION_UNITS_NOT_MATCH, AGGREGATION_METHODS_NOT_MATCH} = STRINGS; | ||
const { | ||
AGGREGATION_UNITS_NOT_MATCH, | ||
AGGREGATION_METHODS_NOT_MATCH, | ||
MISSING_INPUTS_PARAMETER, | ||
MISSING_OUTPUTS_PARAMETER, | ||
} = STRINGS; | ||
|
||
/** | ||
* Retrieves stored explain data. | ||
*/ | ||
export const explain = () => storeExplainData.parameters; | ||
export const explain = () => storeExplainData.plugins; | ||
|
||
/** | ||
* Manages the storage of explain data. | ||
*/ | ||
const storeExplainData = (() => { | ||
let parameter: ExplainStorageType = {}; | ||
let plugins: ExplainStorageType = {}; | ||
|
||
const parameterManager = { | ||
get parameters() { | ||
return parameter; | ||
const pluginManager = { | ||
get plugins() { | ||
return plugins; | ||
}, | ||
set parameters(value: ExplainStorageType) { | ||
parameter = value; | ||
set plugins(value: ExplainStorageType) { | ||
plugins = value; | ||
}, | ||
}; | ||
|
||
return parameterManager; | ||
return pluginManager; | ||
})(); | ||
|
||
/** | ||
* Adds new explain data to the stored explain data. | ||
*/ | ||
export const addExplainData = (params: ExplainParams) => { | ||
const {pluginName, pluginData, metadata} = params; | ||
const parameterMetadata = pluginData?.['parameter-metadata'] || metadata; | ||
const parameters = storeExplainData.parameters; | ||
const allParameters = { | ||
...parameterMetadata?.inputs, | ||
...parameterMetadata?.outputs, | ||
} as ExplainStorageType; | ||
|
||
Object.entries(allParameters).forEach(([name, meta]) => { | ||
const existingParameter = parameters[name]; | ||
|
||
if (parameters[name]?.plugins?.includes(pluginName)) { | ||
return; | ||
} | ||
const {pluginName, metadata} = params; | ||
const plugin: ExplainStorageType = { | ||
[pluginName]: { | ||
inputs: metadata?.inputs ?? {}, | ||
outputs: metadata?.outputs ?? {}, | ||
}, | ||
}; | ||
|
||
if (existingParameter) { | ||
if (meta.unit !== existingParameter.unit) { | ||
throw new ManifestValidationError(AGGREGATION_UNITS_NOT_MATCH(name)); | ||
} | ||
const isInputsMissing = !Object.keys(plugin[pluginName].inputs || {}).length; | ||
const isOutputsMissing = !Object.keys(plugin[pluginName].outputs || {}) | ||
.length; | ||
|
||
if (isInputsMissing) { | ||
delete plugin[pluginName].inputs; | ||
|
||
logger.warn(MISSING_INPUTS_PARAMETER(pluginName)); | ||
} | ||
|
||
if (isOutputsMissing) { | ||
delete plugin[pluginName].outputs; | ||
|
||
logger.warn(MISSING_OUTPUTS_PARAMETER(pluginName)); | ||
} | ||
|
||
checkMetadatas(metadata); | ||
|
||
if (!isInputsMissing || !isOutputsMissing) { | ||
storeExplainData.plugins = { | ||
...storeExplainData.plugins, | ||
...plugin, | ||
}; | ||
} | ||
}; | ||
|
||
if ( | ||
meta['aggregation-method'].component !== | ||
existingParameter['aggregation-method'].component || | ||
meta['aggregation-method'].time !== | ||
existingParameter['aggregation-method'].time | ||
) { | ||
throw new ManifestValidationError(AGGREGATION_METHODS_NOT_MATCH(name)); | ||
/** | ||
* Checks if the 'unit' and 'aggregation-method' of the parameter are the same throughout the manifest | ||
*/ | ||
const checkMetadatas = (metadata: { | ||
inputs?: ParameterMetadata; | ||
outputs?: ParameterMetadata; | ||
}) => { | ||
const inputsOutputsMetadata = {...metadata?.inputs, ...metadata?.outputs}; | ||
const storedParameters: any = {}; | ||
|
||
// Populate stored parameters with metadata from each plugin | ||
Object.values(storeExplainData.plugins).forEach(plugin => { | ||
const storedInputOutputMetadata = {...plugin.inputs, ...plugin.outputs}; | ||
|
||
Object.keys(storedInputOutputMetadata).forEach(parameter => { | ||
if (!storedParameters[parameter]) { | ||
storedParameters[parameter] = { | ||
unit: storedInputOutputMetadata[parameter].unit, | ||
'aggregation-method': | ||
storedInputOutputMetadata[parameter]['aggregation-method'], | ||
}; | ||
} | ||
}); | ||
}); | ||
|
||
// Validate input-output metadata against stored parameters | ||
Object.keys(inputsOutputsMetadata).forEach(parameterName => { | ||
const parameter = inputsOutputsMetadata[parameterName]; | ||
const storedParameter = storedParameters[parameterName]; | ||
|
||
existingParameter.plugins.push(pluginName); | ||
existingParameter.description = | ||
meta.description || existingParameter.description; | ||
} else { | ||
parameters[name] = { | ||
plugins: [pluginName], | ||
unit: meta.unit, | ||
description: meta.description, | ||
'aggregation-method': meta['aggregation-method'], | ||
}; | ||
if ( | ||
parameter && | ||
Object.keys(storedParameters).includes(parameterName) && | ||
storedParameter.unit !== parameter.unit | ||
) { | ||
throw new ManifestValidationError( | ||
AGGREGATION_UNITS_NOT_MATCH(parameterName) | ||
); | ||
} | ||
}); | ||
|
||
storeExplainData.parameters = { | ||
...parameters, | ||
}; | ||
// Check for aggregation-method mismatch | ||
const inputAggregation = parameter['aggregation-method']; | ||
|
||
if ( | ||
storedParameter && | ||
(storedParameter['aggregation-method']?.component !== | ||
inputAggregation?.component || | ||
storedParameter['aggregation-method']?.time !== inputAggregation?.time) | ||
) { | ||
throw new ManifestValidationError( | ||
AGGREGATION_METHODS_NOT_MATCH(parameterName) | ||
); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.