Explorar o código

export functions for refactor

AjayTripathy %!s(int64=6) %!d(string=hai) anos
pai
achega
9ef86a1706
Modificáronse 2 ficheiros con 60 adicións e 61 borrados
  1. 1 1
      costmodel/aggregations.go
  2. 59 60
      costmodel/router.go

+ 1 - 1
costmodel/aggregations.go

@@ -116,7 +116,7 @@ func ComputeIdleCoefficient(costData map[string]*CostData, cli prometheusClient.
 		return nil, err
 	}
 
-	resolutionDuration, err := parseDuration(resolution)
+	resolutionDuration, err := ParseDuration(resolution)
 	resolutionCoefficient := resolutionDuration.Hours()
 
 	allTotals, err := ClusterCostsForAllClusters(cli, cp, windowString, offset)

+ 59 - 60
costmodel/router.go

@@ -74,8 +74,8 @@ type DataEnvelope struct {
 	Message string      `json:"message,omitempty"`
 }
 
-// filterCostData allows through only CostData that matches the given filters for namespace and clusterId
-func filterCostData(data map[string]*CostData, namespace, clusterId string) map[string]*CostData {
+// FilterCostData allows through only CostData that matches the given filters for namespace and clusterId
+func FilterCostData(data map[string]*CostData, namespace, clusterId string) map[string]*CostData {
 	result := make(map[string]*CostData)
 	for key, datum := range data {
 		if costDataPassesFilters(datum, namespace, clusterId) {
@@ -129,7 +129,7 @@ func normalizeTimeParam(param string) (string, error) {
 }
 
 // parseDuration converts a Prometheus-style duration string into a Duration
-func parseDuration(duration string) (*time.Duration, error) {
+func ParseDuration(duration string) (*time.Duration, error) {
 	unitStr := duration[len(duration)-1:]
 	var unit time.Duration
 	switch unitStr {
@@ -155,9 +155,9 @@ func parseDuration(duration string) (*time.Duration, error) {
 	return &dur, nil
 }
 
-// parseTimeRange returns a start and end time, respectively, which are converted from
+// ParseTimeRange returns a start and end time, respectively, which are converted from
 // a duration and offset, defined as strings with Prometheus-style syntax.
-func parseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
+func ParseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
 	// endTime defaults to the current time, unless an offset is explicity declared,
 	// in which case it shifts endTime back by given duration
 	endTime := time.Now()
@@ -187,7 +187,7 @@ func parseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
 	return &startTime, &endTime, nil
 }
 
-func wrapDataWithMessage(data interface{}, err error, message string) []byte {
+func WrapDataWithMessage(data interface{}, err error, message string) []byte {
 	var resp []byte
 
 	if err != nil {
@@ -211,7 +211,7 @@ func wrapDataWithMessage(data interface{}, err error, message string) []byte {
 	return resp
 }
 
-func wrapData(data interface{}, err error) []byte {
+func WrapData(data interface{}, err error) []byte {
 	var resp []byte
 
 	if err != nil {
@@ -241,7 +241,7 @@ func (a *Accesses) RefreshPricingData(w http.ResponseWriter, r *http.Request, ps
 
 	err := a.Cloud.DownloadPricingData()
 
-	w.Write(wrapData(nil, err))
+	w.Write(WrapData(nil, err))
 }
 
 func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -263,20 +263,20 @@ func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps http
 	if aggregationField != "" {
 		c, err := a.Cloud.GetConfig()
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 
 		discount, err := strconv.ParseFloat(c.Discount[:len(c.Discount)-1], 64)
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 		discount = discount * 0.01
 
 		dur, err := time.ParseDuration(window)
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 		// dataCount is the number of time series data expected for the given interval,
@@ -290,13 +290,13 @@ func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps http
 			IdleCoefficients: make(map[string]float64),
 		}
 		agg := AggregateCostData(data, aggregationField, subfields, a.Cloud, opts)
-		w.Write(wrapData(agg, nil))
+		w.Write(WrapData(agg, nil))
 	} else {
 		if fields != "" {
 			filteredData := filterFields(fields, data)
-			w.Write(wrapData(filteredData, err))
+			w.Write(WrapData(filteredData, err))
 		} else {
-			w.Write(wrapData(data, err))
+			w.Write(WrapData(data, err))
 		}
 	}
 }
@@ -309,7 +309,7 @@ func (a *Accesses) ClusterCosts(w http.ResponseWriter, r *http.Request, ps httpr
 	offset := r.URL.Query().Get("offset")
 
 	data, err := ClusterCosts(a.PrometheusClient, a.Cloud, window, offset)
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 func (a *Accesses) ClusterCostsOverTime(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -322,7 +322,7 @@ func (a *Accesses) ClusterCostsOverTime(w http.ResponseWriter, r *http.Request,
 	offset := r.URL.Query().Get("offset")
 
 	data, err := ClusterCostsOverTime(a.PrometheusClient, a.Cloud, start, end, window, offset)
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 // AggregateCostModel handles HTTP requests to the aggregated cost model API, which can be parametrized
@@ -368,21 +368,21 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 	// aggregation field is required
 	if field == "" {
 		w.WriteHeader(http.StatusBadRequest)
-		w.Write(wrapData(nil, fmt.Errorf("Missing aggregation field parameter")))
+		w.Write(WrapData(nil, fmt.Errorf("Missing aggregation field parameter")))
 		return
 	}
 
 	// aggregation subfield is required when aggregation field is "label"
 	if field == "label" && len(subfields) == 0 {
 		w.WriteHeader(http.StatusBadRequest)
-		w.Write(wrapData(nil, fmt.Errorf("Missing aggregation subfield parameter for aggregation by label")))
+		w.Write(WrapData(nil, fmt.Errorf("Missing aggregation subfield parameter for aggregation by label")))
 		return
 	}
 
 	// enforce one of four available rate options
 	if rate != "" && rate != "hourly" && rate != "daily" && rate != "monthly" {
 		w.WriteHeader(http.StatusBadRequest)
-		w.Write(wrapData(nil, fmt.Errorf("If set, rate parameter must be one of: 'hourly', 'daily', 'monthly'")))
+		w.Write(WrapData(nil, fmt.Errorf("If set, rate parameter must be one of: 'hourly', 'daily', 'monthly'")))
 		return
 	}
 
@@ -400,7 +400,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 
 	// check the cache for aggregated response; if cache is hit and not disabled, return response
 	if result, found := a.AggregateCache.Get(aggKey); found && !disableCache {
-		w.Write(wrapDataWithMessage(result, nil, fmt.Sprintf("aggregate cache hit: %s", aggKey)))
+		w.Write(WrapDataWithMessage(result, nil, fmt.Sprintf("aggregate cache hit: %s", aggKey)))
 		return
 	}
 
@@ -417,10 +417,10 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 	}
 
 	// convert duration and offset to start and end times
-	startTime, endTime, err := parseTimeRange(duration, offset)
+	startTime, endTime, err := ParseTimeRange(duration, offset)
 	if err != nil {
 		w.WriteHeader(http.StatusBadRequest)
-		w.Write(wrapData(nil, fmt.Errorf("Error parsing duration (%s) and offset (%s)", duration, offset)))
+		w.Write(WrapData(nil, fmt.Errorf("Error parsing duration (%s) and offset (%s)", duration, offset)))
 		return
 	}
 	durationHours := endTime.Sub(*startTime).Hours()
@@ -446,11 +446,11 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 		// 2 days
 		resolution = "2h"
 	}
-	resolutionDuration, err := parseDuration(resolution)
+	resolutionDuration, err := ParseDuration(resolution)
 	resolutionHours := resolutionDuration.Hours()
 	if err != nil {
 		w.WriteHeader(http.StatusBadRequest)
-		w.Write(wrapData(nil, fmt.Errorf("Error parsing resolution (%s)", resolution)))
+		w.Write(WrapData(nil, fmt.Errorf("Error parsing resolution (%s)", resolution)))
 		return
 	}
 
@@ -474,7 +474,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 		end := endTime.Format(RFC3339Milli)
 		costData, err = a.Model.ComputeCostDataRange(pClient, a.KubeClientSet, a.Cloud, start, end, resolution, "", "", remoteEnabled)
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 
@@ -483,12 +483,12 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 
 	c, err := a.Cloud.GetConfig()
 	if err != nil {
-		w.Write(wrapData(nil, err))
+		w.Write(WrapData(nil, err))
 		return
 	}
 	discount, err := strconv.ParseFloat(c.Discount[:len(c.Discount)-1], 64)
 	if err != nil {
-		w.Write(wrapData(nil, err))
+		w.Write(WrapData(nil, err))
 		return
 	}
 	discount = discount * 0.01
@@ -504,7 +504,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 		idleCoefficients, err = ComputeIdleCoefficient(costData, pClient, a.Cloud, discount, windowStr, offset, resolution)
 		if err != nil {
 			klog.Errorf("error computing idle coefficient: windowString=%s, offset=%s, err=%s", windowStr, offset, err)
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 	}
@@ -519,7 +519,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 		sln = strings.Split(sharedLabelNames, ",")
 		slv = strings.Split(sharedLabelValues, ",")
 		if len(sln) != len(slv) || slv[0] == "" {
-			w.Write(wrapData(nil, fmt.Errorf("Supply exacly one label value per label name")))
+			w.Write(WrapData(nil, fmt.Errorf("Supply exacly one label value per label name")))
 			return
 		}
 	}
@@ -533,7 +533,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 	}
 
 	// filter cost data by namespace and cluster after caching for maximal cache hits
-	costData = filterCostData(costData, namespace, cluster)
+	costData = FilterCostData(costData, namespace, cluster)
 
 	dataCount := int(durationHours / resolutionHours)
 
@@ -551,7 +551,7 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 	result := AggregateCostData(costData, field, subfields, a.Cloud, opts)
 	a.AggregateCache.Set(aggKey, result, cache.DefaultExpiration)
 
-	w.Write(wrapDataWithMessage(result, nil, fmt.Sprintf("aggregate cache miss: %s", aggKey)))
+	w.Write(WrapDataWithMessage(result, nil, fmt.Sprintf("aggregate cache miss: %s", aggKey)))
 }
 
 func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -584,18 +584,18 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 
 	data, err := a.Model.ComputeCostDataRange(pClient, a.KubeClientSet, a.Cloud, start, end, window, namespace, cluster, remoteEnabled)
 	if err != nil {
-		w.Write(wrapData(nil, err))
+		w.Write(WrapData(nil, err))
 	}
 	if aggregationField != "" {
 		c, err := a.Cloud.GetConfig()
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 			return
 		}
 
 		discount, err := strconv.ParseFloat(c.Discount[:len(c.Discount)-1], 64)
 		if err != nil {
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 		}
 		discount = discount * 0.01
 
@@ -604,13 +604,13 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 			IdleCoefficients: make(map[string]float64),
 		}
 		agg := AggregateCostData(data, aggregationField, subfields, a.Cloud, opts)
-		w.Write(wrapData(agg, nil))
+		w.Write(WrapData(agg, nil))
 	} else {
 		if fields != "" {
 			filteredData := filterFields(fields, data)
-			w.Write(wrapData(filteredData, err))
+			w.Write(WrapData(filteredData, err))
 		} else {
-			w.Write(wrapData(data, err))
+			w.Write(WrapData(data, err))
 		}
 	}
 }
@@ -635,12 +635,12 @@ func (a *Accesses) CostDataModelRangeLarge(w http.ResponseWriter, r *http.Reques
 		start, err = time.Parse(RFC3339Milli, startString)
 		if err != nil {
 			klog.V(1).Infof("Error parsing time " + startString + ". Error: " + err.Error())
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 		}
 	} else {
 		window, err := time.ParseDuration(windowString)
 		if err != nil {
-			w.Write(wrapData(nil, fmt.Errorf("Invalid duration '%s'", windowString)))
+			w.Write(WrapData(nil, fmt.Errorf("Invalid duration '%s'", windowString)))
 
 		}
 		start = time.Now().Add(-2 * window)
@@ -649,7 +649,7 @@ func (a *Accesses) CostDataModelRangeLarge(w http.ResponseWriter, r *http.Reques
 		end, err = time.Parse(RFC3339Milli, endString)
 		if err != nil {
 			klog.V(1).Infof("Error parsing time " + endString + ". Error: " + err.Error())
-			w.Write(wrapData(nil, err))
+			w.Write(WrapData(nil, err))
 		}
 	} else {
 		end = time.Now()
@@ -661,7 +661,7 @@ func (a *Accesses) CostDataModelRangeLarge(w http.ResponseWriter, r *http.Reques
 	klog.V(1).Infof("Using remote database for query from %s to %s with window %s", startString, endString, windowString)
 
 	data, err := CostDataRangeFromSQL("", "", windowString, remoteStartStr, remoteEndStr)
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 func (a *Accesses) OutofClusterCosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -679,7 +679,7 @@ func (a *Accesses) OutofClusterCosts(w http.ResponseWriter, r *http.Request, ps
 	} else {
 		data, err = a.Cloud.ExternalAllocations(start, end, "kubernetes_"+aggregator)
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 func (a *Accesses) OutOfClusterCostsWithCache(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -716,7 +716,7 @@ func (a *Accesses) OutOfClusterCostsWithCache(w http.ResponseWriter, r *http.Req
 	key := fmt.Sprintf(`%s:%s:%s`, start, end, aggregation)
 	if value, found := a.OutOfClusterCache.Get(key); found && !disableCache {
 		if data, ok := value.([]*costAnalyzerCloud.OutOfClusterAllocation); ok {
-			w.Write(wrapDataWithMessage(data, nil, fmt.Sprintf("out of cluser cache hit: %s", key)))
+			w.Write(WrapDataWithMessage(data, nil, fmt.Sprintf("out of cluser cache hit: %s", key)))
 			return
 		}
 		klog.Errorf("caching error: failed to type cast data: %s", key)
@@ -727,7 +727,7 @@ func (a *Accesses) OutOfClusterCostsWithCache(w http.ResponseWriter, r *http.Req
 		a.OutOfClusterCache.Set(key, data, cache.DefaultExpiration)
 	}
 
-	w.Write(wrapDataWithMessage(data, err, fmt.Sprintf("out of cluser cache miss: %s", key)))
+	w.Write(WrapDataWithMessage(data, err, fmt.Sprintf("out of cluser cache miss: %s", key)))
 }
 
 func (p *Accesses) GetAllNodePricing(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -735,14 +735,14 @@ func (p *Accesses) GetAllNodePricing(w http.ResponseWriter, r *http.Request, ps
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 
 	data, err := p.Cloud.AllNodePricing()
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 func (p *Accesses) GetConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	data, err := p.Cloud.GetConfig()
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 }
 
 func (p *Accesses) UpdateSpotInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -750,10 +750,10 @@ func (p *Accesses) UpdateSpotInfoConfigs(w http.ResponseWriter, r *http.Request,
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.SpotInfoUpdateType)
 	if err != nil {
-		w.Write(wrapData(data, err))
+		w.Write(WrapData(data, err))
 		return
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 	err = p.Cloud.DownloadPricingData()
 	if err != nil {
 		klog.V(1).Infof("Error redownloading data on config update: %s", err.Error())
@@ -766,10 +766,10 @@ func (p *Accesses) UpdateAthenaInfoConfigs(w http.ResponseWriter, r *http.Reques
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.AthenaInfoUpdateType)
 	if err != nil {
-		w.Write(wrapData(data, err))
+		w.Write(WrapData(data, err))
 		return
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 	return
 }
 
@@ -778,10 +778,10 @@ func (p *Accesses) UpdateBigQueryInfoConfigs(w http.ResponseWriter, r *http.Requ
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.BigqueryUpdateType)
 	if err != nil {
-		w.Write(wrapData(data, err))
+		w.Write(WrapData(data, err))
 		return
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 	return
 }
 
@@ -790,10 +790,10 @@ func (p *Accesses) UpdateConfigByKey(w http.ResponseWriter, r *http.Request, ps
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	data, err := p.Cloud.UpdateConfig(r.Body, "")
 	if err != nil {
-		w.Write(wrapData(data, err))
+		w.Write(WrapData(data, err))
 		return
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 	return
 }
 
@@ -803,10 +803,10 @@ func (p *Accesses) ManagementPlatform(w http.ResponseWriter, r *http.Request, ps
 
 	data, err := p.Cloud.GetManagementPlatform()
 	if err != nil {
-		w.Write(wrapData(data, err))
+		w.Write(WrapData(data, err))
 		return
 	}
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 	return
 }
 
@@ -815,7 +815,7 @@ func (p *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httpro
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 
 	data, err := p.Cloud.ClusterInfo()
-	w.Write(wrapData(data, err))
+	w.Write(WrapData(data, err))
 
 }
 
@@ -828,14 +828,14 @@ func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 func (p *Accesses) GetPrometheusMetadata(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
-	w.Write(wrapData(ValidatePrometheus(p.PrometheusClient, false)))
+	w.Write(WrapData(ValidatePrometheus(p.PrometheusClient, false)))
 }
 
 func (p *Accesses) ContainerUptimes(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 	res, err := ComputeUptimes(p.PrometheusClient)
-	w.Write(wrapData(res, err))
+	w.Write(WrapData(res, err))
 }
 
 func (a *Accesses) recordPrices() {
@@ -1241,5 +1241,4 @@ func init() {
 	Router.GET("/managementPlatform", A.ManagementPlatform)
 	Router.GET("/clusterInfo", A.ClusterInfo)
 	Router.GET("/containerUptimes", A.ContainerUptimes)
-	Router.GET("/aggregatedCostModel", A.AggregateCostModel)
 }