-
Notifications
You must be signed in to change notification settings - Fork 515
/
query.go
173 lines (143 loc) · 5.29 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package kubecost
import (
"time"
)
// Querier is an aggregate interface which has the ability to query each Kubecost store type
type Querier interface {
AllocationQuerier
SummaryAllocationQuerier
AssetQuerier
CloudUsageQuerier
}
// AllocationQuerier interface defining api for requesting Allocation data
type AllocationQuerier interface {
QueryAllocation(start, end time.Time, opts *AllocationQueryOptions) (*AllocationSetRange, error)
}
// SummaryAllocationQuerier interface defining api for requesting SummaryAllocation data
type SummaryAllocationQuerier interface {
QuerySummaryAllocation(start, end time.Time, opts *AllocationQueryOptions) (*SummaryAllocationSetRange, error)
}
// AssetQuerier interface defining api for requesting Asset data
type AssetQuerier interface {
QueryAsset(start, end time.Time, opts *AssetQueryOptions) (*AssetSetRange, error)
}
// CloudUsageQuerier interface defining api for requesting CloudUsage data
type CloudUsageQuerier interface {
QueryCloudUsage(start, end time.Time, opts *CloudUsageQueryOptions) (*CloudUsageSetRange, error)
}
// AllocationQueryOptions defines optional parameters for querying an Allocation Store
type AllocationQueryOptions struct {
Accumulate bool
AccumulateBy time.Duration
AggregateBy []string
Compute bool
DisableAggregatedStores bool
FilterFuncs []AllocationMatchFunc
IdleByNode bool
IncludeExternal bool
IncludeIdle bool
LabelConfig *LabelConfig
MergeUnallocated bool
Reconcile bool
ReconcileNetwork bool
ShareFuncs []AllocationMatchFunc
SharedHourlyCosts map[string]float64
ShareIdle string
ShareSplit string
ShareTenancyCosts bool
SplitIdle bool
Step time.Duration
}
// AssetQueryOptions defines optional parameters for querying an Asset Store
type AssetQueryOptions struct {
Accumulate bool
AggregateBy []string
Compute bool
DisableAdjustments bool
DisableAggregatedStores bool
FilterFuncs []AssetMatchFunc
IncludeCloud bool
SharedHourlyCosts map[string]float64
Step time.Duration
}
// CloudUsageQueryOptions define optional parameters for querying a Store
type CloudUsageQueryOptions struct {
Accumulate bool
AggregateBy []string
Compute bool
FilterFuncs []CloudUsageMatchFunc
FilterValues CloudUsageFilter
}
type CloudUsageFilter struct {
Categories []string `json:"categories"`
Providers []string `json:"providers"`
ProviderIDs []string `json:"providerIDs"`
Accounts []string `json:"accounts"`
Projects []string `json:"projects"`
Services []string `json:"services"`
Labels map[string][]string `json:"labels"`
}
// QueryAllocationAsync provide a functions for retrieving results from any AllocationQuerier Asynchronously
func QueryAllocationAsync(allocationQuerier AllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *AllocationSetRange, chan error) {
asrCh := make(chan *AllocationSetRange)
errCh := make(chan error)
go func(asrCh chan *AllocationSetRange, errCh chan error) {
defer close(asrCh)
defer close(errCh)
asr, err := allocationQuerier.QueryAllocation(start, end, opts)
if err != nil {
errCh <- err
return
}
asrCh <- asr
}(asrCh, errCh)
return asrCh, errCh
}
// QuerySummaryAllocationAsync provide a functions for retrieving results from any SummaryAllocationQuerier Asynchronously
func QuerySummaryAllocationAsync(summaryAllocationQuerier SummaryAllocationQuerier, start, end time.Time, opts *AllocationQueryOptions) (chan *SummaryAllocationSetRange, chan error) {
asrCh := make(chan *SummaryAllocationSetRange)
errCh := make(chan error)
go func(asrCh chan *SummaryAllocationSetRange, errCh chan error) {
defer close(asrCh)
defer close(errCh)
asr, err := summaryAllocationQuerier.QuerySummaryAllocation(start, end, opts)
if err != nil {
errCh <- err
return
}
asrCh <- asr
}(asrCh, errCh)
return asrCh, errCh
}
// QueryAsseetAsync provide a functions for retrieving results from any AssetQuerier Asynchronously
func QueryAssetAsync(assetQuerier AssetQuerier, start, end time.Time, opts *AssetQueryOptions) (chan *AssetSetRange, chan error) {
asrCh := make(chan *AssetSetRange)
errCh := make(chan error)
go func(asrCh chan *AssetSetRange, errCh chan error) {
defer close(asrCh)
defer close(errCh)
asr, err := assetQuerier.QueryAsset(start, end, opts)
if err != nil {
errCh <- err
return
}
asrCh <- asr
}(asrCh, errCh)
return asrCh, errCh
}
// QueryCloudUsageAsync provide a functions for retrieving results from any CloudUsageQuerier Asynchronously
func QueryCloudUsageAsync(cloudUsageQuerier CloudUsageQuerier, start, end time.Time, opts *CloudUsageQueryOptions) (chan *CloudUsageSetRange, chan error) {
cusrCh := make(chan *CloudUsageSetRange)
errCh := make(chan error)
go func(cusrCh chan *CloudUsageSetRange, errCh chan error) {
defer close(cusrCh)
defer close(errCh)
cusr, err := cloudUsageQuerier.QueryCloudUsage(start, end, opts)
if err != nil {
errCh <- err
return
}
cusrCh <- cusr
}(cusrCh, errCh)
return cusrCh, errCh
}