From 4678eec64f4daab80292c1e324ed2ce987dbaaf1 Mon Sep 17 00:00:00 2001 From: James Costerton Date: Tue, 11 Jun 2024 15:53:27 +0100 Subject: [PATCH] Refactoring result to param conversion --- .github/workflows/sonarcloud.yml | 1 + sonar-project.properties | 1 + src/converter.ts | 135 +++++++++++++++---------------- src/validator.ts | 2 +- 4 files changed, 66 insertions(+), 73 deletions(-) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index bafb6df..02b471f 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -63,5 +63,6 @@ jobs: -Dsonar.sources=src -Dsonar.tests=src/__tests__ -Dsonar.test.inclusions=src/**/*.test*.ts + -Dsonar.cpd.exclusions=src/__mocks__/*.ts -Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info projectBaseDir: . diff --git a/sonar-project.properties b/sonar-project.properties index 2ae73af..f83a961 100755 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -5,4 +5,5 @@ sonar.scm.provider=git sonar.sources=src sonar.tests=src/__tests__ sonar.test.inclusions=src/**/*.test*.ts +sonar.cpd.exclusions=src/__mocks__/*.ts sonar.javascript.lcov.reportPaths=./coverage/lcov.info \ No newline at end of file diff --git a/src/converter.ts b/src/converter.ts index 270f3ea..ee8269e 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -128,6 +128,51 @@ export const Converter = (config: YourGlobalConfig) => { return inputs; }; + /** Returns summed or component parameters for cpu and/or memory energy */ + const getEnergyOutputParams = ( + cpuEnergy?: number, + memoryEnergy?: number + ): any => { + const cpuParam = cpuEnergy ? {'cpu/energy': cpuEnergy} : {}; + const memoryParam = memoryEnergy ? {'memory/energy': memoryEnergy} : {}; + const componentParams = { + ...cpuParam, + ...memoryParam, + }; + cpuEnergy = cpuEnergy ? cpuEnergy : 0; + memoryEnergy = memoryEnergy ? memoryEnergy : 0; + return useEnergySum ? {energy: cpuEnergy + memoryEnergy} : componentParams; + }; + + /** Returns summed or component parameters for operational and embodied emissions */ + const getEmissionOutputParams = ( + operatingEmissions: number, + embodiedEmissions?: number + ): any => { + if (!includeCarbonEmissions) return {}; + + const operationalParam = {'carbon-operational': operatingEmissions}; + const embodiedParam = embodiedEmissions + ? {'carbon-embodied': embodiedEmissions} + : {}; + + const componentParams = { + ...operationalParam, + ...embodiedParam, + }; + embodiedEmissions = embodiedEmissions ? embodiedEmissions : 0; + + return useCarbonSum + ? {carbon: operatingEmissions + embodiedEmissions} + : componentParams; + }; + + /** Returns carbon intensity parameter for given electical energy */ + const getCarbonIntensityParam = (kgCO2e: number, energyKW: number): any => { + if (!includeCarbonIntensity) return {}; + return {'carbon-intensity': (kgCO2e / energyKW) * 1000}; + }; + /** Returns enriched output array based on input plugin params and VMInstance batch results */ const getVMInstanceOutputs = ( inputs: PluginParams[], @@ -137,35 +182,21 @@ export const Converter = (config: YourGlobalConfig) => { const result = response.results[index]; if (result.error) throw new Error(`Climatiq - ${result.message}`); - - const cpu_energy: number = result.calculation_details.energy_used_cpu; - const memory_energy: number = + const cpuEnergy: number = result.calculation_details.energy_used_cpu; + const memoryEnergy: number = result.calculation_details.energy_used_memory; - const energy: number = cpu_energy + memory_energy; const operatingEmissions = result.memory_estimate.co2e + result.cpu_estimate.co2e; const embodiedEmissions = result.embodied_cpu_estimate.co2e; - const carbonIntensity = getGridCarbonIntensity( - operatingEmissions, - energy - ); return { ...input, - ...(useEnergySum - ? {energy: energy} - : {'cpu/energy': cpu_energy, 'memory/energy': memory_energy}), - ...(includeCarbonEmissions - ? useCarbonSum - ? {carbon: result.total_co2e} - : { - 'carbon-operational': operatingEmissions, - 'carbon-embodied': embodiedEmissions, - } - : {}), - ...(includeCarbonIntensity - ? {'carbon-intensity': carbonIntensity} - : {}), + ...getEnergyOutputParams(cpuEnergy, memoryEnergy), + ...getEmissionOutputParams(operatingEmissions, embodiedEmissions), + ...getCarbonIntensityParam( + operatingEmissions, + cpuEnergy + memoryEnergy + ), }; }); }; @@ -179,26 +210,14 @@ export const Converter = (config: YourGlobalConfig) => { const result = response.results[index]; if (result.error) throw new Error(`Climatiq - ${result.message}`); - - const cpu_energy: number = result.activity_data.activity_value; - const energy: number = cpu_energy; + const cpuEnergy: number = result.activity_data.activity_value; const operatingEmissions = result.co2e; - const carbonIntensity = getGridCarbonIntensity( - operatingEmissions, - energy - ); return { ...input, - ...(useEnergySum ? {energy: energy} : {'cpu/energy': energy}), - ...(includeCarbonEmissions - ? useCarbonSum - ? {carbon: operatingEmissions} - : {'carbon-operational': operatingEmissions} - : {}), - ...(includeCarbonIntensity - ? {'carbon-intensity': carbonIntensity} - : {}), + ...getEnergyOutputParams(cpuEnergy, undefined), + ...getEmissionOutputParams(operatingEmissions, undefined), + ...getCarbonIntensityParam(operatingEmissions, cpuEnergy), }; }); }; @@ -212,26 +231,14 @@ export const Converter = (config: YourGlobalConfig) => { const result = response.results[index]; if (result.error) throw new Error(`Climatiq - ${result.message}`); - - const memory_energy: number = result.activity_data.activity_value; - const energy: number = memory_energy; + const memoryEnergy: number = result.activity_data.activity_value; const operatingEmissions = result.co2e; - const carbonIntensity = getGridCarbonIntensity( - operatingEmissions, - energy - ); return { ...input, - ...(useEnergySum ? {energy: energy} : {'memory/energy': memory_energy}), - ...(includeCarbonEmissions - ? useCarbonSum - ? {carbon: operatingEmissions} - : {'carbon-operational': operatingEmissions} - : {}), - ...(includeCarbonIntensity - ? {'carbon-intensity': carbonIntensity} - : {}), + ...getEnergyOutputParams(undefined, memoryEnergy), + ...getEmissionOutputParams(operatingEmissions, undefined), + ...getCarbonIntensityParam(operatingEmissions, memoryEnergy), }; }); }; @@ -245,34 +252,18 @@ export const Converter = (config: YourGlobalConfig) => { const result = response.results[index]; if (result.error) throw new Error(`Climatiq - ${result.message}`); - const energy: number = result.activity_data.activity_value; const operatingEmissions = result.co2e; - const carbonIntensity = getGridCarbonIntensity( - operatingEmissions, - energy - ); return { ...input, energy: energy, - ...(includeCarbonEmissions - ? useCarbonSum - ? {carbon: operatingEmissions} - : {'carbon-operational': operatingEmissions} - : {}), - ...(includeCarbonIntensity - ? {'carbon-intensity': carbonIntensity} - : {}), + ...getEmissionOutputParams(operatingEmissions, undefined), + ...getCarbonIntensityParam(operatingEmissions, energy), }; }); }; - /** Calculates the carbon intensity of the given electical energy */ - const getGridCarbonIntensity = (kgCO2e: number, energyKW: number): number => { - return (kgCO2e / energyKW) * 1000; - }; - return { toRequestBatch, toVMInstance, diff --git a/src/validator.ts b/src/validator.ts index df877fd..0c813c6 100755 --- a/src/validator.ts +++ b/src/validator.ts @@ -25,7 +25,7 @@ export const Validator = () => { const ERROR_RAMALLOC_REQUIRED = 'RAM Allocation parameter is required, and should be a number'; - const getZodErrorMessages = (error: z.ZodError): String => { + const getZodErrorMessages = (error: z.ZodError): string => { const messages = error.issues.map((issue: ZodIssue) => { return issue.message; });