Kaynağa Gözat

Merge pull request #1173 from kubecost/niko/aggstore

AggregatedStore
Niko Kovacevic 4 yıl önce
ebeveyn
işleme
e4aba61d65

+ 5 - 0
pkg/kubecost/asset.go

@@ -2712,6 +2712,11 @@ func (as *AssetSet) Map() map[string]Asset {
 	return as.Clone().assets
 }
 
+// Resolution returns the AssetSet's window duration
+func (as *AssetSet) Resolution() time.Duration {
+	return as.Window.Duration()
+}
+
 func (as *AssetSet) Set(asset Asset, aggregateBy []string) error {
 	if as.IsEmpty() {
 		as.Lock()

+ 29 - 27
pkg/kubecost/query.go

@@ -34,37 +34,39 @@ type CloudUsageQuerier interface {
 
 // AllocationQueryOptions defines optional parameters for querying an Allocation Store
 type AllocationQueryOptions struct {
-	Accumulate        bool
-	AccumulateBy      time.Duration
-	AggregateBy       []string
-	Compute           bool
-	FilterFuncs       []AllocationMatchFunc
-	IdleByNode        bool
-	IncludeExternal   bool
-	IncludeIdle       bool
-	LabelConfig       *LabelConfig
-	MergeUnallocated  bool
-	Reconcile         bool
-	ReconcileNetwork  bool
-	ShareFuncs        []AllocationMatchFunc
-	SharedHourlyCosts map[string]float64
-	ShareIdle         string
-	ShareSplit        string
-	ShareTenancyCosts bool
-	SplitIdle         bool
-	Step              time.Duration
+	Accumulate              bool
+	AccumulateBy            time.Duration
+	AggregateBy             []string
+	Compute                 bool
+	DisableAggregatedStores bool
+	FilterFuncs             []AllocationMatchFunc
+	IdleByNode              bool
+	IncludeExternal         bool
+	IncludeIdle             bool
+	LabelConfig             *LabelConfig
+	MergeUnallocated        bool
+	Reconcile               bool
+	ReconcileNetwork        bool
+	ShareFuncs              []AllocationMatchFunc
+	SharedHourlyCosts       map[string]float64
+	ShareIdle               string
+	ShareSplit              string
+	ShareTenancyCosts       bool
+	SplitIdle               bool
+	Step                    time.Duration
 }
 
 // AssetQueryOptions defines optional parameters for querying an Asset Store
 type AssetQueryOptions struct {
-	Accumulate         bool
-	AggregateBy        []string
-	Compute            bool
-	DisableAdjustments bool
-	FilterFuncs        []AssetMatchFunc
-	IncludeCloud       bool
-	SharedHourlyCosts  map[string]float64
-	Step               time.Duration
+	Accumulate              bool
+	AggregateBy             []string
+	Compute                 bool
+	DisableAdjustments      bool
+	DisableAggregatedStores bool
+	FilterFuncs             []AssetMatchFunc
+	IncludeCloud            bool
+	SharedHourlyCosts       map[string]float64
+	Step                    time.Duration
 }
 
 // CloudUsageQueryOptions define optional parameters for querying a Store

+ 59 - 0
pkg/util/timeutil/profile.go

@@ -0,0 +1,59 @@
+package timeutil
+
+import (
+	"fmt"
+	"strings"
+	"time"
+)
+
+type ProfileDataSeries struct {
+	Name   string
+	Series []*ProfileDatum
+}
+
+func NewProfileDataSeries(name string, steps int) *ProfileDataSeries {
+	return &ProfileDataSeries{
+		Name:   name,
+		Series: make([]*ProfileDatum, 0, steps+2),
+	}
+}
+
+func (pds *ProfileDataSeries) Start() {
+	pds.Series = append(pds.Series, &ProfileDatum{
+		Name: "start",
+		Time: time.Now().UTC(),
+	})
+}
+
+func (pds *ProfileDataSeries) Step(name string) {
+	pds.Series = append(pds.Series, &ProfileDatum{
+		Name: name,
+		Time: time.Now().UTC(),
+	})
+}
+
+func (pds *ProfileDataSeries) Stop() {
+	pds.Series = append(pds.Series, &ProfileDatum{
+		Name: "stop",
+		Time: time.Now().UTC(),
+	})
+}
+
+func (pds *ProfileDataSeries) String() string {
+	if pds == nil || len(pds.Series) < 2 {
+		return "--"
+	}
+
+	var sb strings.Builder
+	sb.WriteString(fmt.Sprintf("%s %v", pds.Name, pds.Series[len(pds.Series)-1].Time.Sub(pds.Series[0].Time)))
+	for i := 1; i < len(pds.Series); i++ {
+		pd := pds.Series[i]
+		sb.WriteString(fmt.Sprintf(" [%s %v]", pd.Name, pds.Series[i].Time.Sub(pds.Series[i-1].Time)))
+	}
+	return sb.String()
+}
+
+type ProfileDatum struct {
+	Name string
+	Time time.Time
+}

+ 52 - 0
pkg/util/timeutil/profile_test.go

@@ -0,0 +1,52 @@
+package timeutil
+
+import (
+	"fmt"
+	"testing"
+	"time"
+)
+
+func TestProfileDataSeries_NilOrZero(t *testing.T) {
+	pds1 := NewProfileDataSeries("test1", 0)
+	fmt.Println(pds1.String())
+
+	pds2 := NewProfileDataSeries("test2", 0)
+	pds2.Start()
+	fmt.Println(pds2.String())
+
+	pds2.Stop()
+	fmt.Println(pds2.String())
+}
+
+func TestProfileDataSeries_Series(t *testing.T) {
+	pds := NewProfileDataSeries("test", 3)
+
+	pds.Start()
+
+	time.Sleep(10 * time.Millisecond)
+
+	pds.Step("step1")
+
+	time.Sleep(100 * time.Millisecond)
+
+	pds.Step("step2")
+
+	time.Sleep(5 * time.Millisecond)
+
+	pds.Step("step3")
+
+	time.Sleep(1 * time.Millisecond)
+
+	pds.Stop()
+
+	fmt.Println(len(pds.Series))
+	for i, p := range pds.Series {
+		if p == nil {
+			fmt.Printf("%d nil\n", i)
+		} else {
+			fmt.Printf("%d %s %v\n", i, p.Name, p.Time)
+		}
+	}
+
+	fmt.Println(pds.String())
+}