query.go 5.6 KB

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