Pārlūkot izejas kodu

Merge pull request #1825 from opencost/allocation-view-api

Changes required for new Allocation View API
Niko Kovacevic 3 gadi atpakaļ
vecāks
revīzija
343e43ba6f

+ 116 - 38
pkg/kubecost/summaryallocation.go

@@ -186,6 +186,74 @@ func (sa *SummaryAllocation) CPUEfficiency() float64 {
 	return 1.0
 }
 
+func (sa *SummaryAllocation) Equal(that *SummaryAllocation) bool {
+	if sa == nil || that == nil {
+		return false
+	}
+
+	if sa.Name != that.Name {
+		return false
+	}
+
+	if sa.Start != that.Start {
+		return false
+	}
+
+	if sa.End != that.End {
+		return false
+	}
+
+	if sa.CPUCoreRequestAverage != that.CPUCoreRequestAverage {
+		return false
+	}
+
+	if sa.CPUCoreUsageAverage != that.CPUCoreUsageAverage {
+		return false
+	}
+
+	if sa.CPUCost != that.CPUCost {
+		return false
+	}
+
+	if sa.GPUCost != that.GPUCost {
+		return false
+	}
+
+	if sa.NetworkCost != that.NetworkCost {
+		return false
+	}
+
+	if sa.LoadBalancerCost != that.LoadBalancerCost {
+		return false
+	}
+
+	if sa.PVCost != that.PVCost {
+		return false
+	}
+
+	if sa.RAMBytesRequestAverage != that.RAMBytesRequestAverage {
+		return false
+	}
+
+	if sa.RAMBytesUsageAverage != that.RAMBytesUsageAverage {
+		return false
+	}
+
+	if sa.RAMCost != that.RAMCost {
+		return false
+	}
+
+	if sa.SharedCost != that.SharedCost {
+		return false
+	}
+
+	if sa.ExternalCost != that.ExternalCost {
+		return false
+	}
+
+	return true
+}
+
 func (sa *SummaryAllocation) generateKey(aggregateBy []string, labelConfig *LabelConfig) string {
 	if sa == nil {
 		return ""
@@ -1084,6 +1152,36 @@ func (sas *SummaryAllocationSet) Each(f func(string, *SummaryAllocation)) {
 	}
 }
 
+func (sas *SummaryAllocationSet) Equal(that *SummaryAllocationSet) bool {
+	if sas == nil || that == nil {
+		return false
+	}
+
+	sas.RLock()
+	defer sas.RUnlock()
+
+	if !sas.Window.Equal(that.Window) {
+		return false
+	}
+
+	if len(sas.SummaryAllocations) != len(that.SummaryAllocations) {
+		return false
+	}
+
+	for name, sa := range sas.SummaryAllocations {
+		thatSA, ok := that.SummaryAllocations[name]
+		if !ok {
+			return false
+		}
+
+		if !sa.Equal(thatSA) {
+			return false
+		}
+	}
+
+	return true
+}
+
 // IdleAllocations returns a map of the idle allocations in the AllocationSet.
 func (sas *SummaryAllocationSet) idleAllocations() map[string]*SummaryAllocation {
 	idles := map[string]*SummaryAllocation{}
@@ -1374,8 +1472,7 @@ func (sasr *SummaryAllocationSetRange) AggregateBy(aggregateBy []string, options
 
 // Append appends the given AllocationSet to the end of the range. It does not
 // validate whether or not that violates window continuity.
-func (sasr *SummaryAllocationSetRange) Append(sas *SummaryAllocationSet) error {
-
+func (sasr *SummaryAllocationSetRange) Append(sas *SummaryAllocationSet) {
 	sasr.Lock()
 	defer sasr.Unlock()
 
@@ -1394,8 +1491,6 @@ func (sasr *SummaryAllocationSetRange) Append(sas *SummaryAllocationSet) error {
 	if sasr.Window.End() == nil || (sas.Window.End() != nil && sas.Window.End().After(*sasr.Window.End())) {
 		sasr.Window.end = sas.Window.End()
 	}
-
-	return nil
 }
 
 // Each invokes the given function for each AllocationSet in the range
@@ -1513,8 +1608,7 @@ func (sasr *SummaryAllocationSetRange) Accumulate(accumulateBy AccumulateOption)
 }
 
 func (sasr *SummaryAllocationSetRange) accumulateByNone() (*SummaryAllocationSetRange, error) {
-	result, err := sasr.clone()
-	return result, err
+	return sasr.clone(), nil
 }
 
 func (sasr *SummaryAllocationSetRange) accumulateByAll() (*SummaryAllocationSetRange, error) {
@@ -1535,8 +1629,9 @@ func (sasr *SummaryAllocationSetRange) accumulateByHour() (*SummaryAllocationSet
 		return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", sasr.SummaryAllocationSets[0].Window.Duration())
 	}
 
-	result, err := sasr.clone()
-	return result, err
+	result := sasr.clone()
+
+	return result, nil
 }
 
 func (sasr *SummaryAllocationSetRange) accumulateByDay() (*SummaryAllocationSetRange, error) {
@@ -1560,10 +1655,7 @@ func (sasr *SummaryAllocationSetRange) accumulateByDay() (*SummaryAllocationSetR
 			as = as.Clone()
 		}
 
-		err := toAccumulate.Append(as)
-		if err != nil {
-			return nil, fmt.Errorf("error building accumulation: %s", err)
-		}
+		toAccumulate.Append(as)
 		sas, err := toAccumulate.accumulate()
 		if err != nil {
 			return nil, fmt.Errorf("error accumulating result: %s", err)
@@ -1574,10 +1666,7 @@ func (sasr *SummaryAllocationSetRange) accumulateByDay() (*SummaryAllocationSetR
 			if length := len(toAccumulate.SummaryAllocationSets); length != 1 {
 				return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
 			}
-			err = result.Append(toAccumulate.SummaryAllocationSets[0])
-			if err != nil {
-				return nil, fmt.Errorf("error building result accumulation: %s", err)
-			}
+			result.Append(toAccumulate.SummaryAllocationSets[0])
 			toAccumulate = nil
 		}
 	}
@@ -1601,10 +1690,7 @@ func (sasr *SummaryAllocationSetRange) accumulateByMonth() (*SummaryAllocationSe
 			as = as.Clone()
 		}
 
-		err := toAccumulate.Append(as)
-		if err != nil {
-			return nil, fmt.Errorf("error building monthly accumulation: %s", err)
-		}
+		toAccumulate.Append(as)
 
 		sas, err := toAccumulate.accumulate()
 		if err != nil {
@@ -1618,10 +1704,7 @@ func (sasr *SummaryAllocationSetRange) accumulateByMonth() (*SummaryAllocationSe
 			if length := len(toAccumulate.SummaryAllocationSets); length != 1 {
 				return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
 			}
-			err = result.Append(toAccumulate.SummaryAllocationSets[0])
-			if err != nil {
-				return nil, fmt.Errorf("error building result accumulation: %s", err)
-			}
+			result.Append(toAccumulate.SummaryAllocationSets[0])
 			toAccumulate = nil
 		}
 	}
@@ -1647,10 +1730,7 @@ func (sasr *SummaryAllocationSetRange) accumulateByWeek() (*SummaryAllocationSet
 			as = as.Clone()
 		}
 
-		err := toAccumulate.Append(as)
-		if err != nil {
-			return nil, fmt.Errorf("error building accumulation: %s", err)
-		}
+		toAccumulate.Append(as)
 		sas, err := toAccumulate.accumulate()
 		if err != nil {
 			return nil, fmt.Errorf("error accumulating result: %s", err)
@@ -1662,18 +1742,19 @@ func (sasr *SummaryAllocationSetRange) accumulateByWeek() (*SummaryAllocationSet
 			if length := len(toAccumulate.SummaryAllocationSets); length != 1 {
 				return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
 			}
-			err = result.Append(toAccumulate.SummaryAllocationSets[0])
-			if err != nil {
-				return nil, fmt.Errorf("error building result accumulation: %s", err)
-			}
+			result.Append(toAccumulate.SummaryAllocationSets[0])
 			toAccumulate = nil
 		}
 	}
 	return result, nil
 }
 
+func (sasr *SummaryAllocationSetRange) Clone() *SummaryAllocationSetRange {
+	return sasr.clone()
+}
+
 // clone returns a new SummaryAllocationSetRange cloned from the existing SASR
-func (sasr *SummaryAllocationSetRange) clone() (*SummaryAllocationSetRange, error) {
+func (sasr *SummaryAllocationSetRange) clone() *SummaryAllocationSetRange {
 	sasrSource := NewSummaryAllocationSetRange()
 	sasrSource.Window = sasr.Window.Clone()
 	sasrSource.Step = sasr.Step
@@ -1685,11 +1766,8 @@ func (sasr *SummaryAllocationSetRange) clone() (*SummaryAllocationSetRange, erro
 			sasClone = sas.Clone()
 		}
 
-		err := sasrSource.Append(sasClone)
-		if err != nil {
-			return nil, err
-		}
+		sasrSource.Append(sasClone)
 	}
-	return sasrSource, nil
 
+	return sasrSource
 }

+ 14 - 21
pkg/kubecost/summaryallocation_json.go

@@ -1,8 +1,9 @@
 package kubecost
 
 import (
-	"math"
 	"time"
+
+	"github.com/opencost/opencost/pkg/util/formatutil"
 )
 
 // SummaryAllocationResponse is a sanitized version of SummaryAllocation, which
@@ -36,27 +37,19 @@ func (sa *SummaryAllocation) ToResponse() *SummaryAllocationResponse {
 		Name:                   sa.Name,
 		Start:                  sa.Start,
 		End:                    sa.End,
-		CPUCoreRequestAverage:  float64ToResponse(sa.CPUCoreRequestAverage),
-		CPUCoreUsageAverage:    float64ToResponse(sa.CPUCoreUsageAverage),
-		CPUCost:                float64ToResponse(sa.CPUCost),
-		GPUCost:                float64ToResponse(sa.GPUCost),
-		NetworkCost:            float64ToResponse(sa.NetworkCost),
-		LoadBalancerCost:       float64ToResponse(sa.LoadBalancerCost),
-		PVCost:                 float64ToResponse(sa.PVCost),
-		RAMBytesRequestAverage: float64ToResponse(sa.RAMBytesRequestAverage),
-		RAMBytesUsageAverage:   float64ToResponse(sa.RAMBytesUsageAverage),
-		RAMCost:                float64ToResponse(sa.RAMCost),
-		SharedCost:             float64ToResponse(sa.SharedCost),
-		ExternalCost:           float64ToResponse(sa.ExternalCost),
-	}
-}
-
-func float64ToResponse(f float64) *float64 {
-	if math.IsNaN(f) || math.IsInf(f, 0) {
-		return nil
+		CPUCoreRequestAverage:  formatutil.Float64ToResponse(sa.CPUCoreRequestAverage),
+		CPUCoreUsageAverage:    formatutil.Float64ToResponse(sa.CPUCoreUsageAverage),
+		CPUCost:                formatutil.Float64ToResponse(sa.CPUCost),
+		GPUCost:                formatutil.Float64ToResponse(sa.GPUCost),
+		NetworkCost:            formatutil.Float64ToResponse(sa.NetworkCost),
+		LoadBalancerCost:       formatutil.Float64ToResponse(sa.LoadBalancerCost),
+		PVCost:                 formatutil.Float64ToResponse(sa.PVCost),
+		RAMBytesRequestAverage: formatutil.Float64ToResponse(sa.RAMBytesRequestAverage),
+		RAMBytesUsageAverage:   formatutil.Float64ToResponse(sa.RAMBytesUsageAverage),
+		RAMCost:                formatutil.Float64ToResponse(sa.RAMCost),
+		SharedCost:             formatutil.Float64ToResponse(sa.SharedCost),
+		ExternalCost:           formatutil.Float64ToResponse(sa.ExternalCost),
 	}
-
-	return &f
 }
 
 // SummaryAllocationSetResponse is a sanitized version of SummaryAllocationSet,

+ 11 - 0
pkg/util/formatutil/formatutil.go

@@ -0,0 +1,11 @@
+package formatutil
+
+import "math"
+
+func Float64ToResponse(f float64) *float64 {
+	if math.IsNaN(f) || math.IsInf(f, 0) {
+		return nil
+	}
+
+	return &f
+}

+ 16 - 0
pkg/util/timeutil/timeutil.go

@@ -453,3 +453,19 @@ func leadingInt(s string) (x int64, rem string, err error) {
 	}
 	return x, s[i:], nil
 }
+
+// EarlierOf returns the second time passed in if both are equal
+func EarlierOf(timeOne, timeTwo time.Time) time.Time {
+	if timeOne.Before(timeTwo) {
+		return timeOne
+	}
+	return timeTwo
+}
+
+// LaterOf returns the second time passed in if both are equal
+func LaterOf(timeOne, timeTwo time.Time) time.Time {
+	if timeOne.After(timeTwo) {
+		return timeOne
+	}
+	return timeTwo
+}