Matt Bolt 1 рік тому
батько
коміт
031c64a539
2 змінених файлів з 9 додано та 57 видалено
  1. 7 55
      pkg/costmodel/aggregation.go
  2. 2 2
      pkg/costmodel/handlers.go

+ 7 - 55
pkg/costmodel/aggregation.go

@@ -9,7 +9,6 @@ import (
 
 	"github.com/opencost/opencost/core/pkg/opencost"
 	"github.com/opencost/opencost/core/pkg/util/httputil"
-	"github.com/opencost/opencost/core/pkg/util/json"
 	"github.com/opencost/opencost/pkg/env"
 )
 
@@ -102,7 +101,7 @@ func (a *Accesses) ComputeAllocationHandlerSummary(w http.ResponseWriter, r *htt
 
 		as, err := a.Model.ComputeAllocation(*stepWindow.Start(), *stepWindow.End(), resolution)
 		if err != nil {
-			WriteError(w, InternalServerError(err.Error()))
+			proto.WriteError(w, proto.InternalServerError(err.Error()))
 			return
 		}
 		asr.Append(as)
@@ -114,7 +113,7 @@ func (a *Accesses) ComputeAllocationHandlerSummary(w http.ResponseWriter, r *htt
 	if len(aggregateBy) > 0 {
 		err = asr.AggregateBy(aggregateBy, nil)
 		if err != nil {
-			WriteError(w, InternalServerError(err.Error()))
+			proto.WriteError(w, proto.InternalServerError(err.Error()))
 			return
 		}
 	}
@@ -123,7 +122,7 @@ func (a *Accesses) ComputeAllocationHandlerSummary(w http.ResponseWriter, r *htt
 	if accumulate {
 		asr, err = asr.Accumulate(opencost.AccumulateOptionAll)
 		if err != nil {
-			WriteError(w, InternalServerError(err.Error()))
+			proto.WriteError(w, proto.InternalServerError(err.Error()))
 			return
 		}
 	}
@@ -135,7 +134,7 @@ func (a *Accesses) ComputeAllocationHandlerSummary(w http.ResponseWriter, r *htt
 	}
 	sasr := opencost.NewSummaryAllocationSetRange(sasl...)
 
-	w.Write(WrapData(sasr, nil))
+	WriteData(w, sasr, nil)
 }
 
 // ComputeAllocationHandler computes an AllocationSetRange from the CostModel.
@@ -203,60 +202,13 @@ func (a *Accesses) ComputeAllocationHandler(w http.ResponseWriter, r *http.Reque
 	asr, err := a.Model.QueryAllocation(window, resolution, step, aggregateBy, includeIdle, idleByNode, includeProportionalAssetResourceCosts, includeAggregatedMetadata, sharedLoadBalancer, accumulateBy, shareIdle)
 	if err != nil {
 		if strings.Contains(strings.ToLower(err.Error()), "bad request") {
-			WriteError(w, BadRequest(err.Error()))
+			proto.WriteError(w, proto.BadRequest(err.Error()))
 		} else {
-			WriteError(w, InternalServerError(err.Error()))
+			proto.WriteError(w, proto.InternalServerError(err.Error()))
 		}
 
 		return
 	}
 
-	w.Write(WrapData(asr, nil))
-}
-
-// The below was transferred from a different package in order to maintain
-// previous behavior. Ultimately, we should clean this up at some point.
-// TODO move to util and/or standardize everything
-
-type Error struct {
-	StatusCode int
-	Body       string
-}
-
-func WriteError(w http.ResponseWriter, err Error) {
-	status := err.StatusCode
-	if status == 0 {
-		status = http.StatusInternalServerError
-	}
-	w.WriteHeader(status)
-
-	resp, _ := json.Marshal(&Response{
-		Code:    status,
-		Message: fmt.Sprintf("Error: %s", err.Body),
-	})
-	w.Write(resp)
-}
-
-func BadRequest(message string) Error {
-	return Error{
-		StatusCode: http.StatusBadRequest,
-		Body:       message,
-	}
-}
-
-func InternalServerError(message string) Error {
-	if message == "" {
-		message = "Internal Server Error"
-	}
-	return Error{
-		StatusCode: http.StatusInternalServerError,
-		Body:       message,
-	}
-}
-
-func NotFound() Error {
-	return Error{
-		StatusCode: http.StatusNotFound,
-		Body:       "Not Found",
-	}
+	WriteData(w, asr, nil)
 }

+ 2 - 2
pkg/costmodel/handlers.go

@@ -36,7 +36,7 @@ func (a *Accesses) ComputeAssetsHandler(w http.ResponseWriter, r *http.Request,
 		return
 	}
 
-	w.Write(WrapData(assetSet, nil))
+	WriteData(w, assetSet, nil)
 }
 
 // ComputeAllocationHandler returns the assets from the CostModel.
@@ -67,7 +67,7 @@ func (a *Accesses) ComputeAssetsCarbonHandler(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	w.Write(WrapData(carbonEstimates, nil))
+	WriteData(w, carbonEstimates, nil)
 }
 
 func (a *Accesses) ComputeAssetsFromCostmodel(window opencost.Window, filterString string) (*opencost.AssetSet, error) {