query.go 6.2 KB

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