Skip to content

Commit

Permalink
Merge pull request #153 from Green-Software-Foundation/beautify-code
Browse files Browse the repository at this point in the history
Beautify code
  • Loading branch information
jmcook1186 authored Sep 15, 2023
2 parents 1734d6d + 51c6d11 commit 77d6763
Show file tree
Hide file tree
Showing 28 changed files with 48,863 additions and 208 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
dist/
src/**/**.d.ts
src/**/**.js
22 changes: 19 additions & 3 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
export const CONFIG = {
BOAVIZTA: {
CPU_IMPACT_MODEL_ID: 'org.boavizta.cpu.sci',
CLOUD_IMPACT_MODEL_ID: 'org.boavizta.cloud.sci',
MODEL_IDS: {
BOAVIZTA_CPU: 'org.boavizta.cpu.sci',
BOAVIZTA_CLOUD: 'org.boavizta.cloud.sci',
AVEVA: 'aveva',
EMEM: 'e-mem',
ESHOPPEN: 'org.gsf.eshoppen',
ESHOPPEN_CPU: 'org.gsf.eshoppen-cpu',
ESHOPPEN_MEM: 'org.gsf.eshoppen-mem',
ESHOPPEN_NET: 'org.gsf.eshoppen-net',
SCI_ACCENTURE: 'org.gsf.sci-o',
CCF: 'ccf.cloud.sci',
SCI: 'org.gsf.sci',
SCI_E: 'sci-e',
SCI_M: 'org.gsf.sci-m',
SCI_O: 'org.gsf.sci-o',
SHELL_MODEL: 'shellModel',
TEADS_AWS: 'teads.cloud.sci',
TEADS_CURVE: 'teads.curve',
WATT_TIME: 'org.wattime.grid',
},
VALIDATION: {
IMPL_CLI: {
Expand Down
30 changes: 13 additions & 17 deletions src/lib/boavizta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ import axios from 'axios';
import {IImpactModelInterface} from '../interfaces';
import {CONFIG} from '../../config';

import {
BoaviztaInstanceTypes,
IBoaviztaUsageSCI,
KeyValuePair,
} from '../../types/boavizta';
import {BoaviztaInstanceTypes, IBoaviztaUsageSCI} from '../../types/boavizta';
import {KeyValuePair} from '../../types/common';

export {IImpactModelInterface} from '../interfaces';

export {
BoaviztaInstanceTypes,
IBoaviztaUsageSCI,
KeyValuePair,
} from '../../types/boavizta';

const {BOAVIZTA: Index} = CONFIG;
const {CPU_IMPACT_MODEL_ID, CLOUD_IMPACT_MODEL_ID} = Index;
const {MODEL_IDS} = CONFIG;
const {BOAVIZTA_CPU, BOAVIZTA_CLOUD} = MODEL_IDS;

abstract class BoaviztaImpactModel implements IImpactModelInterface {
name: string | undefined;
Expand Down Expand Up @@ -166,7 +155,7 @@ export class BoaviztaCpuImpactModel
}

modelIdentifier(): string {
return CPU_IMPACT_MODEL_ID;
return BOAVIZTA_CPU;
}

async fetchData(usageData: object | undefined): Promise<object> {
Expand Down Expand Up @@ -222,7 +211,7 @@ export class BoaviztaCloudImpactModel
public allocation = 'LINEAR';

modelIdentifier(): string {
return CLOUD_IMPACT_MODEL_ID;
return BOAVIZTA_CLOUD;
}

async validateLocation(staticParamsCast: object) {
Expand Down Expand Up @@ -354,3 +343,10 @@ export class BoaviztaCloudImpactModel
return this.sharedParams;
}
}

/**
* For JSII.
*/
export {IImpactModelInterface} from '../interfaces';
export {BoaviztaInstanceTypes, IBoaviztaUsageSCI} from '../../types/boavizta';
export {KeyValuePair} from '../../types/common';
39 changes: 22 additions & 17 deletions src/lib/case-studies/aveva-model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {IImpactModelInterface} from '../interfaces';

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

const {MODEL_IDS} = CONFIG;
const {AVEVA} = MODEL_IDS;

export class EAvevaModel implements IImpactModelInterface {
// Defined for compatibility. Not used in Aveva.
authParams: object | undefined;
// name of the data source
name: string | undefined;
authParams: object | undefined; // Defined for compatibility. Not used in Aveva.
name: string | undefined; // name of the data source

/**
* Defined for compatibility. Not used here.
*/
Expand All @@ -13,9 +17,9 @@ export class EAvevaModel implements IImpactModelInterface {
}

/**
* Configures the Aveva Plugin for IEF
* @param {string} name name of the resource
* @param {Object} staticParams static parameters for the resource
* Configures the Aveva Plugin for IEF
* @param {string} name name of the resource
* @param {Object} staticParams static parameters for the resource
*/
async configure(
name: string,
Expand All @@ -26,37 +30,38 @@ export class EAvevaModel implements IImpactModelInterface {
if (staticParams === undefined) {
throw new Error('Required Parameters not provided');
}

return this;
}

/**
* Calculate the total emissions for a list of observations
*
* Calculate the total emissions for a list of observations.
* Each Observation require:
* @param {Object[]} observations
* @param {number} observations[].time time to normalize to in hours
* @param {number} observations[].pb baseline power
* @param {number} observations[].pl measured power
* @param {Object[]} observations
* @param {number} observations[].time time to normalize to in hours
* @param {number} observations[].pb baseline power
* @param {number} observations[].pl measured power
*/
async calculate(observations: object | object[] | undefined): Promise<any[]> {
if (observations === undefined) {
throw new Error('Required Parameters not provided');
} else if (!Array.isArray(observations)) {
throw new Error('Observations must be an array');
}
observations.map(observation => {

return observations.map(observation => {
this.configure(this.name!, observation);
observation['e-cpu'] =
((observation['pl'] - observation['pb']) * observation['time']) / 1000;

return observation;
});

return Promise.resolve(observations);
}

/**
* Returns model identifier
*/
modelIdentifier() {
return 'aveva';
return AVEVA;
}
}
47 changes: 27 additions & 20 deletions src/lib/case-studies/emem-model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {IImpactModelInterface} from '../interfaces';
import {KeyValuePair} from '../../types/boavizta';

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

import {KeyValuePair} from '../../types/common';

const {MODEL_IDS} = CONFIG;
const {EMEM} = MODEL_IDS;

export class EMemModel implements IImpactModelInterface {
// Defined for compatibility. Not used in this.
authParams: object | undefined;
// name of the data source
name: string | undefined;
authParams: object | undefined; // Defined for compatibility. Not used in this.
name: string | undefined; // name of the data source
memoryAllocation = 0;
memoryEnergy = 0;

/**
* Defined for compatibility. Not used.
*/
Expand All @@ -16,11 +21,11 @@ export class EMemModel implements IImpactModelInterface {
}

/**
* Configures the Plugin for IEF
* @param {string} name name of the resource
* @param {Object} staticParams static parameters for the resource
* @param {number} staticParams.tdp Thermal Design Power in Watts
* @param {Interpolation} staticParams.interpolation Interpolation method
* Configures the Plugin for IEF
* @param {string} name name of the resource
* @param {Object} staticParams static parameters for the resource
* @param {number} staticParams.tdp Thermal Design Power in Watts
* @param {Interpolation} staticParams.interpolation Interpolation method
*/
async configure(
name: string,
Expand All @@ -44,22 +49,24 @@ export class EMemModel implements IImpactModelInterface {
}

/**
* Calculate the total emissions for a list of observations
* Calculate the total emissions for a list of observations.
*
* Each Observation require:
* @param {Object[]} observations
* @param {string} observations[].timestamp RFC3339 timestamp string
* @param {number} observations[].mem-util percentage mem usage
* @param {Object[]} observations
* @param {string} observations[].timestamp RFC3339 timestamp string
* @param {number} observations[].mem-util percentage mem usage
*/
async calculate(observations: object | object[] | undefined): Promise<any[]> {
if (observations === undefined) {
throw new Error('Required Parameters not provided');
} else if (!Array.isArray(observations)) {
throw new Error('Observations must be an array');
}

return observations.map((observation: KeyValuePair) => {
this.configure(this.name!, observation);
observation['e-mem'] = this.calculateEnergy(observation);

return observation;
});
}
Expand All @@ -68,7 +75,7 @@ export class EMemModel implements IImpactModelInterface {
* Returns model identifier
*/
modelIdentifier(): string {
return 'e-mem';
return EMEM;
}

/**
Expand All @@ -86,26 +93,26 @@ export class EMemModel implements IImpactModelInterface {
'Required Parameters duration,cpu-util,timestamp not provided for observation'
);
}

if (this.memoryAllocation === 0) {
throw new Error(
'Required Parameter: mem-alloc not provided in configure'
);
}

if (this.memoryEnergy === 0) {
throw new Error(
'Required Parameter: mem-energy not provided in configure'
);
}

const mem_alloc = this.memoryAllocation;
// convert cpu usage to percentage
const mem_util = observation['mem-util'];
const mem_util = observation['mem-util']; // convert cpu usage to percentage

if (mem_util < 0 || mem_util > 100) {
throw new Error('cpu usage must be between 0 and 100');
}

const mem_energy = this.memoryEnergy;

return (mem_alloc * (mem_util / 100) * mem_energy) / 1000;
return (mem_alloc * (mem_util / 100) * this.memoryEnergy) / 1000;
}
}
Loading

0 comments on commit 77d6763

Please sign in to comment.