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