Skip to content

Commit

Permalink
feat: architecture type filter (#842)
Browse files Browse the repository at this point in the history
* chore: rollback eslint to v8

* fix: ec2-info with architecture metadata

* feat: architecture type filter

* test: update tests
  • Loading branch information
hoonoh authored May 16, 2024
1 parent d855f8a commit 45682b7
Show file tree
Hide file tree
Showing 11 changed files with 3,005 additions and 1,072 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test": "jest --runInBand --verbose",
"test:coverage": "yarn test --coverage",
"test:ci": "yarn test:coverage --reporters=default jest-junit --coverageReporters lcov --coverageReporters cobertura",
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint \"**/*.ts\"",
"lint": "eslint \"**/*.ts\"",
"types": "tsc",
"semantic-release": "semantic-release"
},
Expand Down Expand Up @@ -77,7 +77,7 @@
"cz-conventional-changelog": "3.3.0",
"dts-bundle-generator": "9.4.0",
"esbuild": "0.21.3",
"eslint": "9.2.0",
"eslint": "8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-airbnb-typescript": "18.0.0",
"eslint-config-prettier": "9.1.0",
Expand Down
7 changes: 4 additions & 3 deletions scripts/generate-ec2-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { writeFileSync } from 'fs';
import { resolve } from 'path';
import prettier from 'prettier';

import { Ec2InstanceInfo } from '../src/constants/ec2-info';
import { defaultRegions } from '../src/constants/regions';
import { getEc2Info } from '../src/lib/core';

(async () => {
const res = (
await Promise.all(defaultRegions.map(async region => getEc2Info({ region, log: true })))
).reduce((rtn, cur) => ({ ...rtn, ...cur }), {} as { vCpu?: number; memoryGiB?: number });
).reduce((rtn, cur) => ({ ...rtn, ...cur }), {} as Ec2InstanceInfo);
const sorted = Object.fromEntries(Object.entries(res).sort(([a], [b]) => -(a < b)));
console.log(`found ${Object.keys(sorted).length} instance types`);

let output =
`import { _InstanceType } from '@aws-sdk/client-ec2';\n\n` +
`export type Ec2InstanceInfo = { vCpu?: number; memoryGiB?: number };\n\n`;
`import { type _InstanceType, type ArchitectureType } from '@aws-sdk/client-ec2';\n\n` +
`export type Ec2InstanceInfo = { vCpu?: number; memoryGiB?: number; architectures?: ArchitectureType[] };\n\n`;
output += `export const ec2Info: Record<_InstanceType | string, Ec2InstanceInfo> = ${JSON.stringify(
sorted,
)};`;
Expand Down
34 changes: 32 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArchitectureType } from '@aws-sdk/client-ec2';
import ora from 'ora';
import { sep } from 'path';
import { ColumnUserConfig, Indexable, table } from 'table';
Expand Down Expand Up @@ -105,6 +106,14 @@ export const main = (argvInput?: string[]): Promise<void> =>
choices: [...allPlatforms, ...(Object.keys(platformWildcards) as PlatformsWildcards[])],
default: defaults.platforms,
},
architectures: {
alias: 'a',
describe: 'Architectures',
type: 'array',
choices: Object.keys(ArchitectureType) as ArchitectureType[],
string: true,
default: defaults.architectures,
},
limit: {
alias: 'l',
describe: 'Limit results output length',
Expand Down Expand Up @@ -160,6 +169,7 @@ export const main = (argvInput?: string[]): Promise<void> =>
minMemoryGiB,
priceLimit,
platforms,
architectures,
json,
accessKeyId,
secretAccessKey,
Expand Down Expand Up @@ -213,6 +223,11 @@ export const main = (argvInput?: string[]): Promise<void> =>
});
}

const architecturesSet = new Set<ArchitectureType>();
if (architectures?.length) {
architectures.forEach(a => architecturesSet.add(a));
}

if (accessKeyId && !secretAccessKey) {
console.log('`secretAccessKey` missing.');
rej();
Expand All @@ -232,6 +247,7 @@ export const main = (argvInput?: string[]): Promise<void> =>
});

const platformsSetArray = Array.from(platformsSet);
const architecturesSetArray = Array.from(architecturesSet);
const familyTypeSetArray = Array.from(familyTypeSet);
const sizeSetArray = Array.from(sizeSet);

Expand Down Expand Up @@ -278,6 +294,7 @@ export const main = (argvInput?: string[]): Promise<void> =>
minMemoryGiB,
priceLimit,
platforms: platformsSetArray,
architectures: architecturesSetArray,
accessKeyId,
secretAccessKey,
onRegionFetch,
Expand All @@ -301,29 +318,41 @@ export const main = (argvInput?: string[]): Promise<void> =>
let tableFormat: Indexable<ColumnUserConfig> | undefined;

if (!wide) {
tableHeader = [['Type', 'Price', 'Platform', 'Availability Zone']];
tableHeader = [['Type', 'Price', 'Platform', 'Architecture', 'Availability Zone']];
tableData = results.map(info => [
info.instanceType,
info.spotPrice.toFixed(spotPriceToFixedLen),
info.platform,
info.architectures?.join(', '),
info.availabilityZone,
]);
tableFormat = {
0: { alignment: 'left' },
1: { alignment: 'right' },
2: { alignment: 'left' },
3: { alignment: 'left' },
4: { alignment: 'left' },
};
} else {
tableHeader = [
['Type', 'Price', 'vCPU', 'RAM', 'Platform', 'Availability Zone', 'Region'],
[
'Type',
'Price',
'vCPU',
'RAM',
'Platform',
'Architecture',
'Availability Zone',
'Region',
],
];
tableData = results.map(info => [
info.instanceType,
info.spotPrice.toFixed(spotPriceToFixedLen),
info.vCpu ? info.vCpu.toString() : 'unknown',
info.memoryGiB ? info.memoryGiB.toString() : 'unknown',
info.platform,
info.architectures?.join(', '),
info.availabilityZone,
info.availabilityZone
? regionNames[info.availabilityZone.slice(0, -1) as Region]
Expand All @@ -337,6 +366,7 @@ export const main = (argvInput?: string[]): Promise<void> =>
4: { alignment: 'left' },
5: { alignment: 'left' },
6: { alignment: 'left' },
7: { alignment: 'left' },
};
}
console.log(
Expand Down
Loading

0 comments on commit 45682b7

Please sign in to comment.