parse.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package autocomplete
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/filter"
  6. "github.com/opencost/opencost/core/pkg/filter/ast"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/util/httputil"
  9. )
  10. // FilterParser parses a filter query string for autocomplete requests.
  11. type FilterParser func(filterString string) (filter.Filter, error)
  12. // ParseOptions configures ParseRequest.
  13. type ParseOptions struct {
  14. DefaultWindow string
  15. DefaultTenantID string
  16. LabelConfig *opencost.LabelConfig
  17. UTCOffset *time.Duration
  18. WindowValidator WindowValidator
  19. }
  20. // ParseRequest builds a Request from query parameters.
  21. func ParseRequest(qp httputil.QueryParams, opts ParseOptions, validateField FieldValidator, parseFilter FilterParser) (*Request, error) {
  22. windowStr := qp.Get("window", opts.DefaultWindow)
  23. if windowStr == "" {
  24. return nil, fmt.Errorf("%w: missing required 'window' parameter", ErrBadRequest)
  25. }
  26. var window opencost.Window
  27. var err error
  28. if opts.UTCOffset != nil {
  29. window, err = opencost.ParseWindowWithOffset(windowStr, *opts.UTCOffset)
  30. } else {
  31. window, err = opencost.ParseWindowUTC(windowStr)
  32. }
  33. if err != nil {
  34. return nil, fmt.Errorf("%w: invalid window parameter: %w", ErrBadRequest, err)
  35. }
  36. windowValidator := opts.WindowValidator
  37. if windowValidator == nil {
  38. windowValidator = DefaultWindowValidator
  39. }
  40. if err := windowValidator(window); err != nil {
  41. return nil, err
  42. }
  43. field, err := validateField(qp.Get("field", ""))
  44. if err != nil {
  45. return nil, fmt.Errorf("%w: invalid field: %w", ErrBadRequest, err)
  46. }
  47. filterString := qp.Get("filter", "")
  48. var parsedFilter filter.Filter = &ast.VoidOp{}
  49. if filterString != "" {
  50. if parseFilter == nil {
  51. return nil, fmt.Errorf("%w: invalid 'filter' parameter: filter parser is required", ErrBadRequest)
  52. }
  53. parsedFilter, err = parseFilter(filterString)
  54. if err != nil {
  55. return nil, fmt.Errorf("%w: invalid 'filter' parameter: %w", ErrBadRequest, err)
  56. }
  57. if parsedFilter == nil {
  58. parsedFilter = &ast.VoidOp{}
  59. }
  60. }
  61. tenantID := qp.Get("tenantId", opts.DefaultTenantID)
  62. if tenantID == "" {
  63. tenantID = opts.DefaultTenantID
  64. }
  65. labelConfig := opts.LabelConfig
  66. if labelConfig == nil {
  67. labelConfig = opencost.NewLabelConfig()
  68. }
  69. return &Request{
  70. TenantID: tenantID,
  71. Search: SanitizeSearch(qp.Get("search", "")),
  72. Field: field,
  73. Limit: qp.GetInt("limit", 0),
  74. Window: window,
  75. Filter: parsedFilter,
  76. LabelConfig: labelConfig,
  77. }, nil
  78. }