query.go 5.3 KB

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