query.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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) (chan *AllocationSetRange, chan error)
  15. QueryAllocationSync(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) (chan *SummaryAllocationSetRange, chan error)
  20. QuerySummaryAllocationSync(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 bool
  33. AccumulateBy time.Duration
  34. AggregateBy []string
  35. Compute bool
  36. FilterFuncs []AllocationMatchFunc
  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. // AssetQueryOptions defines optional parameters for querying an Asset Store
  53. type AssetQueryOptions struct {
  54. Accumulate bool
  55. AggregateBy []string
  56. AwaitCoverage bool
  57. Compute bool
  58. DisableAdjustments bool
  59. FilterFuncs []AssetMatchFunc
  60. ShareFuncs []AssetMatchFunc
  61. SharedHourlyCosts map[string]float64
  62. Step time.Duration
  63. }
  64. // CloudUsageQueryOptions define optional parameters for querying a Store
  65. type CloudUsageQueryOptions struct {
  66. Accumulate bool
  67. AggregateBy []string
  68. AwaitCoverage bool
  69. FilterFuncs []CloudUsageMatchFunc
  70. Step time.Duration
  71. }
  72. // QueryAllocationAsync provide a functions for retrieving results from any AllocationQuerier Asynchronously
  73. func QueryAllocationAsync(allocationQuerier *AllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *AllocationSetRange, chan error) {
  74. asrCh := make(chan *AllocationSetRange)
  75. errCh := make(chan error)
  76. go func(asrCh chan *AllocationSetRange, errCh chan error) {
  77. defer close(asrCh)
  78. defer close(errCh)
  79. asr, err := allocationQuerier.QueryAllocationSync(start, end, opts)
  80. if err != nil {
  81. errCh <- err
  82. return
  83. }
  84. asrCh <- asr
  85. }(asrCh, errCh)
  86. return asrCh, errCh
  87. }
  88. // QuerySummaryAllocationAsync provide a functions for retrieving results from any SummaryAllocationQuerier Asynchronously
  89. func QuerySummaryAllocationAsync(summaryAllocationQuerier *SummaryAllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *SummaryAllocationSetRange, chan error) {
  90. asrCh := make(chan *SummaryAllocationSetRange)
  91. errCh := make(chan error)
  92. go func(asrCh chan *SummaryAllocationSetRange, errCh chan error) {
  93. defer close(asrCh)
  94. defer close(errCh)
  95. asr, err := summaryAllocationQuerier.QuerySummaryAllocationSync(start, end, opts)
  96. if err != nil {
  97. errCh <- err
  98. return
  99. }
  100. asrCh <- asr
  101. }(asrCh, errCh)
  102. return asrCh, errCh
  103. }
  104. // QueryAsseetAsync provide a functions for retrieving results from any AssetQuerier Asynchronously
  105. func QueryAssetAsync(assetQuerier *AssetQuerier, start, end time.Time, opts *AssetQueryOptions) (chan *AssetSetRange, chan error) {
  106. asrCh := make(chan *AssetSetRange)
  107. errCh := make(chan error)
  108. go func(asrCh chan *AssetSetRange, errCh chan error) {
  109. defer close(asrCh)
  110. defer close(errCh)
  111. asr, err := assetQuerier.QueryAsset(start, end, opts)
  112. if err != nil {
  113. errCh <- err
  114. return
  115. }
  116. asrCh <- asr
  117. } (asrCh, errCh)
  118. return asrCh, errCh
  119. }
  120. // QueryCloudUsage provide a functions for retrieving results from any CloudUsageQuerier Asynchronously
  121. func QueryCloudUsage(cloudUsageQuerier CloudUsageQuerier, start, end time.Time, opts *CloudUsageQueryOptions) (chan *CloudUsageSetRange, chan error) {
  122. cusrCh := make(chan *CloudUsageSetRange)
  123. errCh := make(chan error)
  124. go func(cusrCh chan *CloudUsageSetRange, errCh chan error) {
  125. defer close(cusrCh)
  126. defer close(errCh)
  127. cusr, err := cloudUsageQuerier.QueryCloudUsage(start, end, opts)
  128. if err != nil {
  129. errCh <- err
  130. return
  131. }
  132. cusrCh <- cusr
  133. }(cusrCh, errCh)
  134. return cusrCh, errCh
  135. }