UNPKG

@cloud-carbon-footprint/aws

Version:

The core logic to get cloud usage data and estimate energy and carbon emissions from Amazon Web Services.

136 lines 6.03 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceWrapper = void 0; const csvtojson_1 = __importDefault(require("csvtojson")); const ramda_1 = require("ramda"); const common_1 = require("@cloud-carbon-footprint/common"); class ServiceWrapper { constructor(cloudWatch, cloudWatchLogs, costExplorer, s3, athena, glue) { this.cloudWatch = cloudWatch; this.cloudWatchLogs = cloudWatchLogs; this.costExplorer = costExplorer; this.s3 = s3; this.athena = athena; this.glue = glue; this.checkForPartialData = (array) => { const isPartialData = array.some((obj) => obj.StatusCode === 'PartialData'); if (isPartialData) { throw new common_1.PartialDataError('Partial Data Returned from AWS'); } }; } async getCostAndUsageResponse(params) { return [await this.costExplorer.getCostAndUsage(params).promise()]; } async getMetricDataResponse(params) { return [await this.cloudWatch.getMetricData(params).promise()]; } async getRightsizingRecommendationResponse(params) { return [ await this.costExplorer.getRightsizingRecommendation(params).promise(), ]; } async getAthenaQueryResults(queryExecutionInput) { return [await this.athena.getQueryResults(queryExecutionInput).promise()]; } async getQueryByInterval(intervalInDays, func, start, end, ...args) { let startCopy = new Date(start); let endCopy = new Date(new Date(start).setDate(start.getDate() + intervalInDays)); const promiseArray = []; while (endCopy < end) { promiseArray.push(func(startCopy, endCopy, ...args)); startCopy = new Date(new Date(startCopy).setDate(start.getDate() + intervalInDays)); endCopy = new Date(new Date(startCopy).setDate(start.getDate() + intervalInDays)); } promiseArray.push(func(startCopy, end, ...args)); return Promise.all(promiseArray); } async getCloudWatchLogQueryResults(params) { return await this.cloudWatchLogs.getQueryResults(params).promise(); } async describeLogGroups(params) { return await this.cloudWatchLogs.describeLogGroups(params).promise(); } async describeCloudWatchLogsQueries(params) { return await this.cloudWatchLogs.describeQueries(params).promise(); } async startCloudWatchLogsQuery(params) { return await this.cloudWatchLogs.startQuery(params).promise(); } async startAthenaQueryExecution(queryParams) { return await this.athena.startQueryExecution(queryParams).promise(); } async getAthenaQueryExecution(queryExecutionInput) { return await this.athena.getQueryExecution(queryExecutionInput).promise(); } async getAthenaQueryResultSets(queryExecutionInput) { return await this.getAthenaQueryResults(queryExecutionInput); } async getCostAndUsageResponses(params) { const response = await this.getCostAndUsageResponse(params); return response; } async getMetricDataResponses(params) { const response = await this.getMetricDataResponse(params); this.checkForPartialData(response[0].MetricDataResults); return response; } async getRightsizingRecommendationsResponses(params) { return await this.getRightsizingRecommendationResponse(params); } async listBucketObjects(params) { return await this.s3.listObjectsV2(params).promise(); } async getComputeOptimizerRecommendationsResponse(params) { const stream = this.s3.getObject(params).createReadStream(); const parsedCsv = await (0, csvtojson_1.default)().fromStream(stream); return JSON.parse(JSON.stringify(parsedCsv)); } async getAthenaTableDescription(params) { return await this.glue.getTable(params).promise(); } } __decorate([ enablePagination('NextToken') ], ServiceWrapper.prototype, "getAthenaQueryResultSets", null); __decorate([ enablePagination('NextPageToken') ], ServiceWrapper.prototype, "getCostAndUsageResponses", null); __decorate([ enablePagination('NextToken') ], ServiceWrapper.prototype, "getMetricDataResponses", null); __decorate([ enablePagination('NextPageToken') ], ServiceWrapper.prototype, "getRightsizingRecommendationsResponses", null); exports.ServiceWrapper = ServiceWrapper; function enablePagination(nextPageProperty) { return (target, propertyKey, descriptor) => { const originalMethod = descriptor.value; descriptor.value = async function (props) { const responses = []; let latestResponse; do { const args = [ { ...props, [nextPageProperty]: (0, ramda_1.path)([responses.length - 1, nextPageProperty], responses), }, ]; latestResponse = (await originalMethod.apply(this, args))[0]; responses.push(latestResponse); } while ((0, ramda_1.path)([nextPageProperty], latestResponse)); return responses; }; return descriptor; }; } //# sourceMappingURL=ServiceWrapper.js.map