query.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. )
  60. // AssetQueryOptions defines optional parameters for querying an Asset Store
  61. type AssetQueryOptions struct {
  62. Accumulate bool
  63. AggregateBy []string
  64. Compute bool
  65. DisableAdjustments bool
  66. DisableAggregatedStores bool
  67. Filter filter21.Filter
  68. IncludeCloud bool
  69. SharedHourlyCosts map[string]float64
  70. Step time.Duration
  71. LabelConfig *LabelConfig
  72. }
  73. // CloudUsageQueryOptions define optional parameters for querying a Store
  74. type CloudUsageQueryOptions struct {
  75. Accumulate bool
  76. AggregateBy []string
  77. Compute bool
  78. Filter filter21.Filter
  79. FilterValues CloudUsageFilter
  80. LabelConfig *LabelConfig
  81. }
  82. type CloudUsageFilter struct {
  83. Categories []string `json:"categories"`
  84. Providers []string `json:"providers"`
  85. ProviderIDs []string `json:"providerIDs"`
  86. Accounts []string `json:"accounts"`
  87. Projects []string `json:"projects"`
  88. Services []string `json:"services"`
  89. Labels map[string][]string `json:"labels"`
  90. }
  91. // QueryAllocationAsync provide a functions for retrieving results from any AllocationQuerier Asynchronously
  92. func QueryAllocationAsync(allocationQuerier AllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *AllocationSetRange, chan error) {
  93. asrCh := make(chan *AllocationSetRange)
  94. errCh := make(chan error)
  95. go func(asrCh chan *AllocationSetRange, errCh chan error) {
  96. defer close(asrCh)
  97. defer close(errCh)
  98. asr, err := allocationQuerier.QueryAllocation(start, end, opts)
  99. if err != nil {
  100. errCh <- err
  101. return
  102. }
  103. asrCh <- asr
  104. }(asrCh, errCh)
  105. return asrCh, errCh
  106. }
  107. // QuerySummaryAllocationAsync provide a functions for retrieving results from any SummaryAllocationQuerier Asynchronously
  108. func QuerySummaryAllocationAsync(summaryAllocationQuerier SummaryAllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *SummaryAllocationSetRange, chan error) {
  109. asrCh := make(chan *SummaryAllocationSetRange)
  110. errCh := make(chan error)
  111. go func(asrCh chan *SummaryAllocationSetRange, errCh chan error) {
  112. defer close(asrCh)
  113. defer close(errCh)
  114. asr, err := summaryAllocationQuerier.QuerySummaryAllocation(start, end, opts)
  115. if err != nil {
  116. errCh <- err
  117. return
  118. }
  119. asrCh <- asr
  120. }(asrCh, errCh)
  121. return asrCh, errCh
  122. }
  123. // QueryAsseetAsync provide a functions for retrieving results from any AssetQuerier Asynchronously
  124. func QueryAssetAsync(assetQuerier AssetQuerier, start, end time.Time, opts *AssetQueryOptions) (chan *AssetSetRange, chan error) {
  125. asrCh := make(chan *AssetSetRange)
  126. errCh := make(chan error)
  127. go func(asrCh chan *AssetSetRange, errCh chan error) {
  128. defer close(asrCh)
  129. defer close(errCh)
  130. asr, err := assetQuerier.QueryAsset(start, end, opts)
  131. if err != nil {
  132. errCh <- err
  133. return
  134. }
  135. asrCh <- asr
  136. }(asrCh, errCh)
  137. return asrCh, errCh
  138. }
  139. // QueryCloudUsageAsync provide a functions for retrieving results from any CloudUsageQuerier Asynchronously
  140. func QueryCloudUsageAsync(cloudUsageQuerier CloudUsageQuerier, start, end time.Time, opts *CloudUsageQueryOptions) (chan *CloudUsageSetRange, chan error) {
  141. cusrCh := make(chan *CloudUsageSetRange)
  142. errCh := make(chan error)
  143. go func(cusrCh chan *CloudUsageSetRange, errCh chan error) {
  144. defer close(cusrCh)
  145. defer close(errCh)
  146. cusr, err := cloudUsageQuerier.QueryCloudUsage(start, end, opts)
  147. if err != nil {
  148. errCh <- err
  149. return
  150. }
  151. cusrCh <- cusr
  152. }(cusrCh, errCh)
  153. return cusrCh, errCh
  154. }