@cloud-carbon-footprint/core
Version:
The core logic to get cloud usage data and estimate energy and carbon emissions.
234 lines (229 loc) • 5.8 kB
text/typescript
/*
* © 2021 Thoughtworks, Inc.
*/
import {
AccumulateKilowattHoursBy,
KilowattHoursByServiceAndUsageUnit,
} from '../../FootprintEstimate'
import BillingDataRow from '../../BillingDataRow'
const serviceOne = 'serviceOne'
const serviceTwo = 'serviceTwo'
const usageUnitOne = 'unitOne'
const usageUnitTwo = 'unitTwo'
const kilowattHours = 20
const costOrUsageAmount = 10
const getAccumulateData = (
accumulateBy: AccumulateKilowattHoursBy,
): [
KilowattHoursByServiceAndUsageUnit, // co-efficients before accumulating
Partial<BillingDataRow>, // billing data to accumulate
number, // kilowattHours to accumulate
KilowattHoursByServiceAndUsageUnit, // expected co-efficients after accumulating
][] => {
return [
// new service and usage unit
[
{
total: {},
},
{
serviceName: serviceOne,
usageUnit: usageUnitOne,
[]: costOrUsageAmount,
},
kilowattHours,
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
],
// Same service and usage unit twice
[
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
{
serviceName: serviceOne,
usageUnit: usageUnitOne,
[]: costOrUsageAmount,
},
kilowattHours,
{
[]: {
[]: {
[]: costOrUsageAmount * 2,
kilowattHours: kilowattHours * 2,
},
},
total: {
[]: {
[]: costOrUsageAmount * 2,
kilowattHours: kilowattHours * 2,
},
},
},
],
// New service and new usage unit
[
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
{
serviceName: serviceTwo,
usageUnit: usageUnitTwo,
[]: costOrUsageAmount,
},
kilowattHours,
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
],
// New service but existing usage unit
[
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
{
serviceName: serviceTwo,
usageUnit: usageUnitOne,
[]: costOrUsageAmount,
},
kilowattHours,
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount * 2,
kilowattHours: kilowattHours * 2,
},
},
},
],
// Existing service but new usage unit
[
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
{
serviceName: serviceOne,
usageUnit: usageUnitTwo,
[]: costOrUsageAmount,
},
kilowattHours,
{
[]: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
total: {
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
[]: {
[]: costOrUsageAmount,
kilowattHours: kilowattHours,
},
},
},
],
]
}
export const accumulateKilowattHoursPerCostData = getAccumulateData(
AccumulateKilowattHoursBy.COST,
)
export const accumulateKilowattHoursPerUsageAmount = getAccumulateData(
AccumulateKilowattHoursBy.USAGE_AMOUNT,
)