Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring result to param conversion #15

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: .
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
135 changes: 63 additions & 72 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand All @@ -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
),
};
});
};
Expand All @@ -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),
};
});
};
Expand All @@ -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),
};
});
};
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down