2
0
Эх сурвалжийг харах

functions in summary allocation to compute totalefficiency, RAM efficiency and CPU efficiency on summaryAllocationsets after performing accumulate

Signed-off-by: Alan Rodrigues <alanr5691@yahoo.com>
Alan Rodrigues 3 жил өмнө
parent
commit
a53e44078c

+ 95 - 0
pkg/kubecost/summaryallocation.go

@@ -1170,6 +1170,101 @@ func (sas *SummaryAllocationSet) TotalCost() float64 {
 	return tc
 }
 
+// func to calculate average RAM efficiency over SummaryAllocationSet
+func (sas *SummaryAllocationSet) RAMEfficiency() float64 {
+	if sas == nil {
+		return 0.0
+	}
+
+	sas.RLock()
+	defer sas.RUnlock()
+
+	totalRAMBytesUsage := 0.0
+	totalRAMBytesRequest := 0.0
+	for _, sa := range sas.SummaryAllocations {
+		if sa.RAMBytesRequestAverage > 0 {
+			totalRAMBytesUsage += sa.RAMBytesUsageAverage
+			totalRAMBytesRequest += sa.RAMBytesRequestAverage
+		} else {
+			if sa.RAMBytesUsageAverage == 0.0 && sa.RAMCost == 0.0 {
+				totalRAMBytesUsage += 1.0
+				totalRAMBytesRequest += 1.0
+			}
+		}
+	}
+
+	if totalRAMBytesUsage > 0 {
+		return totalRAMBytesUsage / totalRAMBytesRequest
+	}
+
+	if totalRAMBytesUsage == 0.0 || totalRAMBytesRequest == 0.0 {
+		return 0.0
+	}
+	return 1.0
+}
+
+// func to calculate average CPU efficiency over SummaryAllocationSet
+func (sas *SummaryAllocationSet) CPUEfficiency() float64 {
+	if sas == nil {
+		return 0.0
+	}
+
+	sas.RLock()
+	defer sas.RUnlock()
+
+	totalCPUCoreUsage := 0.0
+	totalCPUCoreRequest := 0.0
+	for _, sa := range sas.SummaryAllocations {
+		if sa.CPUCoreRequestAverage > 0 {
+			totalCPUCoreUsage += sa.CPUCoreUsageAverage
+			totalCPUCoreRequest += sa.CPUCoreRequestAverage
+		} else {
+			if sa.RAMBytesUsageAverage == 0.0 && sa.RAMCost == 0.0 {
+				totalCPUCoreUsage += 1.0
+				totalCPUCoreRequest += 1.0
+			}
+		}
+	}
+
+	if totalCPUCoreUsage > 0 {
+		return totalCPUCoreUsage / totalCPUCoreRequest
+	}
+
+	if totalCPUCoreUsage == 0.0 || totalCPUCoreRequest == 0.0 {
+		return 0.0
+	}
+	return 1.0
+}
+
+// func to calculate average Total efficiency over SummaryAllocationSet
+func (sas *SummaryAllocationSet) TotalEfficiency() float64 {
+	if sas == nil {
+		return 0.0
+	}
+
+	sas.RLock()
+	defer sas.RUnlock()
+
+	tc := 0.0
+	totalRAMCostEff := 0.0
+	totalCPUCostEff := 0.0
+	totalRAMCost := 0.0
+	totalCPUCost := 0.0
+	for _, sa := range sas.SummaryAllocations {
+		if sa.RAMCost+sa.CPUCost > 0 {
+			totalRAMCostEff += sa.RAMEfficiency() * sa.RAMCost
+			totalCPUCostEff += sa.CPUEfficiency() * sa.CPUCost
+			totalRAMCost += sa.RAMCost
+			totalCPUCost += sa.CPUCost
+		}
+	}
+
+	if totalRAMCost+totalCPUCost > 0 {
+		return (totalRAMCostEff + totalCPUCostEff) / (totalRAMCost + totalCPUCost)
+	}
+	return tc
+}
+
 // SummaryAllocationSetRange is a thread-safe slice of SummaryAllocationSets.
 type SummaryAllocationSetRange struct {
 	sync.RWMutex