Переглянути джерело

only compute cost if total cost > 0

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer 3 роки тому
батько
коміт
4643a0f0bb

+ 4 - 1
pkg/costmodel/router.go

@@ -278,11 +278,14 @@ func WrapData(data interface{}, err error) []byte {
 			Data:    data,
 			Data:    data,
 		})
 		})
 	} else {
 	} else {
-		resp, _ = json.Marshal(&Response{
+		resp, err = json.Marshal(&Response{
 			Code:   http.StatusOK,
 			Code:   http.StatusOK,
 			Status: "success",
 			Status: "success",
 			Data:   data,
 			Data:   data,
 		})
 		})
+		if err != nil {
+			log.Errorf("error marshaling response json: %s", err.Error())
+		}
 	}
 	}
 
 
 	return resp
 	return resp

+ 9 - 3
pkg/kubecost/allocation.go

@@ -1761,9 +1761,15 @@ func deriveProportionalAssetResourceCostsFromIdleCoefficients(idleCoeffs map[str
 
 
 	// compute how much each component (cpu, gpu, ram) contributes to the overall price
 	// compute how much each component (cpu, gpu, ram) contributes to the overall price
 	totalCost := totals[idleId]["ram"] + totals[idleId]["gpu"] + totals[idleId]["cpu"]
 	totalCost := totals[idleId]["ram"] + totals[idleId]["gpu"] + totals[idleId]["cpu"]
-	ramFraction := totals[idleId]["ram"] / totalCost
-	cpuFraction := totals[idleId]["cpu"] / totalCost
-	gpuFraction := totals[idleId]["gpu"] / totalCost
+
+	var ramFraction, cpuFraction, gpuFraction float64
+
+	// only compute fraction if totalCost is nonzero, otherwise returns in NaN
+	if totalCost > 0 {
+		ramFraction = totals[idleId]["ram"] / totalCost
+		cpuFraction = totals[idleId]["cpu"] / totalCost
+		gpuFraction = totals[idleId]["gpu"] / totalCost
+	}
 
 
 	// compute the resource usage percentage based on the weighted fractions
 	// compute the resource usage percentage based on the weighted fractions
 	resourcePercentage := (ramPct * ramFraction) + (cpuPct * cpuFraction) + (gpuPct * gpuFraction)
 	resourcePercentage := (ramPct * ramFraction) + (cpuPct * cpuFraction) + (gpuPct * gpuFraction)

+ 3 - 2
pkg/kubecost/allocation_json.go

@@ -5,6 +5,7 @@ import (
 	"math"
 	"math"
 	"time"
 	"time"
 
 
+	"github.com/opencost/opencost/pkg/log"
 	"github.com/opencost/opencost/pkg/util/json"
 	"github.com/opencost/opencost/pkg/util/json"
 )
 )
 
 
@@ -53,8 +54,8 @@ type AllocationJSON struct {
 	SharedCost                     *float64                        `json:"sharedCost"`
 	SharedCost                     *float64                        `json:"sharedCost"`
 	TotalCost                      *float64                        `json:"totalCost"`
 	TotalCost                      *float64                        `json:"totalCost"`
 	TotalEfficiency                *float64                        `json:"totalEfficiency"`
 	TotalEfficiency                *float64                        `json:"totalEfficiency"`
-	RawAllocationOnly              *RawAllocationOnlyData          `json:"rawAllocationOnly,omitEmpty"`
-	ProportionalAssetResourceCosts *ProportionalAssetResourceCosts `json:"proportionalAssetResourceCosts,omitEmpty"`
+	RawAllocationOnly              *RawAllocationOnlyData          `json:"rawAllocationOnly,omitempty"`
+	ProportionalAssetResourceCosts *ProportionalAssetResourceCosts `json:"proportionalAssetResourceCosts,omitempty"`
 }
 }
 
 
 func (aj *AllocationJSON) BuildFromAllocation(a *Allocation) {
 func (aj *AllocationJSON) BuildFromAllocation(a *Allocation) {