-
Notifications
You must be signed in to change notification settings - Fork 523
/
usages.go
280 lines (237 loc) 路 8.04 KB
/
usages.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package prices
import (
"runtime"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/apiclient"
"github.com/infracost/infracost/internal/config"
"github.com/infracost/infracost/internal/schema"
"github.com/infracost/infracost/internal/usage"
)
// PopulateActualCosts fetches cloud provider reported costs from the Infracost
// Cloud Usage API and adds corresponding cost components to the project's resources
func PopulateActualCosts(ctx *config.RunContext, project *schema.Project) error {
resources := project.AllResources()
c := apiclient.NewUsageAPIClient(ctx)
err := popResourcesConcurrent(ctx, c, project, resources)
if err != nil {
return err
}
return nil
}
// popResourcesConcurrent gets the actual usage of all resources concurrently.
// Concurrency level is calculated using the following formula:
// max(min(4, numCPU * 4), 16)
func popResourcesConcurrent(ctx *config.RunContext, c *apiclient.UsageAPIClient, project *schema.Project, resources []*schema.Resource) error {
// Set the number of workers
numWorkers := 4
numCPU := runtime.NumCPU()
if numCPU*4 > numWorkers {
numWorkers = numCPU * 4
}
if numWorkers > 16 {
numWorkers = 16
}
numJobs := len(resources)
jobs := make(chan *schema.Resource, numJobs)
resultErrors := make(chan error, numJobs)
// Fire up the workers
for i := 0; i < numWorkers; i++ {
go func(jobs <-chan *schema.Resource, resultErrors chan<- error) {
for r := range jobs {
err := popResourceActualCosts(ctx, c, project, r)
resultErrors <- err
}
}(jobs, resultErrors)
}
// Feed the workers the jobs of getting prices
for _, r := range resources {
jobs <- r
}
// Get the result of the jobs
for i := 0; i < numJobs; i++ {
err := <-resultErrors
if err != nil {
return err
}
}
return nil
}
func popResourceActualCosts(ctx *config.RunContext, c *apiclient.UsageAPIClient, project *schema.Project, r *schema.Resource) error {
if r.IsSkipped {
return nil
}
vars := apiclient.ActualCostsQueryVariables{
RepoURL: ctx.VCSRepositoryURL(),
ProjectWithWorkspace: project.NameWithWorkspace(),
Address: r.Name,
Currency: c.Currency,
}
actualCostResults, err := c.ListActualCosts(vars)
if actualCostResults == nil || err != nil {
return err
}
for _, actualCost := range actualCostResults {
actualCosts := &schema.ActualCosts{
ResourceID: actualCost.ResourceID,
StartTimestamp: actualCost.StartTimestamp.UTC(),
EndTimestamp: actualCost.EndTimestamp.UTC(),
CostComponents: make([]*schema.CostComponent, 0, len(actualCost.CostComponents)),
}
for _, actual := range actualCost.CostComponents {
monthlyCost, err := decimal.NewFromString(actual.MonthlyCost)
if err != nil {
break
}
monthlyQuantity, err := decimal.NewFromString(actual.MonthlyQuantity)
if err != nil {
break
}
price, err := decimal.NewFromString(actual.Price)
if err != nil {
break
}
cc := &schema.CostComponent{
Name: actual.Description,
Unit: actual.Unit,
UnitMultiplier: decimal.NewFromInt(1),
MonthlyCost: &monthlyCost,
MonthlyQuantity: &monthlyQuantity,
}
cc.SetPrice(price)
actualCosts.CostComponents = append(actualCosts.CostComponents, cc)
}
if len(actualCosts.CostComponents) > 0 {
r.ActualCosts = append(r.ActualCosts, actualCosts)
}
}
return nil
}
// chunkPartialResourcesWithUsage collects all partiral resources with a core resource usage schema
// into groups of the specified chunkSize
func chunkPartialResourcesWithUsage(resources []*schema.PartialResource, chunkSize int) [][]*schema.PartialResource {
var usageResourceChunks [][]*schema.PartialResource
var currentChunk []*schema.PartialResource
for _, rb := range resources {
if rb.CoreResource != nil && len(rb.CoreResource.UsageSchema()) > 0 {
if len(currentChunk) >= chunkSize {
usageResourceChunks = append(usageResourceChunks, currentChunk)
currentChunk = nil
}
currentChunk = append(currentChunk, rb)
}
}
if len(currentChunk) > 0 {
usageResourceChunks = append(usageResourceChunks, currentChunk)
}
return usageResourceChunks
}
// FetchUsageData fetches usage estimates derived from cloud provider reported usage
// from the Infracost Cloud Usage API for each supported resource in the project
func FetchUsageData(ctx *config.RunContext, project *schema.Project) (schema.UsageMap, error) {
c := apiclient.NewUsageAPIClient(ctx)
// Set the number of workers
numWorkers := 4
numCPU := runtime.NumCPU()
if numCPU*4 > numWorkers {
numWorkers = numCPU * 4
}
if numWorkers > 16 {
numWorkers = 16
}
// gather all the CoreResource into chunks
usageResourceChunks := chunkPartialResourcesWithUsage(project.AllPartialResources(), 10)
usageMap := make(map[string]*schema.UsageData, len(usageResourceChunks)*10)
numJobs := len(usageResourceChunks)
jobs := make(chan []*schema.PartialResource, numJobs)
responses := make(chan batchResponse, numJobs)
// Fire up the workers
for i := 0; i < numWorkers; i++ {
go func(jobs <-chan []*schema.PartialResource, responses chan<- batchResponse) {
for req := range jobs {
res := fetchUsageDataBatch(ctx, c, project, req)
responses <- res
}
}(jobs, responses)
}
// Feed the workers the jobs
for _, r := range usageResourceChunks {
jobs <- r
}
// Get the result of the jobs
for i := 0; i < numJobs; i++ {
res := <-responses
if res.Error != nil {
return schema.NewUsageMap(usageMap), res.Error
}
for _, ud := range res.UsageData {
usageMap[ud.Address] = ud
}
}
return schema.NewUsageMap(usageMap), nil
}
type batchResponse struct {
UsageData []*schema.UsageData
Error error
}
func fetchUsageDataBatch(ctx *config.RunContext, c *apiclient.UsageAPIClient, project *schema.Project, resources []*schema.PartialResource) batchResponse {
chunkedQueryVars := make([]*apiclient.UsageQuantitiesQueryVariables, 0, len(resources))
for _, partialResource := range resources {
var usageParams []schema.UsageParam
if crWithUsageParams, ok := partialResource.CoreResource.(schema.CoreResourceWithUsageParams); ok {
usageParams = crWithUsageParams.UsageEstimationParams()
}
queryVars := apiclient.UsageQuantitiesQueryVariables{
RepoURL: ctx.VCSRepositoryURL(),
ProjectWithWorkspace: project.NameWithWorkspace(),
ResourceType: partialResource.CoreResource.CoreType(),
Address: partialResource.Address,
UsageKeys: flattenUsageKeys(partialResource.CoreResource.UsageSchema()),
UsageParams: usageParams,
}
chunkedQueryVars = append(chunkedQueryVars, &queryVars)
}
ud, err := c.ListUsageQuantities(chunkedQueryVars)
return batchResponse{
UsageData: ud,
Error: err,
}
}
// UploadCloudResourceIDs sends the project scoped cloud resource ids to the Usage API, so they can be used
// to provide usage estimates.
func UploadCloudResourceIDs(ctx *config.RunContext, project *schema.Project) error {
c := apiclient.NewUsageAPIClient(ctx)
var resourceIDs []apiclient.ResourceIDAddress
for _, partial := range project.AllPartialResources() {
for _, resourceID := range partial.CloudResourceIDs {
resourceIDs = append(resourceIDs, apiclient.ResourceIDAddress{
Address: partial.Address,
ResourceID: resourceID},
)
}
}
vars := apiclient.CloudResourceIDVariables{
RepoURL: ctx.VCSRepositoryURL(),
ProjectWithWorkspace: project.NameWithWorkspace(),
ResourceIDAddresses: resourceIDs,
}
err := c.UploadCloudResourceIDs(vars)
if err != nil {
return err
}
return nil
}
func flattenUsageKeys(usageSchema []*schema.UsageItem) []string {
usageKeys := make([]string, 0, len(usageSchema))
for _, usageItem := range usageSchema {
if usageItem.ValueType == schema.SubResourceUsage {
ru := usageItem.DefaultValue.(*usage.ResourceUsage)
// recursively flatten any nested keys, then add them to the current list
for _, nestedKey := range flattenUsageKeys(ru.Items) {
usageKeys = append(usageKeys, usageItem.Key+"."+nestedKey)
}
} else {
usageKeys = append(usageKeys, usageItem.Key)
}
}
return usageKeys
}