-
Notifications
You must be signed in to change notification settings - Fork 39
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 #996 from Green-Software-Foundation/explain-rework
Update `explain` feature
- Loading branch information
Showing
4 changed files
with
243 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,79 @@ | ||
import {ExplainParams} from '../types/explain'; | ||
import {ERRORS} from '@grnsft/if-core/utils'; | ||
|
||
import {STRINGS} from '../../common/config'; | ||
|
||
import {ExplainParams, ExplainStorageType} from '../types/explain'; | ||
|
||
const {ManifestValidationError} = ERRORS; | ||
const {AGGREGATION_UNITS_NOT_MATCH, AGGREGATION_METHODS_NOT_MATCH} = STRINGS; | ||
|
||
/** | ||
* Retrieves stored explain data. | ||
*/ | ||
export const explain = () => storeExplainData.plugins; | ||
export const explain = () => storeExplainData.parameters; | ||
|
||
/** | ||
* Manages the storage of explain data. | ||
*/ | ||
const storeExplainData = (() => { | ||
let plugin = {}; | ||
let parameter: ExplainStorageType = {}; | ||
|
||
const pluginManager = { | ||
get plugins() { | ||
return plugin; | ||
const parameterManager = { | ||
get parameters() { | ||
return parameter; | ||
}, | ||
set plugins(value: object) { | ||
plugin = value; | ||
set parameters(value: ExplainStorageType) { | ||
parameter = value; | ||
}, | ||
}; | ||
|
||
return pluginManager; | ||
return parameterManager; | ||
})(); | ||
|
||
/** | ||
* Adds new explain data to the stored explain data. | ||
*/ | ||
export const addExplainData = (params: ExplainParams) => { | ||
const {pluginName, pluginData, metadata} = params; | ||
const plugin = { | ||
[pluginName]: { | ||
method: pluginData!.method, | ||
path: pluginData!.path, | ||
inputs: metadata?.inputs || 'undefined', | ||
outputs: metadata?.outputs || 'undefined', | ||
}, | ||
const parameterMetadata = pluginData?.['parameter-metadata'] || metadata; | ||
const parameters = storeExplainData.parameters; | ||
const allParameters = { | ||
...parameterMetadata?.inputs, | ||
...parameterMetadata?.outputs, | ||
}; | ||
|
||
storeExplainData.plugins = { | ||
...storeExplainData.plugins, | ||
...plugin, | ||
Object.entries(allParameters).forEach(([name, meta]) => { | ||
const existingParameter = parameters[name]; | ||
|
||
if (parameters[name]?.plugins?.includes(pluginName)) { | ||
return; | ||
} | ||
|
||
if (existingParameter) { | ||
if (meta.unit !== existingParameter.unit) { | ||
throw new ManifestValidationError(AGGREGATION_UNITS_NOT_MATCH(name)); | ||
} | ||
|
||
if ( | ||
meta['aggregation-method'] !== existingParameter['aggregation-method'] | ||
) { | ||
throw new ManifestValidationError(AGGREGATION_METHODS_NOT_MATCH(name)); | ||
} | ||
|
||
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'], | ||
}; | ||
} | ||
}); | ||
|
||
storeExplainData.parameters = { | ||
...parameters, | ||
}; | ||
}; |
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