querier.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  40. // SortDirection a string type that acts as an enumeration of possible request options
  41. type SortDirection string
  42. const (
  43. SortDirectionNone SortDirection = ""
  44. SortDirectionAscending SortDirection = "asc"
  45. SortDirectionDescending SortDirection = "desc"
  46. )
  47. // ParseSortDirection provides a resilient way to parse one of the enumerated SortDirection types from a string
  48. // or throws an error if it is not able to.
  49. func ParseSortDirection(sortDirection string) (SortDirection, error) {
  50. switch strings.ToLower(sortDirection) {
  51. case strings.ToLower(string(SortDirectionAscending)):
  52. return SortDirectionAscending, nil
  53. case strings.ToLower(string(SortDirectionDescending)):
  54. return SortDirectionDescending, nil
  55. }
  56. return SortDirectionNone, fmt.Errorf("failed to parse a valid CostMetricName from '%s'", sortDirection)
  57. }
  58. // SortField a string type that acts as an enumeration of possible request options
  59. type SortField string
  60. const (
  61. SortFieldNone SortField = ""
  62. SortFieldName SortField = "name"
  63. SortFieldCost SortField = "cost"
  64. SortFieldKubernetesPercent SortField = "kubernetesPercent"
  65. )
  66. // ParseSortField provides a resilient way to parse one of the enumerated SortField types from a string
  67. // or throws an error if it is not able to.
  68. func ParseSortField(sortColumn string) (SortField, error) {
  69. switch strings.ToLower(sortColumn) {
  70. case strings.ToLower(string(SortFieldName)):
  71. return SortFieldName, nil
  72. case strings.ToLower(string(SortFieldCost)):
  73. return SortFieldCost, nil
  74. case strings.ToLower(string(SortFieldKubernetesPercent)):
  75. return SortFieldKubernetesPercent, nil
  76. }
  77. return SortFieldNone, fmt.Errorf("failed to parse a valid CostMetricName from '%s'", sortColumn)
  78. }