Преглед изворни кода

Merge pull request #2620 from ameijer/props-intersect

Cliff Colvin пре 2 година
родитељ
комит
e3ecfe9b84

+ 0 - 19
pkg/cmd/costmodel/costmodel.go

@@ -9,7 +9,6 @@ import (
 
 	"github.com/julienschmidt/httprouter"
 	"github.com/opencost/opencost/pkg/cloudcost"
-	"github.com/opencost/opencost/pkg/customcost"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"github.com/rs/cors"
 
@@ -58,21 +57,6 @@ func Execute(opts *CostModelOpts) error {
 		a.CloudCostQueryService = cloudcost.NewQueryService(repoQuerier, repoQuerier)
 	}
 
-	log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
-	if env.IsCustomCostEnabled() {
-		hourlyRepo := customcost.NewMemoryRepository()
-		dailyRepo := customcost.NewMemoryRepository()
-		ingConfig := customcost.DefaultIngestorConfiguration()
-		var err error
-		a.CustomCostPipelineService, err = customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
-		if err != nil {
-			return fmt.Errorf("error instantiating custom cost pipeline service: %v", err)
-		}
-
-		customCostQuerier := customcost.NewQuerier(hourlyRepo, dailyRepo, ingConfig.HourlyDuration, ingConfig.DailyDuration)
-		a.CustomCostQueryService = customcost.NewQueryService(customCostQuerier)
-	}
-
 	rootMux := http.NewServeMux()
 	a.Router.GET("/healthz", Healthz)
 
@@ -91,9 +75,6 @@ func Execute(opts *CostModelOpts) error {
 	a.Router.GET("/cloudCost/rebuild", a.CloudCostPipelineService.GetCloudCostRebuildHandler())
 	a.Router.GET("/cloudCost/repair", a.CloudCostPipelineService.GetCloudCostRepairHandler())
 
-	a.Router.GET("/customCost/total", a.CustomCostQueryService.GetCustomCostTotalHandler())
-	a.Router.GET("/customCost/timeseries", a.CustomCostQueryService.GetCustomCostTimeseriesHandler())
-
 	if env.IsPProfEnabled() {
 		a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
 		a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)

+ 19 - 0
pkg/costmodel/router.go

@@ -1772,6 +1772,22 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 		a.MetricsEmitter.Start()
 	}
 
+	log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
+	if env.IsCustomCostEnabled() {
+		hourlyRepo := customcost.NewMemoryRepository()
+		dailyRepo := customcost.NewMemoryRepository()
+		ingConfig := customcost.DefaultIngestorConfiguration()
+		var err error
+		a.CustomCostPipelineService, err = customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
+		if err != nil {
+			log.Errorf("error instantiating custom cost pipeline service: %v", err)
+			return nil
+		}
+
+		customCostQuerier := customcost.NewQuerier(hourlyRepo, dailyRepo, ingConfig.HourlyDuration, ingConfig.DailyDuration)
+		a.CustomCostQueryService = customcost.NewQueryService(customCostQuerier)
+	}
+
 	a.Router.GET("/costDataModel", a.CostDataModel)
 	a.Router.GET("/costDataModelRange", a.CostDataModelRange)
 	a.Router.GET("/aggregatedCostModel", a.AggregateCostModelHandler)
@@ -1830,6 +1846,9 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 	a.Router.GET("/cloud/config/disable", a.CloudConfigController.GetDisableConfigHandler())
 	a.Router.GET("/cloud/config/delete", a.CloudConfigController.GetDeleteConfigHandler())
 
+	a.Router.GET("/customCost/total", a.CustomCostQueryService.GetCustomCostTotalHandler())
+	a.Router.GET("/customCost/timeseries", a.CustomCostQueryService.GetCustomCostTimeseriesHandler())
+
 	a.httpServices.RegisterAll(a.Router)
 
 	return a

+ 1 - 1
pkg/customcost/memoryrepository.go

@@ -53,7 +53,7 @@ func (m *MemoryRepository) Get(startTime time.Time, domain string) (*pb.CustomCo
 	ccr := &pb.CustomCostResponse{}
 	err := proto.Unmarshal(b, ccr)
 	if err != nil {
-		return nil, fmt.Errorf("error unmarshalling data: %w")
+		return nil, fmt.Errorf("error unmarshalling data: %w", err)
 	}
 	return ccr, nil
 }

+ 0 - 4
pkg/customcost/props.go

@@ -27,10 +27,6 @@ func ParseCustomCostProperties(props []string) ([]string, error) {
 		}
 	}
 
-	if len(properties) == 0 {
-		properties = []string{string(CustomCostDomainProp)}
-	}
-
 	return properties, nil
 }
 

+ 50 - 2
pkg/customcost/types.go

@@ -101,7 +101,54 @@ func (cc *CustomCost) Add(other *CustomCost) {
 	cc.BilledCost += other.BilledCost
 	cc.ListCost += other.ListCost
 	cc.ListUnitPrice += other.ListUnitPrice
-	cc.UsageQuantity += other.UsageQuantity
+
+	if cc.Id != other.Id {
+		cc.Id = ""
+	}
+
+	if cc.Zone != other.Zone {
+		cc.Zone = ""
+	}
+
+	if cc.AccountName != other.AccountName {
+		cc.AccountName = ""
+	}
+
+	if cc.ChargeCategory != other.ChargeCategory {
+		cc.ChargeCategory = ""
+	}
+
+	if cc.Description != other.Description {
+		cc.Description = ""
+	}
+
+	if cc.ResourceName != other.ResourceName {
+		cc.ResourceName = ""
+	}
+
+	if cc.ResourceType != other.ResourceType {
+		cc.ResourceType = ""
+	}
+
+	if cc.ProviderId != other.ProviderId {
+		cc.ProviderId = ""
+	}
+
+	if cc.UsageUnit != other.UsageUnit {
+		cc.UsageUnit = ""
+	} else {
+		// when usage units are the same, then we can sum the usages
+		cc.UsageQuantity += other.UsageQuantity
+	}
+
+	if cc.Domain != other.Domain {
+		cc.Domain = ""
+	}
+
+	if cc.Aggregate != other.Aggregate {
+		cc.Aggregate = ""
+	}
+
 }
 
 type CustomCostSet struct {
@@ -121,8 +168,9 @@ func (ccs *CustomCostSet) Add(customCosts []*CustomCost) {
 }
 
 func (ccs *CustomCostSet) Aggregate(aggregateBy []string) error {
+	// when no aggregation, return the original CustomCostSet
 	if len(aggregateBy) == 0 {
-		return fmt.Errorf("found empty aggregateBy")
+		return nil
 	}
 
 	aggMap := make(map[string]*CustomCost)