handlers.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package costmodel
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/julienschmidt/httprouter"
  6. "github.com/opencost/opencost/pkg/env"
  7. "github.com/opencost/opencost/pkg/kubecost"
  8. "github.com/opencost/opencost/pkg/util/httputil"
  9. )
  10. // ComputeAllocationHandler returns the assets from the CostModel.
  11. func (a *Accesses) ComputeAssetsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  12. w.Header().Set("Content-Type", "application/json")
  13. qp := httputil.NewQueryParams(r.URL.Query())
  14. // Window is a required field describing the window of time over which to
  15. // compute allocation data.
  16. window, err := kubecost.ParseWindowWithOffset(qp.Get("window", ""), env.GetParsedUTCOffset())
  17. if err != nil {
  18. http.Error(w, fmt.Sprintf("Invalid 'window' parameter: %s", err), http.StatusBadRequest)
  19. return
  20. }
  21. assetSet, err := a.Model.ComputeAssets(*window.Start(), *window.End())
  22. if err != nil {
  23. http.Error(w, fmt.Sprintf("Error computing asset set: %s", err), http.StatusInternalServerError)
  24. return
  25. }
  26. w.Write(WrapData(assetSet, nil))
  27. }