-
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 #913 from Green-Software-Foundation/explain-feature
Add `explain` feature
- Loading branch information
Showing
7 changed files
with
155 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {explain, addExplainData} from '../../../if-run/lib/explain'; | ||
|
||
describe('lib/explain: ', () => { | ||
it('successfully adds explain data if `inputs` and `outputs` of `metadata` are `undefined`.', () => { | ||
const mockData = { | ||
pluginName: 'divide', | ||
metadata: {kind: 'execute', inputs: undefined, outputs: undefined}, | ||
pluginData: { | ||
path: 'builtin', | ||
method: 'Divide', | ||
}, | ||
}; | ||
const expectedResult = { | ||
divide: { | ||
method: 'Divide', | ||
path: 'builtin', | ||
inputs: 'undefined', | ||
outputs: 'undefined', | ||
}, | ||
}; | ||
|
||
addExplainData(mockData); | ||
const result = explain(); | ||
expect.assertions(1); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('successfully adds explain data if `inputs` and `outputs` of `metadata` are valid data.', () => { | ||
const mockData = { | ||
pluginName: 'sum', | ||
metadata: { | ||
kind: 'execute', | ||
inputs: { | ||
'cpu/energy': { | ||
unit: 'kWh', | ||
description: 'energy consumed by the cpu', | ||
}, | ||
'network/energy': { | ||
unit: 'kWh', | ||
description: 'energy consumed by data ingress and egress', | ||
}, | ||
}, | ||
outputs: { | ||
'energy-sum': { | ||
unit: 'kWh', | ||
description: 'sum of energy components', | ||
}, | ||
}, | ||
}, | ||
pluginData: { | ||
path: 'builtin', | ||
method: 'Sum', | ||
}, | ||
}; | ||
const expectedResult = { | ||
divide: { | ||
method: 'Divide', | ||
path: 'builtin', | ||
inputs: 'undefined', | ||
outputs: 'undefined', | ||
}, | ||
sum: { | ||
method: 'Sum', | ||
path: 'builtin', | ||
inputs: { | ||
'cpu/energy': { | ||
unit: 'kWh', | ||
description: 'energy consumed by the cpu', | ||
}, | ||
'network/energy': { | ||
unit: 'kWh', | ||
description: 'energy consumed by data ingress and egress', | ||
}, | ||
}, | ||
outputs: { | ||
'energy-sum': {unit: 'kWh', description: 'sum of energy components'}, | ||
}, | ||
}, | ||
}; | ||
|
||
addExplainData(mockData); | ||
const result = explain(); | ||
expect.assertions(1); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {ExplainParams} from '../types/explain'; | ||
|
||
/** | ||
* Retrieves stored explain data. | ||
*/ | ||
export const explain = () => storeExplainData.plugins; | ||
|
||
/** | ||
* Manages the storage of explain data. | ||
*/ | ||
const storeExplainData = (() => { | ||
let plugin = {}; | ||
|
||
const pluginManager = { | ||
get plugins() { | ||
return plugin; | ||
}, | ||
set plugins(value: object) { | ||
plugin = value; | ||
}, | ||
}; | ||
|
||
return pluginManager; | ||
})(); | ||
|
||
/** | ||
* 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', | ||
}, | ||
}; | ||
|
||
storeExplainData.plugins = { | ||
...storeExplainData.plugins, | ||
...plugin, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {ParameterMetadata} from '@grnsft/if-core/types'; | ||
|
||
export type ExplainParams = { | ||
pluginName: string; | ||
pluginData: {method: string; path: string}; | ||
metadata: {inputs?: ParameterMetadata; outputs?: ParameterMetadata}; | ||
}; |