Skip to content

Commit

Permalink
Merge pull request #978 from Green-Software-Foundation/move-mapping-f…
Browse files Browse the repository at this point in the history
…unctions

Move mapping functions
  • Loading branch information
jmcook1186 committed Aug 22, 2024
2 parents a8c2c12 + d908767 commit 8222e47
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 291 deletions.
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.17",
"@grnsft/if-core": "^0.0.18",
"axios": "^1.7.2",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.4.6",
Expand Down
155 changes: 1 addition & 154 deletions src/__tests__/common/util/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ jest.mock('node:readline/promises', () =>
require('../../../__mocks__/readline')
);

import {
parseManifestFromStdin,
mapInputIfNeeded,
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';
import {parseManifestFromStdin} from '../../../common/util/helpers';

describe('common/util/helpers: ', () => {
describe('parseManifestFromStdin(): ', () => {
Expand Down Expand Up @@ -46,152 +41,4 @@ describe('common/util/helpers: ', () => {
expect(response).toEqual(expectedMessage);
});
});

describe('mapInputIfNeeded(): ', () => {
it('returns a new object with no changes when mapping is empty.', () => {
const input = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};
const mapping = {};

const result = mapInputIfNeeded(input, mapping);

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

it('returns a new object with keys remapped according to the mapping.', () => {
const input = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/carbon-footprint': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};
const mapping = {'device/emissions-embodied': 'device/carbon-footprint'};

const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 60 * 60 * 24 * 30,
'device/emissions-embodied': 200,
'device/expected-lifespan': 60 * 60 * 24 * 365 * 4,
'resources-reserved': 1,
'resources-total': 1,
};

const result = mapInputIfNeeded(input, mapping);

expect(result).toEqual(expectedOutput);
expect(result).not.toHaveProperty('device/carbon-footprint');
});
});

describe('mapConfigIfNeeded', () => {
it('returns the config as is if no mapping is provided.', () => {
const config = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/utilized',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};

const nullMapping = null;
expect(mapConfigIfNeeded(config, nullMapping!)).toEqual(config);

const undefinedMapping = undefined;
expect(mapConfigIfNeeded(config, undefinedMapping!)).toEqual(config);
});

it('recursively maps config keys and values according to the mapping.', () => {
const config = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/utilized',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};
const mapping = {
'cpu/utilized': 'cpu/util',
};

const expected = {
filepath: './file.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/util',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: ['cpu-tdp', 'tdp'],
};
expect(mapConfigIfNeeded(config, mapping)).toEqual(expected);
});

it('returns an empty object or array when config is an empty object or array.', () => {
expect(mapConfigIfNeeded({}, {})).toEqual({});
expect(mapConfigIfNeeded([], {})).toEqual([]);
});
});

describe('mapOutputIfNeeded(): ', () => {
const output = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 1,
'network/energy': 1,
'memory/energy': 1,
};
it('returns provided `output` if `mapping` is not valid.', () => {
const mapping = undefined;
const mappedOutput = mapOutputIfNeeded(output, mapping!);

expect.assertions(1);
expect(mappedOutput).toEqual(output);
});

it('returns mapped output if `mapping` has data.', () => {
const mapping = {
'cpu/energy': 'energy-from-cpu',
'network/energy': 'energy-from-network',
};
const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'energy-from-cpu': 1,
'energy-from-network': 1,
'memory/energy': 1,
};
const mappedOutput = mapOutputIfNeeded(output, mapping);

expect.assertions(1);
expect(mappedOutput).toEqual(expectedOutput);
});

it('returns the correct mapped output if some properties are mismatched.', () => {
const mapping = {
'mock-cpu/energy': 'energy-from-cpu',
'network/energy': 'energy-from-network',
};
const expectedOutput = {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu/energy': 1,
'energy-from-network': 1,
'memory/energy': 1,
};
const mappedOutput = mapOutputIfNeeded(output, mapping);

expect.assertions(1);
expect(mappedOutput).toEqual(expectedOutput);
});
});
});
68 changes: 0 additions & 68 deletions src/common/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {createInterface} from 'node:readline/promises';
import {exec} from 'child_process';
import * as path from 'path';
import {promisify} from 'util';
import {MappingParams, PluginParams} from '@grnsft/if-core/types';

/**
* Promise version of Node's `exec` from `child-process`.
Expand Down Expand Up @@ -64,70 +63,3 @@ export const parseManifestFromStdin = async () => {

return match![1];
};

/**
* Maps input data if the mapping has valid data.
*/
export const mapInputIfNeeded = (
input: PluginParams,
mapping: MappingParams
) => {
const newInput = Object.assign({}, input);

Object.entries(mapping || {}).forEach(([key, value]) => {
if (value in newInput) {
const mappedKey = input[value];
newInput[key] = mappedKey;
delete newInput[value];
}
});

return newInput;
};

/**
* Maps config data if the mapping hass valid data.
*/
export const mapConfigIfNeeded = (config: any, mapping: MappingParams) => {
if (!mapping) {
return config;
}

if (typeof config !== 'object' || config === null) {
return config;
}

const result: Record<string, any> = Array.isArray(config) ? [] : {};

Object.entries(config).forEach(([key, value]) => {
const mappedKey = mapping[key] || key;

if (typeof value === 'object' && value !== null) {
result[mappedKey] = mapConfigIfNeeded(value, mapping);
} else {
result[mappedKey] =
typeof value === 'string' && value in mapping ? mapping[value] : value;
}
});

return result;
};

/**
* Maps the output parameter of the plugin if the `mapping` parameter is provided.
*/
export const mapOutputIfNeeded = (
output: PluginParams,
mapping: MappingParams
) => {
if (!mapping) return output;

return Object.entries(output).reduce((acc, [key, value]) => {
if (key in mapping) {
acc[mapping[key]] = value;
} else {
acc[key] = value;
}
return acc;
}, {} as PluginParams);
};
8 changes: 4 additions & 4 deletions src/if-run/builtins/coefficient/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {z} from 'zod';
import {ERRORS} from '@grnsft/if-core/utils';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
CoefficientConfig,
ExecutePlugin,
Expand All @@ -9,10 +13,6 @@ import {
} from '@grnsft/if-core/types';

import {validate} from '../../../common/util/validations';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';

import {STRINGS} from '../../config';

Expand Down
8 changes: 4 additions & 4 deletions src/if-run/builtins/copy-param/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {z} from 'zod';
import {ERRORS} from '@grnsft/if-core/utils';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
ConfigParams,
ExecutePlugin,
Expand All @@ -11,10 +15,6 @@ import {
import {validate} from '../../../common/util/validations';

import {STRINGS} from '../../config';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';

const {MISSING_CONFIG} = STRINGS;
const {ConfigError} = ERRORS;
Expand Down
8 changes: 4 additions & 4 deletions src/if-run/builtins/csv-lookup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import axios from 'axios';
import {z} from 'zod';
import {parse} from 'csv-parse/sync';
import {ERRORS} from '@grnsft/if-core/utils';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
ExecutePlugin,
MappingParams,
Expand All @@ -15,10 +19,6 @@ import {
import {validate} from '../../../common/util/validations';

import {STRINGS} from '../../config';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';

const {
FILE_FETCH_FAILED,
Expand Down
8 changes: 4 additions & 4 deletions src/if-run/builtins/divide/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {z} from 'zod';
import {ERRORS} from '@grnsft/if-core/utils';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
ExecutePlugin,
PluginParams,
Expand All @@ -11,10 +15,6 @@ import {
import {validate} from '../../../common/util/validations';

import {STRINGS} from '../../config';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';

const {ConfigError, MissingInputDataError} = ERRORS;
const {MISSING_CONFIG, MISSING_INPUT_DATA, ZERO_DIVISION} = STRINGS;
Expand Down
8 changes: 4 additions & 4 deletions src/if-run/builtins/exponent/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {z} from 'zod';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '@grnsft/if-core/utils/helpers';
import {
ExecutePlugin,
PluginParams,
Expand All @@ -9,10 +13,6 @@ import {
import {ERRORS} from '@grnsft/if-core/utils';

import {validate} from '../../../common/util/validations';
import {
mapConfigIfNeeded,
mapOutputIfNeeded,
} from '../../../common/util/helpers';

import {STRINGS} from '../../config';

Expand Down
Loading

0 comments on commit 8222e47

Please sign in to comment.