querier.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package cloudcost
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/autocomplete"
  8. "github.com/opencost/opencost/core/pkg/filter"
  9. "github.com/opencost/opencost/core/pkg/opencost"
  10. )
  11. // Querier allows for querying ranges of CloudCost data
  12. type Querier interface {
  13. Query(context.Context, QueryRequest) (*opencost.CloudCostSetRange, error)
  14. QueryCloudCostAutocomplete(context.Context, autocomplete.Request) (*autocomplete.Response, error)
  15. }
  16. type QueryRequest struct {
  17. Start time.Time
  18. End time.Time
  19. AggregateBy []string
  20. Accumulate opencost.AccumulateOption
  21. Filter filter.Filter
  22. }
  23. // DefaultChartItemsLength the default max number of items for a ViewGraphDataSet
  24. const DefaultChartItemsLength int = 10
  25. // ViewQuerier defines a contract for return View types to the QueryService to service the View Api
  26. type ViewQuerier interface {
  27. QueryViewGraph(context.Context, ViewQueryRequest) (ViewGraphData, error)
  28. QueryViewTotals(context.Context, ViewQueryRequest) (*ViewTotals, error)
  29. QueryViewTable(context.Context, ViewQueryRequest) (ViewTableRows, error)
  30. }
  31. type ViewQueryRequest struct {
  32. QueryRequest
  33. CostMetricName opencost.CostMetricName
  34. ChartItemsLength int
  35. Offset int
  36. Limit int
  37. SortDirection SortDirection
  38. SortColumn SortField
  39. SkipCount bool
  40. }
  41. // SortDirection a string type that acts as an enumeration of possible request options
  42. type SortDirection string
  43. const (
  44. SortDirectionNone SortDirection = ""
  45. SortDirectionAscending SortDirection = "asc"
  46. SortDirectionDescending SortDirection = "desc"
  47. )
  48. // ParseSortDirection provides a resilient way to parse one of the enumerated SortDirection types from a string
  49. // or throws an error if it is not able to.
  50. func ParseSortDirection(sortDirection string) (SortDirection, error) {
  51. switch strings.ToLower(sortDirection) {
  52. case strings.ToLower(string(SortDirectionAscending)):
  53. return SortDirectionAscending, nil
  54. case strings.ToLower(string(SortDirectionDescending)):
  55. return SortDirectionDescending, nil
  56. }
  57. return SortDirectionNone, fmt.Errorf("failed to parse a valid CostMetricName from '%s'", sortDirection)
  58. }
  59. // SortField a string type that acts as an enumeration of possible request options
  60. type SortField string
  61. const (
  62. SortFieldNone SortField = ""
  63. SortFieldName SortField = "name"
  64. SortFieldCost SortField = "cost"
  65. SortFieldKubernetesPercent SortField = "kubernetesPercent"
  66. )
  67. // ParseSortField provides a resilient way to parse one of the enumerated SortField types from a string
  68. // or throws an error if it is not able to.
  69. func ParseSortField(sortColumn string) (SortField, error) {
  70. switch strings.ToLower(sortColumn) {
  71. case strings.ToLower(string(SortFieldName)):
  72. return SortFieldName, nil
  73. case strings.ToLower(string(SortFieldCost)):
  74. return SortFieldCost, nil
  75. case strings.ToLower(string(SortFieldKubernetesPercent)):
  76. return SortFieldKubernetesPercent, nil
  77. }
  78. return SortFieldNone, fmt.Errorf("failed to parse a valid CostMetricName from '%s'", sortColumn)
  79. }