Skip to content

Commit

Permalink
feat(lib): make small code imporovments
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Apr 2, 2024
1 parent bb3b3c5 commit 2d4f4eb
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/lib/ccf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,36 +271,27 @@ export const CloudCarbonFootprint = (
* Validates the resolved architecture using the validateAwsArchitecture method.
*/
const resolveAwsArchitecture = (architecture: string) => {
const modifyArchitecture: {[key: string]: () => void} = {
'AMD ': () => {
architecture = architecture.substring(4);
},
Skylake: () => {
architecture = 'Sky Lake';
},
Graviton: () => {
architecture = architecture.includes('2') ? 'Graviton2' : 'Graviton';
},
Unknown: () => {
architecture = 'Average';
},
let formattedArchitecture = architecture;

const architectureFormat: {[key: string]: () => string} = {
'AMD ': () => architecture.substring(4),
Skylake: () => 'Sky Lake',
Graviton: () => (architecture.includes('2') ? 'Graviton2' : 'Graviton'),
Unknown: () => 'Average',
};

Object.keys(modifyArchitecture).forEach(key => {
Object.keys(architectureFormat).forEach(key => {
if (architecture.includes(key)) {
modifyArchitecture[key]();
formattedArchitecture = architectureFormat[key]();
}
});

validateAwsArchitecture(architecture);

return architecture;
return formattedArchitecture;
};

/**
* Validates the AWS instance architecture against a predefined set of supported architectures.
*/

const validateAwsArchitecture = (architecture: string) => {
if (!(architecture in instanceUsage['aws'])) {
throw new UnsupportedValueError(
Expand Down Expand Up @@ -381,9 +372,12 @@ export const CloudCarbonFootprint = (
instance['Instance type']
] ?? ['Average'];

return architectures.map((architecture: string) =>
resolveAwsArchitecture(architecture)
);
return architectures.map((architecture: string) => {
const AWSArchitecture = resolveAwsArchitecture(architecture);
validateAwsArchitecture(AWSArchitecture);

return AWSArchitecture;
});
};

/**
Expand Down

0 comments on commit 2d4f4eb

Please sign in to comment.