UNPKG

@cloud-carbon-footprint/core

Version:

The core logic to get cloud usage data and estimate energy and carbon emissions.

42 lines 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EstimateUnknownUsageBy = void 0; const FootprintEstimate_1 = require("../FootprintEstimate"); var EstimateUnknownUsageBy; (function (EstimateUnknownUsageBy) { EstimateUnknownUsageBy["COST"] = "cost"; EstimateUnknownUsageBy["USAGE_AMOUNT"] = "usageAmount"; })(EstimateUnknownUsageBy = exports.EstimateUnknownUsageBy || (exports.EstimateUnknownUsageBy = {})); class UnknownEstimator { constructor(estimateKilowattHoursBy) { this.estimateKilowattHoursBy = estimateKilowattHoursBy; } estimate(data, region, emissionsFactors, constants) { return data.map((data) => { const estimatedKilowattHours = this.estimateKilowattHours(data, constants.kilowattHoursByServiceAndUsageUnit); const estimatedCo2eEmissions = (0, FootprintEstimate_1.estimateCo2)(estimatedKilowattHours, region, emissionsFactors); return { timestamp: data.timestamp, kilowattHours: estimatedKilowattHours, co2e: estimatedCo2eEmissions, usesAverageCPUConstant: false, }; }); } estimateKilowattHours(unknownUsage, kilowattHoursByServiceAndUsageUnit) { const serviceAndUsageUnit = kilowattHoursByServiceAndUsageUnit[unknownUsage.service] && kilowattHoursByServiceAndUsageUnit[unknownUsage.service][unknownUsage.usageUnit]; if (serviceAndUsageUnit) return ((serviceAndUsageUnit.kilowattHours / serviceAndUsageUnit[this.estimateKilowattHoursBy]) * unknownUsage[this.estimateKilowattHoursBy]); const totalForUsageUnit = kilowattHoursByServiceAndUsageUnit.total[unknownUsage.usageUnit]; if (totalForUsageUnit) return ((totalForUsageUnit.kilowattHours / totalForUsageUnit[this.estimateKilowattHoursBy]) * unknownUsage[this.estimateKilowattHoursBy]); return 0; } } exports.default = UnknownEstimator; //# sourceMappingURL=UnknownEstimator.js.map