query.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package kubecost
  2. import (
  3. "time"
  4. filter21 "github.com/opencost/opencost/pkg/filter21"
  5. )
  6. // Querier is an aggregate interface which has the ability to query each Kubecost store type
  7. type Querier interface {
  8. AllocationQuerier
  9. SummaryAllocationQuerier
  10. AssetQuerier
  11. CloudUsageQuerier
  12. }
  13. // AllocationQuerier interface defining api for requesting Allocation data
  14. type AllocationQuerier interface {
  15. QueryAllocation(start, end time.Time, opts *AllocationQueryOptions) (*AllocationSetRange, error)
  16. }
  17. // SummaryAllocationQuerier interface defining api for requesting SummaryAllocation data
  18. type SummaryAllocationQuerier interface {
  19. QuerySummaryAllocation(start, end time.Time, opts *AllocationQueryOptions) (*SummaryAllocationSetRange, error)
  20. }
  21. // AssetQuerier interface defining api for requesting Asset data
  22. type AssetQuerier interface {
  23. QueryAsset(start, end time.Time, opts *AssetQueryOptions) (*AssetSetRange, error)
  24. }
  25. // CloudUsageQuerier interface defining api for requesting CloudUsage data
  26. type CloudUsageQuerier interface {
  27. QueryCloudUsage(start, end time.Time, opts *CloudUsageQueryOptions) (*CloudUsageSetRange, error)
  28. }
  29. // AllocationQueryOptions defines optional parameters for querying an Allocation Store
  30. type AllocationQueryOptions struct {
  31. Accumulate AccumulateOption
  32. AggregateBy []string
  33. Compute bool
  34. DisableAggregatedStores bool
  35. Filter filter21.Filter
  36. IdleByNode bool
  37. IncludeExternal bool
  38. IncludeIdle bool
  39. LabelConfig *LabelConfig
  40. MergeUnallocated bool
  41. Reconcile bool
  42. ReconcileNetwork bool
  43. ShareFuncs []AllocationMatchFunc
  44. SharedHourlyCosts map[string]float64
  45. ShareIdle string
  46. ShareSplit string
  47. ShareTenancyCosts bool
  48. SplitIdle bool
  49. Step time.Duration
  50. }
  51. type AccumulateOption string
  52. const (
  53. AccumulateOptionNone AccumulateOption = ""
  54. AccumulateOptionAll AccumulateOption = "all"
  55. AccumulateOptionHour AccumulateOption = "hour"
  56. AccumulateOptionDay AccumulateOption = "day"
  57. AccumulateOptionWeek AccumulateOption = "week"
  58. AccumulateOptionMonth AccumulateOption = "month"
  59. AccumulateOptionQuarter AccumulateOption = "quarter"
  60. )
  61. // AssetQueryOptions defines optional parameters for querying an Asset Store
  62. type AssetQueryOptions struct {
  63. Accumulate bool
  64. AggregateBy []string
  65. Compute bool
  66. DisableAdjustments bool
  67. DisableAggregatedStores bool
  68. Filter filter21.Filter
  69. IncludeCloud bool
  70. SharedHourlyCosts map[string]float64
  71. Step time.Duration
  72. LabelConfig *LabelConfig
  73. }
  74. // CloudUsageQueryOptions define optional parameters for querying a Store
  75. type CloudUsageQueryOptions struct {
  76. Accumulate bool
  77. AggregateBy []string
  78. Compute bool
  79. Filter filter21.Filter
  80. FilterValues CloudUsageFilter
  81. LabelConfig *LabelConfig
  82. }
  83. type CloudUsageFilter struct {
  84. Categories []string `json:"categories"`
  85. Providers []string `json:"providers"`
  86. ProviderIDs []string `json:"providerIDs"`
  87. Accounts []string `json:"accounts"`
  88. Projects []string `json:"projects"`
  89. Services []string `json:"services"`
  90. Labels map[string][]string `json:"labels"`
  91. }
  92. // QueryAllocationAsync provide a functions for retrieving results from any AllocationQuerier Asynchronously
  93. func QueryAllocationAsync(allocationQuerier AllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *AllocationSetRange, chan error) {
  94. asrCh := make(chan *AllocationSetRange)
  95. errCh := make(chan error)
  96. go func(asrCh chan *AllocationSetRange, errCh chan error) {
  97. defer close(asrCh)
  98. defer close(errCh)
  99. asr, err := allocationQuerier.QueryAllocation(start, end, opts)
  100. if err != nil {
  101. errCh <- err
  102. return
  103. }
  104. asrCh <- asr
  105. }(asrCh, errCh)
  106. return asrCh, errCh
  107. }
  108. // QuerySummaryAllocationAsync provide a functions for retrieving results from any SummaryAllocationQuerier Asynchronously
  109. func QuerySummaryAllocationAsync(summaryAllocationQuerier SummaryAllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *SummaryAllocationSetRange, chan error) {
  110. asrCh := make(chan *SummaryAllocationSetRange)
  111. errCh := make(chan error)
  112. go func(asrCh chan *SummaryAllocationSetRange, errCh chan error) {
  113. defer close(asrCh)
  114. defer close(errCh)
  115. asr, err := summaryAllocationQuerier.QuerySummaryAllocation(start, end, opts)
  116. if err != nil {
  117. errCh <- err
  118. return
  119. }
  120. asrCh <- asr
  121. }(asrCh, errCh)
  122. return asrCh, errCh
  123. }
  124. // QueryAsseetAsync provide a functions for retrieving results from any AssetQuerier Asynchronously
  125. func QueryAssetAsync(assetQuerier AssetQuerier, start, end time.Time, opts *AssetQueryOptions) (chan *AssetSetRange, chan error) {
  126. asrCh := make(chan *AssetSetRange)
  127. errCh := make(chan error)
  128. go func(asrCh chan *AssetSetRange, errCh chan error) {
  129. defer close(asrCh)
  130. defer close(errCh)
  131. asr, err := assetQuerier.QueryAsset(start, end, opts)
  132. if err != nil {
  133. errCh <- err
  134. return
  135. }
  136. asrCh <- asr
  137. }(asrCh, errCh)
  138. return asrCh, errCh
  139. }
  140. // QueryCloudUsageAsync provide a functions for retrieving results from any CloudUsageQuerier Asynchronously
  141. func QueryCloudUsageAsync(cloudUsageQuerier CloudUsageQuerier, start, end time.Time, opts *CloudUsageQueryOptions) (chan *CloudUsageSetRange, chan error) {
  142. cusrCh := make(chan *CloudUsageSetRange)
  143. errCh := make(chan error)
  144. go func(cusrCh chan *CloudUsageSetRange, errCh chan error) {
  145. defer close(cusrCh)
  146. defer close(errCh)
  147. cusr, err := cloudUsageQuerier.QueryCloudUsage(start, end, opts)
  148. if err != nil {
  149. errCh <- err
  150. return
  151. }
  152. cusrCh <- cusr
  153. }(cusrCh, errCh)
  154. return cusrCh, errCh
  155. }