query.go 4.6 KB

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