querier.go 3.3 KB

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