Skip to content

Commit d63b807

Browse files
authored
Merge pull request #1108 from Green-Software-Foundation/regroup
Skip regrouping on already regrouped manifests
2 parents 8eac74f + 43db5f3 commit d63b807

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

src/__tests__/if-run/lib/compute.test.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ jest.mock('../../../common/util/logger', () => ({
2222
},
2323
}));
2424

25-
import * as explainer from '../../../if-run/lib/explain';
26-
27-
import {compute} from '../../../if-run/lib/compute';
2825
import {ComputeParams} from '../../../if-run/types/compute';
2926
import {pluginStorage} from '../../../if-run/util/plugin-storage';
3027

3128
describe('lib/compute: ', () => {
29+
beforeEach(() => {
30+
jest.resetModules();
31+
});
32+
3233
/**
3334
* Mock plugins.
3435
*/
@@ -135,6 +136,7 @@ describe('lib/compute: ', () => {
135136

136137
describe('compute(): ', () => {
137138
it('computes simple tree with execute plugin.', async () => {
139+
const {compute} = require('../../../if-run/lib/compute');
138140
const tree = {
139141
children: {
140142
mockChild: {
@@ -156,6 +158,7 @@ describe('lib/compute: ', () => {
156158
});
157159

158160
it('computes simple tree with regroup on inputs only (no compute).', async () => {
161+
const {compute} = require('../../../if-run/lib/compute');
159162
const tree = {
160163
children: {
161164
mockChild: {
@@ -184,7 +187,34 @@ describe('lib/compute: ', () => {
184187
expect(response.children.mockChild.children).toEqual(expectedResponse);
185188
});
186189

190+
it('skips regrouping on already regrouped data.', async () => {
191+
const {compute} = require('../../../if-run/lib/compute');
192+
const expectedResponse = {
193+
'uk-west': {
194+
inputs: [{region: 'uk-west', timestamp: 'mock-timestamp-1'}],
195+
},
196+
'uk-east': {
197+
inputs: [
198+
{region: 'uk-east', timestamp: 'mock-timestamp-2'},
199+
{region: 'uk-east', timestamp: 'mock-timestamp-3'},
200+
],
201+
},
202+
};
203+
const tree = {
204+
children: {
205+
mockChild: {
206+
pipeline: {regroup: ['region']},
207+
children: expectedResponse,
208+
},
209+
},
210+
};
211+
const response = await compute(tree, paramsExecute);
212+
213+
expect(response.children.mockChild.children).toEqual(expectedResponse);
214+
});
215+
187216
it('computes simple tree with regroup, grouping inputs and outputs.', async () => {
217+
const {compute} = require('../../../if-run/lib/compute');
188218
const tree = {
189219
children: {
190220
mockChild: {
@@ -232,6 +262,7 @@ describe('lib/compute: ', () => {
232262
});
233263

234264
it('computes simple tree with defaults and execute plugin.', async () => {
265+
const {compute} = require('../../../if-run/lib/compute');
235266
const tree = {
236267
children: {
237268
mockChild: {
@@ -259,6 +290,7 @@ describe('lib/compute: ', () => {
259290
});
260291

261292
it('computes nested tree with defaults and execute plugin.', async () => {
293+
const {compute} = require('../../../if-run/lib/compute');
262294
const tree = {
263295
children: {
264296
mockChild1: {
@@ -308,6 +340,7 @@ describe('lib/compute: ', () => {
308340
});
309341

310342
it('computes simple tree with no defaults and no inputs with execute plugin.', async () => {
343+
const {compute} = require('../../../if-run/lib/compute');
311344
const tree = {
312345
children: {
313346
mockChild: {
@@ -323,6 +356,7 @@ describe('lib/compute: ', () => {
323356
});
324357

325358
it('computes simple tree with defaults and no inputs with execute plugin.', async () => {
359+
const {compute} = require('../../../if-run/lib/compute');
326360
const tree = {
327361
children: {
328362
mockChild: {
@@ -343,6 +377,7 @@ describe('lib/compute: ', () => {
343377
});
344378

345379
it('computes simple tree with append, preserving existing outputs.', async () => {
380+
const {compute} = require('../../../if-run/lib/compute');
346381
const tree = {
347382
children: {
348383
mockChild: {
@@ -378,6 +413,7 @@ describe('lib/compute: ', () => {
378413
});
379414

380415
it('computes simple tree with append when outputs is null.', async () => {
416+
const {compute} = require('../../../if-run/lib/compute');
381417
const tree = {
382418
children: {
383419
mockChild: {
@@ -402,6 +438,7 @@ describe('lib/compute: ', () => {
402438
});
403439

404440
it('computes simple tree with regroup and append, with existing outputs preserved and regrouped without re-computing.', async () => {
441+
const {compute} = require('../../../if-run/lib/compute');
405442
const tree = {
406443
children: {
407444
mockChild: {
@@ -434,6 +471,7 @@ describe('lib/compute: ', () => {
434471
});
435472

436473
it('computes simple tree with regroup and append, with existing outputs preserved and without new outputs.', async () => {
474+
const {compute} = require('../../../if-run/lib/compute');
437475
const tree = {
438476
children: {
439477
mockChild: {
@@ -460,6 +498,7 @@ describe('lib/compute: ', () => {
460498
});
461499

462500
it('computes simple tree with regroup and no append, with existing outputs that are removed.', async () => {
501+
const {compute} = require('../../../if-run/lib/compute');
463502
const tree = {
464503
children: {
465504
mockChild: {
@@ -488,6 +527,7 @@ describe('lib/compute: ', () => {
488527
});
489528

490529
it('computes simple tree with observe plugin.', async () => {
530+
const {compute} = require('../../../if-run/lib/compute');
491531
const tree = {
492532
children: {
493533
mockChild: {
@@ -507,6 +547,7 @@ describe('lib/compute: ', () => {
507547
});
508548

509549
it('computes simple tree with observe plugin.', async () => {
550+
const {compute} = require('../../../if-run/lib/compute');
510551
const tree = {
511552
children: {
512553
mockChild: {
@@ -526,6 +567,7 @@ describe('lib/compute: ', () => {
526567
});
527568

528569
it('observes simple tree with observe plugin.', async () => {
570+
const {compute} = require('../../../if-run/lib/compute');
529571
const tree = {
530572
children: {
531573
mockChild: {
@@ -545,6 +587,7 @@ describe('lib/compute: ', () => {
545587
});
546588

547589
it('observes simple tree with config.', async () => {
590+
const {compute} = require('../../../if-run/lib/compute');
548591
const tree = {
549592
children: {
550593
mockChild: {
@@ -565,6 +608,7 @@ describe('lib/compute: ', () => {
565608
});
566609

567610
it('warns when pipeline is null.', async () => {
611+
const {compute} = require('../../../if-run/lib/compute');
568612
process.env.LOGGER = 'invalid';
569613
const tree = {
570614
children: {
@@ -583,6 +627,7 @@ describe('lib/compute: ', () => {
583627
});
584628

585629
it('warns when pipeline is an empty object.', async () => {
630+
const {compute} = require('../../../if-run/lib/compute');
586631
const tree = {
587632
children: {
588633
mockChild: {
@@ -598,6 +643,7 @@ describe('lib/compute: ', () => {
598643
});
599644

600645
it('warns when config is provided in the tree.', async () => {
646+
const {compute} = require('../../../if-run/lib/compute');
601647
process.env.LOGGER = 'true';
602648
const tree = {
603649
children: {
@@ -624,6 +670,7 @@ describe('lib/compute: ', () => {
624670
});
625671

626672
it('warns when config is provided in the tree and it is null.', async () => {
673+
const {compute} = require('../../../if-run/lib/compute');
627674
process.env.LOGGER = 'empty';
628675
const tree = {
629676
children: {
@@ -650,6 +697,8 @@ describe('lib/compute: ', () => {
650697
});
651698

652699
it('observes simple tree with execute plugin and explain property.', async () => {
700+
const {compute} = require('../../../if-run/lib/compute');
701+
const explainer = require('../../../if-run/lib/explain');
653702
const tree = {
654703
children: {
655704
mockChild: {
@@ -678,6 +727,7 @@ describe('lib/compute: ', () => {
678727
});
679728

680729
it('computes simple tree with execute plugin and explain property.', async () => {
730+
const {compute} = require('../../../if-run/lib/compute');
681731
const tree = {
682732
children: {
683733
mockChild: {

src/if-run/config/strings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Note that for the '--output' option you also need to define the output type in y
5151
COMPUTING_COMPONENT_PIPELINE: (component: string) =>
5252
`**Computing \`${component}\` pipeline**`,
5353
REGROUPING: 'Regrouping',
54+
SKIPPING_REGROUP: 'Already correctly grouped - skipping regrouping.',
5455
OBSERVING: (nodeName: string) =>
5556
`Running observe pipeline: \`${nodeName}\` plugin`,
5657
MERGING_DEFAULTS_WITH_INPUT_DATA: 'Merging defaults with input data',

src/if-run/lib/compute.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ const {
2020
COMPUTING_COMPONENT_PIPELINE,
2121
REGROUPING,
2222
OBSERVING,
23+
SKIPPING_REGROUP,
2324
} = STRINGS;
2425

26+
const childNames = new Set();
27+
2528
/**
2629
* Traverses all child nodes based on children grouping.
2730
*/
2831
const traverse = async (children: any, params: ComputeParams) => {
2932
for (const child in children) {
3033
console.debug(COMPUTING_COMPONENT_PIPELINE(child));
34+
childNames.add(child);
3135
await computeNode(children[child], params);
3236
}
3337
};
@@ -143,27 +147,40 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
143147
if ((noFlags || params.regroup) && pipelineCopy.regroup) {
144148
const originalOutputs = params.append ? node.outputs || [] : [];
145149

146-
node.children = Regroup(
147-
outputStorage,
148-
originalOutputs,
149-
pipelineCopy.regroup
150+
// Grabs all the values according to grouping criteria.
151+
const regroupValues = pipelineCopy.regroup
152+
.map(group => [...new Set(outputStorage.map(output => output[group]))])
153+
.flat();
154+
// Checks if regroup values are present in the children list.
155+
const isRegrouped = regroupValues.every(one =>
156+
[...childNames].includes(one)
150157
);
151158

152-
delete node.inputs;
153-
delete node.outputs;
159+
if (!isRegrouped) {
160+
node.children = Regroup(
161+
outputStorage,
162+
originalOutputs,
163+
pipelineCopy.regroup
164+
);
154165

155-
debugLogger.setExecutingPluginName();
156-
console.debug(REGROUPING);
166+
delete node.inputs;
167+
delete node.outputs;
157168

158-
return traverse(node.children, {
159-
...params,
160-
pipeline: {
161-
...pipelineCopy,
162-
regroup: undefined,
163-
},
164-
defaults,
165-
config,
166-
});
169+
debugLogger.setExecutingPluginName();
170+
console.debug(REGROUPING);
171+
172+
return traverse(node.children, {
173+
...params,
174+
pipeline: {
175+
...pipelineCopy,
176+
regroup: undefined,
177+
},
178+
defaults,
179+
config,
180+
});
181+
} else {
182+
console.debug(SKIPPING_REGROUP);
183+
}
167184
}
168185

169186
console.debug('\n');

0 commit comments

Comments
 (0)