Skip to content

Commit

Permalink
Merge pull request #4 from atwoosnam/implicit_groupby
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-woosnam authored Apr 8, 2024
2 parents bda4d72 + 66adcc3 commit 095b176
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 55 deletions.
18 changes: 5 additions & 13 deletions manifests/group-by-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tree:
children:
child-1:
pipeline:
- branch-on
# - branch-on
- group-by
config:
group-by:
Expand All @@ -28,15 +28,7 @@ tree:
region: uk-west
duration: 1
cpu/utilization: 10
- timestamp: '2023-12-12T00:00:01.000Z'
duration: 5
cpu/utilization: 20
region: uk-west
- timestamp: '2023-12-12T00:00:06.000Z'
duration: 7
cpu/utilization: 15
region: uk-west
- timestamp: '2023-12-12T00:00:13.000Z'
duration: 30
region: uk-west
cpu/utilization: 15
- timestamp: '2023-12-12T00:00:00.000Z'
region: uk-north
duration: 1
cpu/utilization: 10
48 changes: 21 additions & 27 deletions manifests/realistic-manifest.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: pipeline-demo
description:
tags:
aggregation:
metrics:
- 'carbon'
type: 'both'
# aggregation: # TODO: I think this is broken, perhaps a regression.
# metrics:
# - 'carbon'
# type: 'both'
initialize:
plugins:
'branch-on':
Expand Down Expand Up @@ -38,24 +38,18 @@ initialize:
end-time: '2023-12-12T00:01:00.000Z'
interval: 5
allow-padding: true
'group-by':
path: builtin
method: GroupBy
tree:
children:
child-1:
pipeline:
- teads-curve
- sci-e
- sci-m
- sci-o
- time-sync
- sci
- branch-on
# - teads-curve
# - sci-e
# - sci-m
# - sci-o
# - time-sync
# - sci
config:
group-by:
group:
- region
- instance-type
branch-on:
region:
- uk-north
Expand Down Expand Up @@ -91,17 +85,17 @@ tree:
cpu/utilization: 15
child-2:
pipeline:
- teads-curve
- sci-e
- sci-m
- sci-o
- time-sync
- sci
- branch-on
# - teads-curve
# - sci-e
# - sci-m
# - sci-o
# - time-sync
# - sci
config:
group-by:
group:
- region
- instance-type
branch-on:
region:
- uk-north
defaults:
cpu/thermal-design-power: 100
grid/carbon-intensity: 800
Expand Down
22 changes: 22 additions & 0 deletions manifests/simple-manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Simple
description: Simplest possible manifest to test branching.
tags:
initialize:
plugins:
branch-on:
path: 'if-branch-plugin'
method: Branch
tree:
children:
child-1:
pipeline:
- branch-on
config:
branch-on:
region:
- uk-north
defaults:
inputs:
- timestamp: '2023-12-12T00:00:00.000Z'
region: uk-west
duration: 1
33 changes: 20 additions & 13 deletions src/__tests__/unit/lib/branch/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@ describe('lib/branch: ', () => {
describe('execute(): ', () => {
/** Currently does not test for grouping */
it('successfully applies branch to given input.', async () => {
const expectedResult = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: '15s',
'cpu-util': 34,
region: 'uk-south',
const expectedResult = {
'uk-north': {
inputs: [
{
'cpu-util': 34,
duration: '15s',
region: 'uk-north',
timestamp: '2021-01-01T00:00:00Z',
},
],
},
{
timestamp: '2021-01-01T00:00:00Z',
duration: '15s',
'cpu-util': 34,
region: 'uk-north',
'uk-south': {
inputs: [
{
'cpu-util': 34,
duration: '15s',
region: 'uk-south',
timestamp: '2021-01-01T00:00:00Z',
},
],
},
];

};
const result = await branch.execute(
[
{
Expand Down
56 changes: 54 additions & 2 deletions src/lib/branch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ export const Branch = (): PluginInterface => {
kind: 'execute',
};

/**
* Creates structure to insert inputs by groups.
*/
const appendGroup = (
input: PluginParams,
object: any,
branches: string[]
): any => {
if (branches.length > 0) {
const branch = branches.shift() as string;

object.children = object.children ?? {};
object.children[branch] = object.children[branch] ?? {};

if (branches.length === 0) {
if (
object.children[branch].inputs &&
object.children[branch].inputs.length > 0
) {
object.children[branch].inputs.push(input);
} else {
object.children[branch].inputs = [input];
}
}

appendGroup(input, object.children[branch], branches);
}

return object;
};

/**
* Clones `inputs` with specified `branch-on` fields, returning originals plus
* altered copies with updated values substitued from `component-config` specification.
Expand All @@ -20,23 +51,44 @@ export const Branch = (): PluginInterface => {
config: Record<string, any[]>
): Promise<PluginParams[]> => {
const validConfig = validateConfig(config);
console.log(validConfig);

// New logic to duplicate inputs and replace values
const newInputs = [...inputs]; // Start with a copy of the original inputs
const branches = Array<string>();
inputs.forEach(input => {
for (const key in validConfig) {
if (key in input) {
// Duplicate the input for each value in the "validConfig" array
validConfig[key].forEach((value: string) => {
const newInput = {...input, [key]: value};
newInputs.push(newInput); // Add the new input to the newInputs array

/**
* Extract the branches that exist in the input since we don't
* branch if it does not already exist.
*/
// if (!branches.includes(value)) { branches.push(value); }
// if (!branches.includes(input[key])) { branches.push(input[key]) };
});
}
}
});
const groupedInputs = newInputs.reduce((acc, input) => {
for (const key in validConfig) {
if (!input[key]) {
throw new InputValidationError(key);
}

branches.push(input[key]);
}
acc = {
...acc,
...appendGroup(input, acc, branches),
};
return acc;
}, {}).children; // children refers to the inputs manifest structure

return newInputs; // Return the new array with the duplicates included
return groupedInputs; // Return the de-duplicated grouped arrays
};

/**
Expand Down

0 comments on commit 095b176

Please sign in to comment.