query.go 5.3 KB

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