diff --git a/package-lock.json b/package-lock.json index c11cceb0d..663d3b063 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,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", @@ -1186,9 +1186,9 @@ } }, "node_modules/@grnsft/if-core": { - "version": "0.0.17", - "resolved": "https://registry.npmjs.org/@grnsft/if-core/-/if-core-0.0.17.tgz", - "integrity": "sha512-t94QvQgP4qomJoSqPUy70Th271/3b3E8+FfmW0Fv00je5Lb5OVAzF1FAD1GdP1UkoXJkbklO99d6hbTMCZSHlw==", + "version": "0.0.18", + "resolved": "https://registry.npmjs.org/@grnsft/if-core/-/if-core-0.0.18.tgz", + "integrity": "sha512-fZVGADUFf+9CQm+upGwGbQ5zlj7mndxZJ/Bfx2V/8+diU//xvK2vACC/7a1M0pHHhP8tKbfgdGopwWUI3POzcw==", "dependencies": { "typescript": "^5.1.6", "zod": "^3.23.8" diff --git a/package.json b/package.json index 8a86b90ee..63ab8bf07 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/common/util/helpers.test.ts b/src/__tests__/common/util/helpers.test.ts index 387689371..11838100c 100644 --- a/src/__tests__/common/util/helpers.test.ts +++ b/src/__tests__/common/util/helpers.test.ts @@ -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(): ', () => { @@ -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); - }); - }); }); diff --git a/src/common/util/helpers.ts b/src/common/util/helpers.ts index 3a1f623ef..01b87c79d 100644 --- a/src/common/util/helpers.ts +++ b/src/common/util/helpers.ts @@ -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`. @@ -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 = 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); -}; diff --git a/src/if-run/builtins/coefficient/index.ts b/src/if-run/builtins/coefficient/index.ts index 289b4d7be..0a002f67a 100644 --- a/src/if-run/builtins/coefficient/index.ts +++ b/src/if-run/builtins/coefficient/index.ts @@ -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, @@ -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'; diff --git a/src/if-run/builtins/copy-param/index.ts b/src/if-run/builtins/copy-param/index.ts index 2128cff7d..fec1fd850 100644 --- a/src/if-run/builtins/copy-param/index.ts +++ b/src/if-run/builtins/copy-param/index.ts @@ -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, @@ -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; diff --git a/src/if-run/builtins/csv-lookup/index.ts b/src/if-run/builtins/csv-lookup/index.ts index 373cbdeb0..f66981b97 100644 --- a/src/if-run/builtins/csv-lookup/index.ts +++ b/src/if-run/builtins/csv-lookup/index.ts @@ -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, @@ -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, diff --git a/src/if-run/builtins/divide/index.ts b/src/if-run/builtins/divide/index.ts index ba02194bb..767fd1d6d 100644 --- a/src/if-run/builtins/divide/index.ts +++ b/src/if-run/builtins/divide/index.ts @@ -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, @@ -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; diff --git a/src/if-run/builtins/exponent/index.ts b/src/if-run/builtins/exponent/index.ts index 38bc07d49..199f44b17 100644 --- a/src/if-run/builtins/exponent/index.ts +++ b/src/if-run/builtins/exponent/index.ts @@ -1,4 +1,8 @@ import {z} from 'zod'; +import { + mapConfigIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -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'; diff --git a/src/if-run/builtins/interpolation/index.ts b/src/if-run/builtins/interpolation/index.ts index 1bd1cf48b..c7c9887f7 100644 --- a/src/if-run/builtins/interpolation/index.ts +++ b/src/if-run/builtins/interpolation/index.ts @@ -1,6 +1,10 @@ import Spline from 'typescript-cubic-spline'; import {z} from 'zod'; import {ERRORS} from '@grnsft/if-core/utils'; +import { + mapConfigIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -11,10 +15,6 @@ import { } from '@grnsft/if-core/types'; import {validate} from '../../../common/util/validations'; -import { - mapConfigIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/mock-observations/index.ts b/src/if-run/builtins/mock-observations/index.ts index 7b1dfee89..1aa72d303 100644 --- a/src/if-run/builtins/mock-observations/index.ts +++ b/src/if-run/builtins/mock-observations/index.ts @@ -1,5 +1,10 @@ import {DateTime, Duration} from 'luxon'; import {z} from 'zod'; +import {ERRORS} from '@grnsft/if-core/utils'; +import { + mapConfigIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -8,13 +13,8 @@ import { PluginParametersMetadata, MappingParams, } from '@grnsft/if-core/types'; -import {ERRORS} from '@grnsft/if-core/utils'; import {validate} from '../../../common/util/validations'; -import { - mapConfigIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/multiply/index.ts b/src/if-run/builtins/multiply/index.ts index e33ee1f15..530cb77a1 100644 --- a/src/if-run/builtins/multiply/index.ts +++ b/src/if-run/builtins/multiply/index.ts @@ -1,4 +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, @@ -6,13 +11,8 @@ import { PluginParametersMetadata, MappingParams, } from '@grnsft/if-core/types'; -import {ERRORS} from '@grnsft/if-core/utils'; import {validate} from '../../../common/util/validations'; -import { - mapConfigIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/regex/index.ts b/src/if-run/builtins/regex/index.ts index 9f017e8e8..5e65e677f 100644 --- a/src/if-run/builtins/regex/index.ts +++ b/src/if-run/builtins/regex/index.ts @@ -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, @@ -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'; diff --git a/src/if-run/builtins/sci-embodied/index.ts b/src/if-run/builtins/sci-embodied/index.ts index 1aad045a5..c6c953b50 100644 --- a/src/if-run/builtins/sci-embodied/index.ts +++ b/src/if-run/builtins/sci-embodied/index.ts @@ -1,4 +1,8 @@ import {z} from 'zod'; +import { + mapInputIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, ParameterMetadata, @@ -10,10 +14,6 @@ import { import {validate, allDefined} from '../../../common/util/validations'; import {STRINGS} from '../../config'; -import { - mapInputIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; const {SCI_EMBODIED_ERROR} = STRINGS; diff --git a/src/if-run/builtins/sci/index.ts b/src/if-run/builtins/sci/index.ts index dbafa6352..407621b86 100644 --- a/src/if-run/builtins/sci/index.ts +++ b/src/if-run/builtins/sci/index.ts @@ -1,5 +1,9 @@ import {z} from 'zod'; import {ERRORS} from '@grnsft/if-core/utils'; +import { + mapInputIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -10,10 +14,6 @@ import { } from '@grnsft/if-core/types'; import {validate, allDefined} from '../../../common/util/validations'; -import { - mapInputIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/shell/index.ts b/src/if-run/builtins/shell/index.ts index 8926a266e..ddf8b619d 100644 --- a/src/if-run/builtins/shell/index.ts +++ b/src/if-run/builtins/shell/index.ts @@ -3,6 +3,7 @@ import {spawnSync, SpawnSyncReturns} from 'child_process'; import {loadAll, dump} from 'js-yaml'; import {z} from 'zod'; import {ERRORS} from '@grnsft/if-core/utils'; +import {mapOutputIfNeeded} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -12,7 +13,6 @@ import { } from '@grnsft/if-core/types'; import {validate} from '../../../common/util/validations'; -import {mapOutputIfNeeded} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/subtract/index.ts b/src/if-run/builtins/subtract/index.ts index d4dc1181a..5051017c5 100644 --- a/src/if-run/builtins/subtract/index.ts +++ b/src/if-run/builtins/subtract/index.ts @@ -1,4 +1,9 @@ import {z} from 'zod'; +import {ERRORS} from '@grnsft/if-core/utils'; +import { + mapConfigIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, MappingParams, @@ -6,13 +11,8 @@ import { PluginParams, SubtractConfig, } from '@grnsft/if-core/types'; -import {ERRORS} from '@grnsft/if-core/utils'; import {validate} from '../../../common/util/validations'; -import { - mapConfigIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; import {STRINGS} from '../../config'; diff --git a/src/if-run/builtins/sum/index.ts b/src/if-run/builtins/sum/index.ts index eb0e519a5..580821ddb 100644 --- a/src/if-run/builtins/sum/index.ts +++ b/src/if-run/builtins/sum/index.ts @@ -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, @@ -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'; diff --git a/src/if-run/builtins/time-converter/index.ts b/src/if-run/builtins/time-converter/index.ts index e757ed967..c4b9ef577 100644 --- a/src/if-run/builtins/time-converter/index.ts +++ b/src/if-run/builtins/time-converter/index.ts @@ -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, @@ -13,10 +17,6 @@ import {validate} from '../../../common/util/validations'; import {STRINGS} from '../../config'; import {TIME_UNITS_IN_SECONDS} from './config'; -import { - mapConfigIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; const {ConfigError} = ERRORS; const {MISSING_CONFIG} = STRINGS; diff --git a/src/if-run/builtins/time-sync/index.ts b/src/if-run/builtins/time-sync/index.ts index 5d909bf77..f72cae045 100644 --- a/src/if-run/builtins/time-sync/index.ts +++ b/src/if-run/builtins/time-sync/index.ts @@ -3,6 +3,10 @@ import {isDate} from 'node:util/types'; import {Settings, DateTime, DateTimeMaybeValid, Interval} from 'luxon'; import {z} from 'zod'; import {ERRORS} from '@grnsft/if-core/utils'; +import { + mapInputIfNeeded, + mapOutputIfNeeded, +} from '@grnsft/if-core/utils/helpers'; import { ExecutePlugin, PluginParams, @@ -18,10 +22,6 @@ import {validate} from '../../../common/util/validations'; import {STRINGS} from '../../config'; import {getAggregationMethod} from '../../lib/aggregate'; -import { - mapInputIfNeeded, - mapOutputIfNeeded, -} from '../../../common/util/helpers'; Settings.defaultZone = 'utc';