Skip to content

Commit

Permalink
Fix overage calculation to handle negative utilization and simplify a…
Browse files Browse the repository at this point in the history
…nnual cost estimation
  • Loading branch information
rbjornstad committed Dec 17, 2024
1 parent d954577 commit 99cd626
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/lib/utils/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function yearlyOverageCost(
const costPerCpuCorePerYear = 136.69;
const costPerBytePerYear = 18.71 / 1024 / 1024 / 1024;

const overage = request - request * (utilization / 100);
const overage = request - request * (Math.abs(utilization) / 100);

let cost = 0.0;

Expand Down Expand Up @@ -101,23 +101,25 @@ export function mergeCalculateAndSortOverageData(
throw new Error(`No corresponding CPU data found for ${memItem.workload.name}`);
}

const estimatedAnnualOverageCost =
yearlyOverageCost(
UtilizationResourceType.CPU,
cpuItem.requested,
cpuItem.used / cpuItem.requested
) +
yearlyOverageCost(
UtilizationResourceType.MEMORY,
memItem.requested,
memItem.used / memItem.requested
);

// Combine the memory and CPU data into one object
return {
name: memItem.workload.name,
env: memItem.workload.environment.name,
unusedMem: memItem.requested - memItem.used,
unusedCpu: cpuItem.requested - cpuItem.used,
estimatedAnnualOverageCost:
yearlyOverageCost(
UtilizationResourceType.CPU,
cpuItem.requested,
cpuItem.used / cpuItem.requested
) +
yearlyOverageCost(
UtilizationResourceType.MEMORY,
memItem.requested,
memItem.used / memItem.requested
)
estimatedAnnualOverageCost: estimatedAnnualOverageCost
};
})
.sort((a, b) => {
Expand Down

0 comments on commit 99cd626

Please sign in to comment.