-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate multiply
plugin to builtins
#709
Merged
Merged
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c89d83a
feat(lib): add readme to builtins/multiply
jmcook1186 46bb1fe
feat(lib): export multiply from builtins
jmcook1186 194b51f
feat(lib): move types into if
jmcook1186 9bf98d6
feat(lib): add more helpers to if
jmcook1186 9656476
feat(lib): move types into if
jmcook1186 3ebfa35
feat(lib): move plugin code into if
jmcook1186 749f739
test(lib): move unit tests into if
jmcook1186 4224648
Apply suggestions from code review
jmcook1186 69057c5
Update src/builtins/multiply/README.md
jmcook1186 97eb4e6
Update src/builtins/multiply/README.md
jmcook1186 6d336e0
Merge branch 'main' into migrate-multiply-plugin
jmcook1186 a28ab40
Update helpers.ts
jmcook1186 aced6e2
Update helpers.ts
jmcook1186 473be32
fix(lib): run linter
jmcook1186 b04dc17
Merge branch 'main' into migrate-multiply-plugin
jmcook1186 80bf519
Merge branch 'main' into migrate-multiply-plugin
jmcook1186 2b7d73e
fix(lib): update import path for Multiply in manifests
jmcook1186 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,102 @@ | ||
import {Multiply} from '../../../builtins/multiply'; | ||
|
||
import {ERRORS} from '../../../util/errors'; | ||
|
||
const {InputValidationError} = ERRORS; | ||
|
||
describe('lib/multiply: ', () => { | ||
describe('Multiply: ', () => { | ||
const globalConfig = { | ||
'input-parameters': ['cpu/energy', 'network/energy', 'memory/energy'], | ||
'output-parameter': 'energy', | ||
}; | ||
const multiply = Multiply(globalConfig); | ||
|
||
describe('init: ', () => { | ||
it('successfully initalized.', () => { | ||
expect(multiply).toHaveProperty('metadata'); | ||
expect(multiply).toHaveProperty('execute'); | ||
}); | ||
}); | ||
|
||
describe('execute(): ', () => { | ||
it('successfully applies Multiply strategy to given input.', async () => { | ||
expect.assertions(1); | ||
|
||
const expectedResult = [ | ||
{ | ||
duration: 3600, | ||
'cpu/energy': 2, | ||
'network/energy': 2, | ||
'memory/energy': 2, | ||
energy: 8, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]; | ||
|
||
const result = await multiply.execute([ | ||
{ | ||
duration: 3600, | ||
'cpu/energy': 2, | ||
'network/energy': 2, | ||
'memory/energy': 2, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it('throws an error on missing params in input.', async () => { | ||
const expectedMessage = | ||
'Multiply: cpu/energy is missing from the input array.'; | ||
|
||
expect.assertions(1); | ||
|
||
try { | ||
await multiply.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]); | ||
} catch (error) { | ||
expect(error).toStrictEqual( | ||
new InputValidationError(expectedMessage) | ||
); | ||
} | ||
}); | ||
|
||
it('returns a result with input params not related to energy.', async () => { | ||
expect.assertions(1); | ||
const newConfig = { | ||
'input-parameters': ['carbon', 'other-carbon'], | ||
'output-parameter': 'carbon-product', | ||
}; | ||
const multiply = Multiply(newConfig); | ||
|
||
const data = [ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
carbon: 3, | ||
'other-carbon': 2, | ||
}, | ||
]; | ||
const response = await multiply.execute(data); | ||
|
||
const expectedResult = [ | ||
{ | ||
duration: 3600, | ||
carbon: 3, | ||
'other-carbon': 2, | ||
'carbon-product': 6, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
}, | ||
]; | ||
|
||
expect(response).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {GroupBy} from './group-by'; | ||
export {TimeSync} from './time-sync'; | ||
export {Multiply} from './multiply'; |
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,95 @@ | ||
# Multiply | ||
|
||
`multiply` is a generic plugin for multiplying two or more values in an `input` array. | ||
|
||
You provide the names of the values you want to multiply, and a name to use to append the product to the output array. | ||
|
||
For example, you could multiply `cpu/energy` and `network/energy` and name the result `energy-product`. `energy-product` would then be added to every observation in your input array as the product of `cpu/energy` and `network/energy`. | ||
|
||
## Parameters | ||
|
||
### Plugin config | ||
|
||
Two parameters are required in global config: `input-parameters` and `output-parameter`. | ||
|
||
`input-parameters`: an array of strings. Each string should match an existing key in the `inputs` array | ||
`output-parameter`: a string defining the name to use to add the product of the input parameters to the output array. | ||
|
||
### Inputs | ||
|
||
All of `input-parameters` must be available in the input array. | ||
|
||
## Returns | ||
|
||
- `output-parameter`: the product of all `input-parameters` with the parameter name defined by `output-parameter` in global config. | ||
|
||
## Calculation | ||
|
||
```pseudocode | ||
output = input0 * input1 * input2 ... inputN | ||
``` | ||
|
||
## Implementation | ||
|
||
To run the plugin, you must first create an instance of `Multiply`. Then, you can call `execute()`. | ||
|
||
```typescript | ||
import {Multiply} from 'builtins'; | ||
|
||
const config = { | ||
inputParameters: ['cpu/energy', 'network/energy'], | ||
outputParameter: 'energy-product', | ||
}; | ||
|
||
const mult = Multiply(config); | ||
const result = await mult.execute([ | ||
{ | ||
duration: 3600, | ||
timestamp: '2021-01-01T00:00:00Z', | ||
'cpu/energy': 0.001, | ||
'memory/energy': 0.0005, | ||
}, | ||
]); | ||
``` | ||
|
||
## Example manifest | ||
|
||
IF users will typically call the plugin as part of a pipeline defined in a manifest file. In this case, instantiating the plugin is handled by `ie` and does not have to be done explicitly by the user. The following is an example manifest that calls `multiply`: | ||
|
||
```yaml | ||
name: multiply-demo | ||
description: | ||
tags: | ||
initialize: | ||
outputs: | ||
- yaml | ||
plugins: | ||
multiply: | ||
method: Multiply | ||
path: 'builtins' | ||
jmcook1186 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
global-config: | ||
input-parameters: ['cpu/energy', 'network/energy'] | ||
output-parameter: 'energy-product' | ||
tree: | ||
children: | ||
child: | ||
pipeline: | ||
- multiply | ||
config: | ||
multiply: | ||
inputs: | ||
- timestamp: 2023-08-06T00:00 | ||
duration: 3600 | ||
cpu/energy: 0.001 | ||
network/energy: 0.001 | ||
``` | ||
|
||
You can run this example by saving it as `./examples/manifests/test/multiply.yml` and executing the following command from the project root: | ||
|
||
```sh | ||
npm i -g @grnsft/if | ||
npm i -g @grnsft/if-plugins | ||
jmcook1186 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ie --manifest ./examples/manifests/test/multiply.yml --output ./examples/outputs/multiply.yml | ||
``` | ||
|
||
The results will be saved to a new `yaml` file in `./examples/outputs` |
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,87 @@ | ||
import {z} from 'zod'; | ||
|
||
import {buildErrorMessage} from '../../util/helpers'; | ||
import {ERRORS} from '../../util/errors'; | ||
import {validate} from '../../util/validations'; | ||
|
||
import {ExecutePlugin, PluginParams} from '../../types/interface'; | ||
import {MultiplyConfig} from './types'; | ||
|
||
const {InputValidationError} = ERRORS; | ||
|
||
export const Multiply = (globalConfig: MultiplyConfig): ExecutePlugin => { | ||
const errorBuilder = buildErrorMessage(Multiply.name); | ||
const metadata = { | ||
kind: 'execute', | ||
}; | ||
|
||
/** | ||
* Checks global config value are valid. | ||
*/ | ||
const validateGlobalConfig = () => { | ||
const globalConfigSchema = z.object({ | ||
'input-parameters': z.array(z.string()), | ||
'output-parameter': z.string().min(1), | ||
}); | ||
|
||
return validate<z.infer<typeof globalConfigSchema>>( | ||
globalConfigSchema, | ||
globalConfig | ||
); | ||
}; | ||
|
||
/** | ||
* Checks for required fields in input. | ||
*/ | ||
const validateSingleInput = ( | ||
input: PluginParams, | ||
inputParameters: string[] | ||
) => { | ||
inputParameters.forEach(metricToMultiply => { | ||
if ( | ||
input[metricToMultiply] === undefined || | ||
isNaN(input[metricToMultiply]) | ||
) { | ||
throw new InputValidationError( | ||
errorBuilder({ | ||
message: `${metricToMultiply} is missing from the input array`, | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
return input; | ||
}; | ||
|
||
/** | ||
* Calculate the product of each input parameter. | ||
*/ | ||
const execute = (inputs: PluginParams[]): PluginParams[] => { | ||
const safeGlobalConfig = validateGlobalConfig(); | ||
const inputParameters = safeGlobalConfig['input-parameters']; | ||
const outputParameter = safeGlobalConfig['output-parameter']; | ||
|
||
return inputs.map(input => { | ||
const safeInput = validateSingleInput(input, inputParameters); | ||
|
||
return { | ||
...input, | ||
[outputParameter]: calculateProduct(safeInput, inputParameters), | ||
}; | ||
}); | ||
}; | ||
|
||
/** | ||
* Calculates the product of the energy components. | ||
*/ | ||
const calculateProduct = (input: PluginParams, inputParameters: string[]) => | ||
inputParameters.reduce( | ||
(accumulator, metricToMultiply) => accumulator * input[metricToMultiply], | ||
1 | ||
); | ||
|
||
return { | ||
metadata, | ||
execute, | ||
}; | ||
}; |
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,4 @@ | ||
export type MultiplyConfig = { | ||
'input-parameters': string[]; | ||
'output-parameter': string; | ||
}; |
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,4 @@ | ||
export type ErrorFormatParams = { | ||
scope?: string; | ||
message: string; | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this part for builtin plugins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so... ?