@cloud-carbon-footprint/gcp
Version:
The core logic to get cloud usage data and estimate energy and carbon emissions from Google Cloud Platform.
80 lines • 5.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const monitoring_1 = require("@google-cloud/monitoring");
const bigquery_1 = require("@google-cloud/bigquery");
const resource_manager_1 = require("@google-cloud/resource-manager");
const recommender_1 = require("@google-cloud/recommender");
const compute_1 = require("@google-cloud/compute");
const google_auth_library_1 = require("google-auth-library");
const core_1 = require("@cloud-carbon-footprint/core");
const common_1 = require("@cloud-carbon-footprint/common");
const ServiceWrapper_1 = __importDefault(require("../lib/ServiceWrapper"));
const lib_1 = require("../lib");
const domain_1 = require("../domain");
class GCPAccount extends core_1.CloudProviderAccount {
id;
name;
regions;
constructor(id, name, regions) {
super();
this.id = id;
this.name = name;
this.regions = regions;
}
async getDataForRegions(startDate, endDate, grouping) {
const estimationResults = await Promise.all(this.regions.map(async (regionId) => {
return await this.getDataForRegion(regionId, startDate, endDate, grouping);
}));
return estimationResults.flat();
}
async getDataForRegion(regionId, startDate, endDate, grouping) {
const gcpServices = this.getServices();
const gcpConstants = {
minWatts: domain_1.GCP_CLOUD_CONSTANTS.MIN_WATTS_MEDIAN,
maxWatts: domain_1.GCP_CLOUD_CONSTANTS.MAX_WATTS_MEDIAN,
powerUsageEffectiveness: domain_1.GCP_CLOUD_CONSTANTS.getPUE(),
};
const region = new core_1.Region(regionId, gcpServices, (0, domain_1.getGCPEmissionsFactors)(), gcpConstants);
return await this.getRegionData('GCP', region, startDate, endDate, grouping);
}
async getDataFromBillingExportTable(startDate, endDate, grouping) {
const billingExportTableService = new lib_1.BillingExportTable(new core_1.ComputeEstimator(), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.SSDCOEFFICIENT), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.HDDCOEFFICIENT), new core_1.NetworkingEstimator(domain_1.GCP_CLOUD_CONSTANTS.NETWORKING_COEFFICIENT), new core_1.MemoryEstimator(domain_1.GCP_CLOUD_CONSTANTS.MEMORY_COEFFICIENT), new core_1.UnknownEstimator(domain_1.GCP_CLOUD_CONSTANTS.ESTIMATE_UNKNOWN_USAGE_BY), new core_1.EmbodiedEmissionsEstimator(domain_1.GCP_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN), new bigquery_1.BigQuery({ projectId: this.id }));
return await billingExportTableService.getEstimates(startDate, endDate, grouping);
}
static async getBillingExportDataFromInputData(inputData) {
const billingExportTableService = new lib_1.BillingExportTable(new core_1.ComputeEstimator(), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.SSDCOEFFICIENT), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.HDDCOEFFICIENT), new core_1.NetworkingEstimator(domain_1.GCP_CLOUD_CONSTANTS.NETWORKING_COEFFICIENT), new core_1.MemoryEstimator(domain_1.GCP_CLOUD_CONSTANTS.MEMORY_COEFFICIENT), new core_1.UnknownEstimator(domain_1.GCP_CLOUD_CONSTANTS.ESTIMATE_UNKNOWN_USAGE_BY), new core_1.EmbodiedEmissionsEstimator(domain_1.GCP_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN));
return await billingExportTableService.getEstimatesFromInputData(inputData);
}
getServices() {
return (0, common_1.configLoader)().GCP.CURRENT_SERVICES.map(({ key }) => {
return this.getService(key);
});
}
async getDataForRecommendations() {
const auth = new google_auth_library_1.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
const googleAuthClient = await auth.getClient();
const serviceWrapper = new ServiceWrapper_1.default(new resource_manager_1.ProjectsClient(), googleAuthClient, new compute_1.InstancesClient(), new compute_1.DisksClient(), new compute_1.AddressesClient(), new compute_1.ImagesClient(), new compute_1.MachineTypesClient(), new recommender_1.RecommenderClient());
const recommendations = new lib_1.Recommendations(new core_1.ComputeEstimator(), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.HDDCOEFFICIENT), new core_1.StorageEstimator(domain_1.GCP_CLOUD_CONSTANTS.SSDCOEFFICIENT), serviceWrapper);
return await recommendations.getRecommendations();
}
getService(key) {
if (this.services[key] === undefined)
throw new Error('Unsupported service: ' + key);
const options = {
projectId: this.id,
};
return this.services[key](options);
}
services = {
computeEngine: (options) => {
return new lib_1.ComputeEngine(new monitoring_1.v3.MetricServiceClient(options));
},
};
}
exports.default = GCPAccount;
//# sourceMappingURL=GCPAccount.js.map