소스 검색

adds efficiency to return type (#2763) (#2772)

* adds efficiency to return type

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* use db efficiency

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* test core module, provide fallback for efficiency

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* fix naming

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* fix test

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* fix test

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* addl test fixes

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* code review feedback

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* final set of code review fixes

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* test push

Signed-off-by: Alex Meijer <ameijer@kubecost.com>

* Revert "test push"

This reverts commit 5509f5c562fd73d7977c9311f7e8acb86d20ee92.

---------

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Signed-off-by: Alex Meijer <ameijer@users.noreply.github.com>
(cherry picked from commit 42256934874639c0886662ee65d5ede047f34d0d)
Alex Meijer 1 년 전
부모
커밋
c262c40c38
2개의 변경된 파일15개의 추가작업 그리고 2개의 파일을 삭제
  1. 4 1
      core/pkg/opencost/summaryallocation.go
  2. 11 1
      core/pkg/opencost/summaryallocation_json.go

+ 4 - 1
core/pkg/opencost/summaryallocation.go

@@ -40,6 +40,7 @@ type SummaryAllocation struct {
 	ExternalCost           float64               `json:"externalCost"`
 	Share                  bool                  `json:"-"`
 	UnmountedPVCost        float64               `json:"-"`
+	Efficiency             float64               `json:"efficiency"`
 }
 
 // NewSummaryAllocation converts an Allocation to a SummaryAllocation by
@@ -88,7 +89,7 @@ func NewSummaryAllocation(alloc *Allocation, reconcile, reconcileNetwork bool) *
 	if sa.IsUnmounted() {
 		sa.UnmountedPVCost = sa.PVCost
 	}
-
+	sa.Efficiency = sa.TotalEfficiency()
 	return sa
 }
 
@@ -151,6 +152,7 @@ func (sa *SummaryAllocation) Add(that *SummaryAllocation) error {
 	sa.RAMCost += that.RAMCost
 	sa.SharedCost += that.SharedCost
 
+	sa.Efficiency = sa.TotalEfficiency()
 	return nil
 }
 
@@ -173,6 +175,7 @@ func (sa *SummaryAllocation) Clone() *SummaryAllocation {
 		RAMCost:                sa.RAMCost,
 		SharedCost:             sa.SharedCost,
 		ExternalCost:           sa.ExternalCost,
+		Efficiency:             sa.Efficiency,
 	}
 }
 

+ 11 - 1
core/pkg/opencost/summaryallocation_json.go

@@ -35,6 +35,16 @@ func (sa *SummaryAllocation) ToResponse() *SummaryAllocationResponse {
 		return nil
 	}
 
+	// if the efficiency has already been set,
+	// prefer that since it has been calculated elsewhere
+	// and matches the sorting criteria more closely
+	efficiency := sa.Efficiency
+	if efficiency == 0 {
+		// if efficiency has not been set by SQL or otherwise, calculate it
+		// using the object method
+		efficiency = sa.TotalEfficiency()
+
+	}
 	return &SummaryAllocationResponse{
 		Name:                   sa.Name,
 		Start:                  sa.Start,
@@ -51,7 +61,7 @@ func (sa *SummaryAllocation) ToResponse() *SummaryAllocationResponse {
 		RAMCost:                formatutil.Float64ToResponse(sa.RAMCost),
 		SharedCost:             formatutil.Float64ToResponse(sa.SharedCost),
 		ExternalCost:           formatutil.Float64ToResponse(sa.ExternalCost),
-		TotalEfficiency:        formatutil.Float64ToResponse(sa.TotalEfficiency()),
+		TotalEfficiency:        formatutil.Float64ToResponse(efficiency),
 		TotalCost:              formatutil.Float64ToResponse(sa.TotalCost()),
 	}
 }