querier.go 2.9 KB

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