Skip to content

Commit 0eb4834

Browse files
authored
Merge pull request #1065 from Green-Software-Foundation/update-explain-feature
Update explain feature
2 parents f4259da + c2bb2ac commit 0eb4834

File tree

5 files changed

+169
-125
lines changed

5 files changed

+169
-125
lines changed

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

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ describe('lib/explain: ', () => {
1313
const mockData = {
1414
pluginName: 'divide',
1515
metadata: {kind: 'execute', inputs: undefined, outputs: undefined},
16-
pluginData: {
17-
path: 'builtin',
18-
method: 'Divide',
19-
},
2016
};
2117

2218
addExplainData(mockData);
@@ -50,30 +46,29 @@ describe('lib/explain: ', () => {
5046
},
5147
},
5248
},
53-
pluginData: {
54-
path: 'builtin',
55-
method: 'Sum',
56-
},
5749
};
5850

5951
const expectedResult = {
60-
'cpu/energy': {
61-
plugins: ['sum'],
62-
unit: 'kWh',
63-
description: 'energy consumed by the cpu',
64-
'aggregation-method': 'sum',
65-
},
66-
'network/energy': {
67-
plugins: ['sum'],
68-
unit: 'kWh',
69-
description: 'energy consumed by data ingress and egress',
70-
'aggregation-method': 'sum',
71-
},
72-
'energy-sum': {
73-
plugins: ['sum'],
74-
unit: 'kWh',
75-
description: 'sum of energy components',
76-
'aggregation-method': 'sum',
52+
sum: {
53+
inputs: {
54+
'cpu/energy': {
55+
unit: 'kWh',
56+
description: 'energy consumed by the cpu',
57+
'aggregation-method': 'sum',
58+
},
59+
'network/energy': {
60+
unit: 'kWh',
61+
description: 'energy consumed by data ingress and egress',
62+
'aggregation-method': 'sum',
63+
},
64+
},
65+
outputs: {
66+
'energy-sum': {
67+
unit: 'kWh',
68+
description: 'sum of energy components',
69+
'aggregation-method': 'sum',
70+
},
71+
},
7772
},
7873
};
7974

@@ -111,42 +106,50 @@ describe('lib/explain: ', () => {
111106
},
112107
},
113108
},
114-
pluginData: {
115-
path: 'builtin',
116-
method: 'Sum',
117-
},
118109
};
119110

120111
const expectedResult = {
121-
'cpu/energy': {
122-
plugins: ['sum', 'sum-energy'],
123-
unit: 'kWh',
124-
description: 'energy consumed by the cpu',
125-
'aggregation-method': 'sum',
126-
},
127-
'network/energy': {
128-
plugins: ['sum'],
129-
unit: 'kWh',
130-
description: 'energy consumed by data ingress and egress',
131-
'aggregation-method': 'sum',
132-
},
133-
'energy-sum': {
134-
plugins: ['sum'],
135-
unit: 'kWh',
136-
description: 'sum of energy components',
137-
'aggregation-method': 'sum',
138-
},
139-
'memory/energy': {
140-
plugins: ['sum-energy'],
141-
unit: 'kWh',
142-
description: 'energy consumed by data from memory',
143-
'aggregation-method': 'sum',
112+
sum: {
113+
inputs: {
114+
'cpu/energy': {
115+
unit: 'kWh',
116+
description: 'energy consumed by the cpu',
117+
'aggregation-method': 'sum',
118+
},
119+
'network/energy': {
120+
unit: 'kWh',
121+
description: 'energy consumed by data ingress and egress',
122+
'aggregation-method': 'sum',
123+
},
124+
},
125+
outputs: {
126+
'energy-sum': {
127+
unit: 'kWh',
128+
description: 'sum of energy components',
129+
'aggregation-method': 'sum',
130+
},
131+
},
144132
},
145-
'total/energy': {
146-
plugins: ['sum-energy'],
147-
unit: 'kWh',
148-
description: 'sum of energy components',
149-
'aggregation-method': 'sum',
133+
'sum-energy': {
134+
inputs: {
135+
'cpu/energy': {
136+
unit: 'kWh',
137+
description: 'energy consumed by the cpu',
138+
'aggregation-method': 'sum',
139+
},
140+
'memory/energy': {
141+
unit: 'kWh',
142+
description: 'energy consumed by data from memory',
143+
'aggregation-method': 'sum',
144+
},
145+
},
146+
outputs: {
147+
'total/energy': {
148+
unit: 'kWh',
149+
description: 'sum of energy components',
150+
'aggregation-method': 'sum',
151+
},
152+
},
150153
},
151154
};
152155

@@ -184,10 +187,6 @@ describe('lib/explain: ', () => {
184187
},
185188
},
186189
},
187-
pluginData: {
188-
path: 'builtin',
189-
method: 'Sum',
190-
},
191190
};
192191

193192
expect.assertions(2);
@@ -239,10 +238,6 @@ describe('lib/explain: ', () => {
239238
},
240239
},
241240
},
242-
pluginData: {
243-
path: 'builtin',
244-
method: 'Sum',
245-
},
246241
};
247242

248243
expect.assertions(2);

src/common/config/strings.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Incubation projects are experimental, offer no support guarantee, have minimal g
1111
MANIFEST_IS_MISSING: 'Manifest is missing.',
1212
DIRECTORY_NOT_FOUND: 'Directory not found.',
1313
AGGREGATION_UNITS_NOT_MATCH: (param: string) =>
14-
`Your manifest uses two instances of ${param} with different units. Please check that you are using consistent units for ${param} throughout your manifest.`,
14+
`Your manifest uses two instances of \`${param}\` with different units. Please check that you are using consistent units for \`${param}\` throughout your manifest.`,
1515
AGGREGATION_METHODS_NOT_MATCH: (param: string) =>
16-
`Your manifest uses two instances of ${param} with different 'aggregation-method'. Please check that you are using right 'aggregation-method' for ${param} throughout your manifest.`,
16+
`Your manifest uses two instances of \`${param}\` with different 'aggregation-method'. Please check that you are using right 'aggregation-method' for \`${param}\` throughout your manifest.`,
17+
MISSING_INPUTS_PARAMETER: (pluginName: string) =>
18+
`The inputs parameter metadata of the \`${pluginName}\` plugin is missing.`,
19+
MISSING_OUTPUTS_PARAMETER: (pluginName: string) =>
20+
`The outputs parameter metadata of the \`${pluginName}\` plugin is missing.`,
1721
};

src/if-run/lib/compute.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
132132
addExplainData({
133133
pluginName,
134134
metadata: plugin.metadata,
135-
pluginData: params.context.initialize!.plugins[pluginName],
136135
});
137136
}
138137
}
@@ -184,6 +183,7 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
184183
debugLogger.setExecutingPluginName(pluginName);
185184

186185
outputStorage = await plugin.execute(outputStorage, nodeConfig);
186+
187187
debugLogger.setExecutingPluginName();
188188

189189
node.outputs = outputStorage;
@@ -192,7 +192,6 @@ const computeNode = async (node: Node, params: ComputeParams): Promise<any> => {
192192
addExplainData({
193193
pluginName,
194194
metadata: plugin.metadata,
195-
pluginData: params.context.initialize!.plugins[pluginName],
196195
});
197196
}
198197
}

src/if-run/lib/explain.ts

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,133 @@
1+
import {ParameterMetadata} from '@grnsft/if-core/types';
12
import {ERRORS} from '@grnsft/if-core/utils';
23

34
import {STRINGS} from '../../common/config';
45

6+
import {logger} from '../../common/util/logger';
7+
58
import {ExplainParams, ExplainStorageType} from '../types/explain';
69

710
const {ManifestValidationError} = ERRORS;
8-
const {AGGREGATION_UNITS_NOT_MATCH, AGGREGATION_METHODS_NOT_MATCH} = STRINGS;
11+
const {
12+
AGGREGATION_UNITS_NOT_MATCH,
13+
AGGREGATION_METHODS_NOT_MATCH,
14+
MISSING_INPUTS_PARAMETER,
15+
MISSING_OUTPUTS_PARAMETER,
16+
} = STRINGS;
917

1018
/**
1119
* Retrieves stored explain data.
1220
*/
13-
export const explain = () => storeExplainData.parameters;
21+
export const explain = () => storeExplainData.plugins;
1422

1523
/**
1624
* Manages the storage of explain data.
1725
*/
1826
const storeExplainData = (() => {
19-
let parameter: ExplainStorageType = {};
27+
let plugins: ExplainStorageType = {};
2028

21-
const parameterManager = {
22-
get parameters() {
23-
return parameter;
29+
const pluginManager = {
30+
get plugins() {
31+
return plugins;
2432
},
25-
set parameters(value: ExplainStorageType) {
26-
parameter = value;
33+
set plugins(value: ExplainStorageType) {
34+
plugins = value;
2735
},
2836
};
2937

30-
return parameterManager;
38+
return pluginManager;
3139
})();
3240

3341
/**
3442
* Adds new explain data to the stored explain data.
3543
*/
3644
export const addExplainData = (params: ExplainParams) => {
37-
const {pluginName, pluginData, metadata} = params;
38-
const parameterMetadata = pluginData?.['parameter-metadata'] || metadata;
39-
const parameters = storeExplainData.parameters;
40-
const allParameters = {
41-
...parameterMetadata?.inputs,
42-
...parameterMetadata?.outputs,
43-
} as ExplainStorageType;
44-
45-
Object.entries(allParameters).forEach(([name, meta]) => {
46-
const existingParameter = parameters[name];
47-
48-
if (parameters[name]?.plugins?.includes(pluginName)) {
49-
return;
50-
}
45+
const {pluginName, metadata} = params;
46+
const plugin: ExplainStorageType = {
47+
[pluginName]: {
48+
inputs: metadata?.inputs ?? {},
49+
outputs: metadata?.outputs ?? {},
50+
},
51+
};
5152

52-
if (existingParameter) {
53-
if (meta.unit !== existingParameter.unit) {
54-
throw new ManifestValidationError(AGGREGATION_UNITS_NOT_MATCH(name));
55-
}
53+
const isInputsMissing = !Object.keys(plugin[pluginName].inputs || {}).length;
54+
const isOutputsMissing = !Object.keys(plugin[pluginName].outputs || {})
55+
.length;
56+
57+
if (isInputsMissing) {
58+
delete plugin[pluginName].inputs;
59+
60+
logger.warn(MISSING_INPUTS_PARAMETER(pluginName));
61+
}
62+
63+
if (isOutputsMissing) {
64+
delete plugin[pluginName].outputs;
65+
66+
logger.warn(MISSING_OUTPUTS_PARAMETER(pluginName));
67+
}
68+
69+
checkMetadatas(metadata);
70+
71+
if (!isInputsMissing || !isOutputsMissing) {
72+
storeExplainData.plugins = {
73+
...storeExplainData.plugins,
74+
...plugin,
75+
};
76+
}
77+
};
5678

57-
if (
58-
meta['aggregation-method'].component !==
59-
existingParameter['aggregation-method'].component ||
60-
meta['aggregation-method'].time !==
61-
existingParameter['aggregation-method'].time
62-
) {
63-
throw new ManifestValidationError(AGGREGATION_METHODS_NOT_MATCH(name));
79+
/**
80+
* Checks if the 'unit' and 'aggregation-method' of the parameter are the same throughout the manifest
81+
*/
82+
const checkMetadatas = (metadata: {
83+
inputs?: ParameterMetadata;
84+
outputs?: ParameterMetadata;
85+
}) => {
86+
const inputsOutputsMetadata = {...metadata?.inputs, ...metadata?.outputs};
87+
const storedParameters: any = {};
88+
89+
// Populate stored parameters with metadata from each plugin
90+
Object.values(storeExplainData.plugins).forEach(plugin => {
91+
const storedInputOutputMetadata = {...plugin.inputs, ...plugin.outputs};
92+
93+
Object.keys(storedInputOutputMetadata).forEach(parameter => {
94+
if (!storedParameters[parameter]) {
95+
storedParameters[parameter] = {
96+
unit: storedInputOutputMetadata[parameter].unit,
97+
'aggregation-method':
98+
storedInputOutputMetadata[parameter]['aggregation-method'],
99+
};
64100
}
101+
});
102+
});
103+
104+
// Validate input-output metadata against stored parameters
105+
Object.keys(inputsOutputsMetadata).forEach(parameterName => {
106+
const parameter = inputsOutputsMetadata[parameterName];
107+
const storedParameter = storedParameters[parameterName];
65108

66-
existingParameter.plugins.push(pluginName);
67-
existingParameter.description =
68-
meta.description || existingParameter.description;
69-
} else {
70-
parameters[name] = {
71-
plugins: [pluginName],
72-
unit: meta.unit,
73-
description: meta.description,
74-
'aggregation-method': meta['aggregation-method'],
75-
};
109+
if (
110+
parameter &&
111+
Object.keys(storedParameters).includes(parameterName) &&
112+
storedParameter.unit !== parameter.unit
113+
) {
114+
throw new ManifestValidationError(
115+
AGGREGATION_UNITS_NOT_MATCH(parameterName)
116+
);
76117
}
77-
});
78118

79-
storeExplainData.parameters = {
80-
...parameters,
81-
};
119+
// Check for aggregation-method mismatch
120+
const inputAggregation = parameter['aggregation-method'];
121+
122+
if (
123+
storedParameter &&
124+
(storedParameter['aggregation-method']?.component !==
125+
inputAggregation?.component ||
126+
storedParameter['aggregation-method']?.time !== inputAggregation?.time)
127+
) {
128+
throw new ManifestValidationError(
129+
AGGREGATION_METHODS_NOT_MATCH(parameterName)
130+
);
131+
}
132+
});
82133
};

0 commit comments

Comments
 (0)