Skip to content

Commit

Permalink
[1234]: adds electricity maps config and handling for AWS estimations
Browse files Browse the repository at this point in the history
  • Loading branch information
camcash17 committed Sep 12, 2023
1 parent c87c725 commit 4a33722
Show file tree
Hide file tree
Showing 18 changed files with 414 additions and 97 deletions.
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ fileignoreconfig:
checksum: fd9afa539a5991cb21416293ae9d17a57005b5bcacdd148fe9f4ce0fd3de145e
- filename: packages/common/src/Config.ts
checksum: 8cde1d40a5a56e3f1454534817d254a7b61baf2d28e185bf0c5093d8320c8b85
- filename: packages/common/src/EmissionsFactors.ts
checksum: e28dbaf55e4a2a02ec43a1adfad5b060774a5dcd1c872b9bb5e28727e26f8de7
- filename: packages/common/src/__tests__/EmissionsFactors.test.ts
checksum: b513f4119cae71648485f3f3d6795f1062f361f45028deaddfa506132a0a49cb
- filename: packages/core/package.json
checksum: c0f3b35fe6ca5f0bb14a34ccda0ed917ed97c03b116190210f4da82628891706
- filename: packages/core/src/CloudConstantsTypes.ts
Expand Down
8 changes: 5 additions & 3 deletions packages/app/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default class App {
const config = configLoader()
includeCloudProviders(cloudProviderToSeed, config)
const { AWS, GCP, AZURE, ALI } = config
if (configLoader().ELECTRICITY_MAPS_TOKEN)
appLogger.info('Using Electricity Maps')
if (process.env.TEST_MODE) {
return []
}
Expand Down Expand Up @@ -235,10 +237,10 @@ export default class App {
return allRecommendations.flat()
}

getAwsEstimatesFromInputData(
async getAwsEstimatesFromInputData(
inputData: LookupTableInput[],
): LookupTableOutput[] {
return AWSAccount.getCostAndUsageReportsDataFromInputData(inputData)
): Promise<LookupTableOutput[]> {
return await AWSAccount.getCostAndUsageReportsDataFromInputData(inputData)
}

getGcpEstimatesFromInputData(
Expand Down
6 changes: 4 additions & 2 deletions packages/aws/src/__tests__/AWSAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('AWSAccount', () => {
expect(result).toEqual(expectedEstimatesResult)
})

it('should getCostAndUsageReportsDataFromInputData', () => {
it('should getCostAndUsageReportsDataFromInputData', async () => {
const inputData: LookupTableInput[] = [
{
serviceName: 'AmazonEC2',
Expand All @@ -218,7 +218,9 @@ describe('AWSAccount', () => {
]

const AWSAccount = require('../application/AWSAccount').default
const result = AWSAccount.getCostAndUsageReportsDataFromInputData(inputData)
const result = await AWSAccount.getCostAndUsageReportsDataFromInputData(
inputData,
)

const expectedResult: LookupTableOutput[] = [
{
Expand Down
7 changes: 4 additions & 3 deletions packages/aws/src/__tests__/CostAndUsageReports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@ describe('CostAndUsageReports Service', () => {
expect(result).toEqual(expectedResult)
})

it(' successfully return lookup table data from getEstimatesFromInputData function', () => {
it(' successfully return lookup table data from getEstimatesFromInputData function', async () => {
// given
const inputData: LookupTableInput[] = [
{
Expand All @@ -2312,8 +2312,9 @@ describe('CostAndUsageReports Service', () => {
AWS_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN,
),
)
const result =
costAndUsageReportsService.getEstimatesFromInputData(inputData)
const result = await costAndUsageReportsService.getEstimatesFromInputData(
inputData,
)

// then
const expectedResult: LookupTableOutput[] = [
Expand Down
14 changes: 9 additions & 5 deletions packages/aws/src/application/AWSAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class AWSAccount extends CloudProviderAccount {
)
}

getDataFromCostAndUsageReports(
async getDataFromCostAndUsageReports(
startDate: Date,
endDate: Date,
grouping: GroupBy,
Expand All @@ -154,12 +154,16 @@ export default class AWSAccount extends CloudProviderAccount {
),
),
)
return costAndUsageReportsService.getEstimates(startDate, endDate, grouping)
return await costAndUsageReportsService.getEstimates(
startDate,
endDate,
grouping,
)
}

static getCostAndUsageReportsDataFromInputData(
static async getCostAndUsageReportsDataFromInputData(
inputData: LookupTableInput[],
): LookupTableOutput[] {
): Promise<LookupTableOutput[]> {
const costAndUsageReportsService = new CostAndUsageReports(
new ComputeEstimator(),
new StorageEstimator(AWS_CLOUD_CONSTANTS.SSDCOEFFICIENT),
Expand All @@ -171,7 +175,7 @@ export default class AWSAccount extends CloudProviderAccount {
AWS_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN,
),
)
return costAndUsageReportsService.getEstimatesFromInputData(inputData)
return await costAndUsageReportsService.getEstimatesFromInputData(inputData)
}

private getService(
Expand Down
13 changes: 7 additions & 6 deletions packages/aws/src/lib/AWSComputeEstimatesBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

import {
CloudConstants,
CloudConstantsEmissionsFactors,
ComputeEstimator,
ComputeUsage,
FootprintEstimate,
FootprintEstimatesDataBuilder,
} from '@cloud-carbon-footprint/core'
import { containsAny } from '@cloud-carbon-footprint/common'
import {
AWS_CLOUD_CONSTANTS,
AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH,
} from '../domain'
import { AWS_CLOUD_CONSTANTS } from '../domain'
import { GPU_INSTANCES_TYPES } from './AWSInstanceTypes'
import CostAndUsageReportsRow from './CostAndUsageReportsRow'
import RightsizingRecommendation from './Recommendations/Rightsizing/RightsizingTargetRecommendation'
Expand All @@ -26,6 +24,7 @@ export default class AWSComputeEstimatesBuilder extends FootprintEstimatesDataBu
| CostAndUsageReportsRow
| ComputeOptimizerRecommendationWithProcessors,
computeEstimator: ComputeEstimator,
emissionsFactors: CloudConstantsEmissionsFactors,
) {
super(rowData)

Expand All @@ -38,6 +37,7 @@ export default class AWSComputeEstimatesBuilder extends FootprintEstimatesDataBu
this.computeFootprint = this.getComputeFootprint(
computeEstimator,
this.region,
emissionsFactors,
)
}

Expand Down Expand Up @@ -78,19 +78,20 @@ export default class AWSComputeEstimatesBuilder extends FootprintEstimatesDataBu
private getComputeFootprint(
computeEstimator: ComputeEstimator,
region: string,
emissionsFactors: CloudConstantsEmissionsFactors,
): FootprintEstimate {
const computeEstimate = computeEstimator.estimate(
[this.getComputeUsage()],
region,
AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH,
emissionsFactors,
this.getComputeConstants(),
)[0]

if (this.isGpuInstance()) {
const gpuComputeEstimate = computeEstimator.estimate(
[this.getGpuComputeUsage()],
region,
AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH,
emissionsFactors,
this.getGpuComputeConstants(),
)[0]

Expand Down
11 changes: 6 additions & 5 deletions packages/aws/src/lib/AWSMemoryEstimatesBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import {
FootprintEstimatesDataBuilder,
calculateGigabyteHours,
getPhysicalChips,
CloudConstantsEmissionsFactors,
} from '@cloud-carbon-footprint/core'
import {
AWS_CLOUD_CONSTANTS,
AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH,
} from '../domain'
import { AWS_CLOUD_CONSTANTS } from '../domain'
import {
BURSTABLE_INSTANCE_BASELINE_UTILIZATION,
EC2_INSTANCE_TYPES,
Expand All @@ -33,6 +31,7 @@ export default class AWSMemoryEstimatesBuilder extends FootprintEstimatesDataBui
| CostAndUsageReportsRow
| EC2CurrentComputeOptimizerRecommendation,
memoryEstimator: MemoryEstimator,
emissionsFactors: CloudConstantsEmissionsFactors,
) {
super(rowData)

Expand All @@ -49,6 +48,7 @@ export default class AWSMemoryEstimatesBuilder extends FootprintEstimatesDataBui
this.memoryUsage,
this.memoryConstants,
this.region,
emissionsFactors,
)
}

Expand Down Expand Up @@ -135,11 +135,12 @@ export default class AWSMemoryEstimatesBuilder extends FootprintEstimatesDataBui
memoryUsage: MemoryUsage,
memoryConstants: CloudConstants,
region: string,
emissionsFactors: CloudConstantsEmissionsFactors,
): FootprintEstimate {
return memoryEstimator.estimate(
[memoryUsage],
region,
AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH,
emissionsFactors,
memoryConstants,
)[0]
}
Expand Down
34 changes: 34 additions & 0 deletions packages/aws/src/lib/AWSRegions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* © 2021 Thoughtworks, Inc.
*/

import { mappedRegionsToElectricityMapZones } from '@cloud-carbon-footprint/common'

export enum AWS_REGIONS {
US_EAST_1 = 'us-east-1',
US_EAST_2 = 'us-east-2',
Expand Down Expand Up @@ -62,3 +64,35 @@ export const AWS_MAPPED_REGION_NAMES_TO_CODES: { [key: string]: string } = {
'China (Beijing)': AWS_REGIONS.CN_NORTH_1,
'China (Ningxia)': AWS_REGIONS.CN_NORTHWEST_1,
}

export const AWS_MAPPED_REGIONS_TO_ELECTRICITY_MAPS_ZONES: mappedRegionsToElectricityMapZones =
{
[AWS_REGIONS.US_EAST_1]: 'US-MIDA-PJM',
[AWS_REGIONS.US_EAST_2]: 'US-MIDA-PJM',
[AWS_REGIONS.US_WEST_1]: 'US-CAL-CISO',
[AWS_REGIONS.US_WEST_2]: 'US-NW-PACW',
[AWS_REGIONS.AF_SOUTH_1]: 'ZA',
[AWS_REGIONS.AP_EAST_1]: null,
[AWS_REGIONS.AP_SOUTH_1]: 'IN-WE',
[AWS_REGIONS.AP_NORTHEAST_3]: 'JP-KN',
[AWS_REGIONS.AP_NORTHEAST_2]: 'KR',
[AWS_REGIONS.AP_SOUTHEAST_1]: 'SG',
[AWS_REGIONS.AP_SOUTHEAST_2]: 'AU-NSW',
[AWS_REGIONS.AP_NORTHEAST_1]: 'JP-TK',
[AWS_REGIONS.AP_SOUTHEAST_3]: 'ID',
[AWS_REGIONS.CA_CENTRAL_1]: 'CA-QC',
[AWS_REGIONS.CN_NORTH_1]: null,
[AWS_REGIONS.CN_NORTHWEST_1]: null,
[AWS_REGIONS.EU_CENTRAL_1]: 'DE',
[AWS_REGIONS.EU_WEST_1]: 'IE',
[AWS_REGIONS.EU_WEST_2]: 'GB',
[AWS_REGIONS.EU_SOUTH_1]: 'IT-NO',
[AWS_REGIONS.EU_WEST_3]: 'FR',
[AWS_REGIONS.EU_NORTH_1]: 'SE-SE3',
[AWS_REGIONS.ME_SOUTH_1]: null,
[AWS_REGIONS.ME_CENTRAL_1]: null,
[AWS_REGIONS.SA_EAST_1]: 'BR-CS',
[AWS_REGIONS.US_GOV_EAST_1]: 'US-MIDA-PJM',
[AWS_REGIONS.US_GOV_WEST_1]: 'US-NW-PACW',
[AWS_REGIONS.UNKNOWN]: null,
}
Loading

0 comments on commit 4a33722

Please sign in to comment.