Skip to content
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

Allow 0 and off values as input-parameter in Interpolation Plugin #1050

Merged
merged 11 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.


### A description of the changes proposed in the Pull Request
Expand Down
24 changes: 24 additions & 0 deletions manifests/examples/builtins/interpolation/success-with-off.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: interpolation-demo
description: simple demo of interpolation plugin
tags:
initialize:
plugins:
interpolation:
method: Interpolation
path: "builtin"
config:
method: linear
x: [0, 10, 50, 100]
y: [0.12, 0.32, 0.75, 1.02]
input-parameter: "cpu/utilization"
output-parameter: "result"
tree:
children:
child:
pipeline:
compute:
- interpolation
inputs:
- timestamp: 2023-07-06T00:00
duration: 3600
cpu/utilization: off
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"@grnsft/if-core": "^0.0.25",
"@grnsft/if-core": "^0.0.26",
"axios": "^1.7.2",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.4.6",
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/if-run/builtins/interpolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,49 @@ describe('builtins/interpolation: ', () => {
expect(result).toEqual(outputs);
});

it('returns valid output parameter if input parameter is 0.', async () => {
const inputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 0,
},
];
const outputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 0,
'interpolation-result': 0.12,
},
];

const result = await plugin.execute(inputs);

expect(result).toEqual(outputs);
});

it('returns no output parameter if input parameter is `off`.', async () => {
const inputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 'off',
},
];
const outputs = [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 0,
},
];

const result = await plugin.execute(inputs);

expect(result).toEqual(outputs);
});

it('returns result when `mapping` has valid data.', async () => {
const mapping = {
'cpu/utilization': 'cpu/util',
Expand Down
2 changes: 1 addition & 1 deletion src/if-run/builtins/interpolation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The plugin expects the following input parameters:

- `timestamp`: a timestamp for the input (required)
- `duration`: the amount of time, in seconds, that the input covers. (required)
- `[input-parameter]` - a field whose name matches the string provided to input-parameter in config (i.e. if the input-parameter in config is cpu/utilisation then cpu-utilisation must exist in the input data)
- `[input-parameter]` - a field whose name matches the string provided to input-parameter in config (i.e. if the input-parameter in config is cpu/utilisation then cpu-utilisation must exist in the input data). Value can be greater or equal to 0. For modeling server which totally turned off, `off` value can be used.

## Output

Expand Down
28 changes: 19 additions & 9 deletions src/if-run/builtins/interpolation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ export const Interpolation = PluginFactory({
.object({
timestamp: z.string().or(z.date()),
duration: z.number(),
[inputParameter]: z.number().gt(0),
[inputParameter]: z.number().gte(0).or(z.literal('off')),
})
.refine(
data =>
data[inputParameter] >= config.x[0] &&
data[inputParameter] <= config.x[config.x.length - 1],
(data[inputParameter] >= config.x[0] &&
data[inputParameter] <= config.x[config.x.length - 1]) ||
data[inputParameter] === 'off',
{
message: WITHIN_THE_RANGE,
}
Expand All @@ -68,14 +69,22 @@ export const Interpolation = PluginFactory({
return validate<z.infer<typeof schema>>(schema, input, index);
},
implementation: async (inputs: PluginParams[], config: ConfigParams) => {
const {'output-parameter': outputParameter} = config;
const {
'input-parameter': inputParameter,
'output-parameter': outputParameter,
} = config;

return inputs.map(input => {
const calculatedResult = calculateResult(config, input);
if (input[inputParameter] === 'off') {
return {
...input,
[inputParameter]: 0,
};
}

return {
...input,
[outputParameter]: calculatedResult,
[outputParameter]: calculateResult(config, input),
};
});
},
Expand Down Expand Up @@ -169,7 +178,8 @@ const getPolynomialInterpolation = (
return result;
};

/**
* Sorts given point items in ascending order.
*/
const sortPoints = (items: number[]) =>
items.sort((a: number, b: number) => {
return a - b;
});
items.sort((a: number, b: number) => a - b);
15 changes: 8 additions & 7 deletions src/if-run/lib/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
});
}

let inputStorage = structuredClone(node.inputs) as PluginParams[];
inputStorage = mergeDefaults(inputStorage, defaults);
let outputStorage = structuredClone(node.inputs) as PluginParams[];
outputStorage = mergeDefaults(outputStorage, defaults);
const pipelineCopy = structuredClone(pipeline) || {};

/** Checks if pipeline is not an array or empty object. */
Expand All @@ -125,8 +125,8 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
const plugin = params.pluginStorage.get(pluginName);
const nodeConfig = config && config[pluginName];

inputStorage = await plugin.execute(inputStorage, nodeConfig);
node.inputs = inputStorage;
outputStorage = await plugin.execute(outputStorage, nodeConfig);
node.inputs = outputStorage;

if (params.context.explainer) {
addExplainData({
Expand All @@ -145,7 +145,7 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
const originalOutputs = params.append ? node.outputs || [] : [];

node.children = Regroup(
inputStorage,
outputStorage,
originalOutputs,
pipelineCopy.regroup
);
Expand Down Expand Up @@ -183,10 +183,11 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
console.debug(COMPUTING_PIPELINE_FOR_NODE(pluginName));
debugLogger.setExecutingPluginName(pluginName);

inputStorage = await plugin.execute(inputStorage, nodeConfig);
outputStorage = await plugin.execute(outputStorage, nodeConfig);
console.log(outputStorage);
debugLogger.setExecutingPluginName();

node.outputs = inputStorage;
node.outputs = outputStorage;

if (params.context.explainer) {
addExplainData({
Expand Down