|
@@ -6,9 +6,9 @@ import (
|
|
|
"net/http"
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/julienschmidt/httprouter"
|
|
|
- "github.com/opencost/opencost/core/pkg/filter"
|
|
|
|
|
- allocationfilter "github.com/opencost/opencost/core/pkg/filter/allocation"
|
|
|
|
|
- assetfilter "github.com/opencost/opencost/core/pkg/filter/asset"
|
|
|
|
|
|
|
+ "github.com/opencost/opencost/core/pkg/autocomplete"
|
|
|
|
|
+ coreallocation "github.com/opencost/opencost/core/pkg/autocomplete/allocation"
|
|
|
|
|
+ coreasset "github.com/opencost/opencost/core/pkg/autocomplete/asset"
|
|
|
"github.com/opencost/opencost/core/pkg/opencost"
|
|
"github.com/opencost/opencost/core/pkg/opencost"
|
|
|
"github.com/opencost/opencost/core/pkg/util/httputil"
|
|
"github.com/opencost/opencost/core/pkg/util/httputil"
|
|
|
"github.com/opencost/opencost/pkg/allocation"
|
|
"github.com/opencost/opencost/pkg/allocation"
|
|
@@ -20,34 +20,21 @@ func (a *Accesses) ComputeAllocationAutocompleteHandler(w http.ResponseWriter, r
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
qp := httputil.NewQueryParams(r.URL.Query())
|
|
qp := httputil.NewQueryParams(r.URL.Query())
|
|
|
|
|
|
|
|
- window, err := opencost.ParseWindowWithOffset(qp.Get("window", ""), env.GetParsedUTCOffset())
|
|
|
|
|
|
|
+ offset := env.GetParsedUTCOffset()
|
|
|
|
|
+ req, err := coreallocation.ParseRequest(qp, autocomplete.ParseOptions{
|
|
|
|
|
+ LabelConfig: opencost.NewLabelConfig(),
|
|
|
|
|
+ UTCOffset: &offset,
|
|
|
|
|
+ })
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- http.Error(w, fmt.Sprintf("Invalid 'window' parameter: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
+ http.Error(w, fmt.Sprintf("Invalid allocation autocomplete request: %s", err), http.StatusBadRequest)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var parsedFilter filter.Filter
|
|
|
|
|
filterString := qp.Get("filter", "")
|
|
filterString := qp.Get("filter", "")
|
|
|
- if filterString != "" {
|
|
|
|
|
- parser := allocationfilter.NewAllocationFilterParser()
|
|
|
|
|
- parsedFilter, err = parser.Parse(filterString)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- http.Error(w, fmt.Sprintf("Invalid 'filter' parameter: %s", err), http.StatusBadRequest)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- resp, err := a.QueryAllocationAutocomplete(allocation.AllocationAutocompleteRequest{
|
|
|
|
|
- Search: qp.Get("search", ""),
|
|
|
|
|
- Field: qp.Get("field", ""),
|
|
|
|
|
- Limit: qp.GetInt("limit", 0),
|
|
|
|
|
- Window: window,
|
|
|
|
|
- Filter: parsedFilter,
|
|
|
|
|
- LabelConfig: opencost.NewLabelConfig(),
|
|
|
|
|
- }, filterString, r.Context())
|
|
|
|
|
|
|
+ resp, err := a.QueryAllocationAutocomplete(*req, filterString, r.Context())
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
status := http.StatusInternalServerError
|
|
status := http.StatusInternalServerError
|
|
|
- if allocation.IsAutocompleteBadRequest(err) {
|
|
|
|
|
|
|
+ if autocomplete.IsBadRequest(err) {
|
|
|
status = http.StatusBadRequest
|
|
status = http.StatusBadRequest
|
|
|
}
|
|
}
|
|
|
http.Error(w, fmt.Sprintf("Error getting allocation autocomplete: %s", err), status)
|
|
http.Error(w, fmt.Sprintf("Error getting allocation autocomplete: %s", err), status)
|
|
@@ -57,7 +44,7 @@ func (a *Accesses) ComputeAllocationAutocompleteHandler(w http.ResponseWriter, r
|
|
|
WriteData(w, resp, nil)
|
|
WriteData(w, resp, nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (a *Accesses) QueryAllocationAutocomplete(req allocation.AllocationAutocompleteRequest, filterString string, ctx context.Context) (*allocation.AllocationAutocompleteResponse, error) {
|
|
|
|
|
|
|
+func (a *Accesses) QueryAllocationAutocomplete(req autocomplete.Request, filterString string, ctx context.Context) (*autocomplete.Response, error) {
|
|
|
asr, err := a.Model.QueryAllocation(req.Window, req.Window.Duration(), nil, false, false, false, false, false, opencost.AccumulateOptionNone, false, filterString)
|
|
asr, err := a.Model.QueryAllocation(req.Window, req.Window.Duration(), nil, false, false, false, false, false, opencost.AccumulateOptionNone, false, filterString)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("error querying allocations: %w", err)
|
|
return nil, fmt.Errorf("error querying allocations: %w", err)
|
|
@@ -69,38 +56,20 @@ func (a *Accesses) ComputeAssetsAutocompleteHandler(w http.ResponseWriter, r *ht
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
qp := httputil.NewQueryParams(r.URL.Query())
|
|
qp := httputil.NewQueryParams(r.URL.Query())
|
|
|
|
|
|
|
|
- window, err := opencost.ParseWindowWithOffset(qp.Get("window", ""), env.GetParsedUTCOffset())
|
|
|
|
|
|
|
+ offset := env.GetParsedUTCOffset()
|
|
|
|
|
+ req, err := coreasset.ParseRequest(qp, autocomplete.ParseOptions{
|
|
|
|
|
+ DefaultTenantID: "opencost",
|
|
|
|
|
+ UTCOffset: &offset,
|
|
|
|
|
+ })
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- http.Error(w, fmt.Sprintf("Invalid 'window' parameter: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
+ http.Error(w, fmt.Sprintf("Invalid asset autocomplete request: %s", err), http.StatusBadRequest)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- if window.IsOpen() || window.Start() == nil || window.End() == nil {
|
|
|
|
|
- http.Error(w, fmt.Sprintf("Invalid 'window' parameter: %s", window.String()), http.StatusBadRequest)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var parsedFilter filter.Filter
|
|
|
|
|
- filterString := qp.Get("filter", "")
|
|
|
|
|
- if filterString != "" {
|
|
|
|
|
- parser := assetfilter.NewAssetFilterParser()
|
|
|
|
|
- parsedFilter, err = parser.Parse(filterString)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- http.Error(w, fmt.Sprintf("Invalid 'filter' parameter: %s", err), http.StatusBadRequest)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- resp, err := a.QueryAssetAutocomplete(asset.AssetAutocompleteRequest{
|
|
|
|
|
- TenantID: qp.Get("tenantId", "opencost"),
|
|
|
|
|
- Search: qp.Get("search", ""),
|
|
|
|
|
- Field: qp.Get("field", ""),
|
|
|
|
|
- Limit: qp.GetInt("limit", 0),
|
|
|
|
|
- Window: window,
|
|
|
|
|
- Filter: parsedFilter,
|
|
|
|
|
- }, r.Context())
|
|
|
|
|
|
|
+ resp, err := a.QueryAssetAutocomplete(*req, r.Context())
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
status := http.StatusInternalServerError
|
|
status := http.StatusInternalServerError
|
|
|
- if asset.IsAutocompleteBadRequest(err) {
|
|
|
|
|
|
|
+ if autocomplete.IsBadRequest(err) {
|
|
|
status = http.StatusBadRequest
|
|
status = http.StatusBadRequest
|
|
|
}
|
|
}
|
|
|
http.Error(w, fmt.Sprintf("Error getting asset autocomplete: %s", err), status)
|
|
http.Error(w, fmt.Sprintf("Error getting asset autocomplete: %s", err), status)
|
|
@@ -110,9 +79,9 @@ func (a *Accesses) ComputeAssetsAutocompleteHandler(w http.ResponseWriter, r *ht
|
|
|
WriteData(w, resp, nil)
|
|
WriteData(w, resp, nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (a *Accesses) QueryAssetAutocomplete(req asset.AssetAutocompleteRequest, ctx context.Context) (*asset.AssetAutocompleteResponse, error) {
|
|
|
|
|
|
|
+func (a *Accesses) QueryAssetAutocomplete(req autocomplete.Request, ctx context.Context) (*autocomplete.Response, error) {
|
|
|
if req.Window.IsOpen() || req.Window.Start() == nil || req.Window.End() == nil {
|
|
if req.Window.IsOpen() || req.Window.Start() == nil || req.Window.End() == nil {
|
|
|
- return nil, fmt.Errorf("%w: invalid window: %s", asset.ErrAutocompleteBadRequest, req.Window.String())
|
|
|
|
|
|
|
+ return nil, fmt.Errorf("%w: invalid window: %s", autocomplete.ErrBadRequest, req.Window.String())
|
|
|
}
|
|
}
|
|
|
assetSet, err := a.Model.ComputeAssets(*req.Window.Start(), *req.Window.End())
|
|
assetSet, err := a.Model.ComputeAssets(*req.Window.Start(), *req.Window.End())
|
|
|
if err != nil {
|
|
if err != nil {
|