-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathreconciler.go
268 lines (219 loc) · 8.1 KB
/
reconciler.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
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package controller
import (
"bytes"
"compress/gzip"
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
"time"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/usage/pkg/contentservice"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Reconciler interface {
Reconcile() error
}
type ReconcilerFunc func() error
func (f ReconcilerFunc) Reconcile() error {
return f()
}
type UsageReconciler struct {
nowFunc func() time.Time
conn *gorm.DB
pricer *WorkspacePricer
billingController BillingController
contentService contentservice.Interface
}
func NewUsageReconciler(conn *gorm.DB, pricer *WorkspacePricer, billingController BillingController, contentService contentservice.Interface) *UsageReconciler {
return &UsageReconciler{
conn: conn,
pricer: pricer,
billingController: billingController,
contentService: contentService,
nowFunc: time.Now,
}
}
type UsageReconcileStatus struct {
StartTime time.Time
EndTime time.Time
WorkspaceInstances int
InvalidWorkspaceInstances int
}
func (u *UsageReconciler) Reconcile() (err error) {
ctx := context.Background()
now := time.Now().UTC()
reportUsageReconcileStarted()
defer func() {
reportUsageReconcileFinished(time.Since(now), err)
}()
startOfCurrentMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
startOfNextMonth := startOfCurrentMonth.AddDate(0, 1, 0)
status, report, err := u.ReconcileTimeRange(ctx, startOfCurrentMonth, startOfNextMonth)
if err != nil {
return err
}
log.WithField("usage_reconcile_status", status).Info("Reconcile completed.")
reportBytes := &bytes.Buffer{}
gz := gzip.NewWriter(reportBytes)
err = json.NewEncoder(gz).Encode(report)
if err != nil {
return fmt.Errorf("failed to marshal report to JSON: %w", err)
}
err = gz.Close()
if err != nil {
return fmt.Errorf("failed to compress usage report: %w", err)
}
err = db.CreateUsageRecords(ctx, u.conn, usageReportToUsageRecords(report, u.pricer, u.nowFunc().UTC()))
if err != nil {
return fmt.Errorf("failed to write usage records to database: %s", err)
}
filename := fmt.Sprintf("%s.gz", now.Format(time.RFC3339))
err = u.contentService.UploadFile(ctx, filename, reportBytes)
if err != nil {
return fmt.Errorf("failed to upload usage report: %w", err)
}
return nil
}
func (u *UsageReconciler) ReconcileTimeRange(ctx context.Context, from, to time.Time) (*UsageReconcileStatus, UsageReport, error) {
now := u.nowFunc().UTC()
log.Infof("Gathering usage data from %s to %s", from, to)
status := &UsageReconcileStatus{
StartTime: from,
EndTime: to,
}
instances, invalidInstances, err := u.loadWorkspaceInstances(ctx, from, to)
if err != nil {
return nil, nil, fmt.Errorf("failed to load workspace instances: %w", err)
}
status.WorkspaceInstances = len(instances)
status.InvalidWorkspaceInstances = len(invalidInstances)
if len(invalidInstances) > 0 {
log.WithField("invalid_workspace_instances", invalidInstances).Errorf("Detected %d invalid instances. These will be skipped in the current run.", len(invalidInstances))
}
log.WithField("workspace_instances", instances).Debug("Successfully loaded workspace instances.")
instancesByAttributionID := groupInstancesByAttributionID(instances)
err = u.billingController.Reconcile(ctx, now, instancesByAttributionID)
if err != nil {
return nil, nil, fmt.Errorf("failed to reconcile billing: %w", err)
}
return status, instancesByAttributionID, nil
}
type UsageReport map[db.AttributionID][]db.WorkspaceInstanceForUsage
func (u UsageReport) CreditSummaryForTeams(pricer *WorkspacePricer, maxStopTime time.Time) map[string]int64 {
creditsPerTeamID := map[string]int64{}
for attribution, instances := range u {
entity, id := attribution.Values()
if entity != db.AttributionEntity_Team {
continue
}
var credits float64
for _, instance := range instances {
credits += pricer.CreditsUsedByInstance(&instance, maxStopTime)
}
creditsPerTeamID[id] = int64(math.Ceil(credits))
}
return creditsPerTeamID
}
type invalidWorkspaceInstance struct {
reason string
workspaceInstanceID uuid.UUID
}
func (u *UsageReconciler) loadWorkspaceInstances(ctx context.Context, from, to time.Time) ([]db.WorkspaceInstanceForUsage, []invalidWorkspaceInstance, error) {
log.Infof("Gathering usage data from %s to %s", from, to)
instances, err := db.ListWorkspaceInstancesInRange(ctx, u.conn, from, to)
if err != nil {
return nil, nil, fmt.Errorf("failed to list instances from db: %w", err)
}
log.Infof("Identified %d instances between %s and %s", len(instances), from, to)
valid, invalid := validateInstances(instances)
trimmed := trimStartStopTime(valid, from, to)
return trimmed, invalid, nil
}
func validateInstances(instances []db.WorkspaceInstanceForUsage) (valid []db.WorkspaceInstanceForUsage, invalid []invalidWorkspaceInstance) {
for _, i := range instances {
// i is a pointer to the current element, we need to assign it to ensure we're copying the value, not the current pointer.
instance := i
// Each instance must have a start time, without it, we do not have a baseline for usage computation.
if !instance.CreationTime.IsSet() {
invalid = append(invalid, invalidWorkspaceInstance{
reason: "missing creation time",
workspaceInstanceID: instance.ID,
})
continue
}
start := instance.CreationTime.Time()
// Currently running instances do not have a stopped time set, so we ignore these.
if instance.StoppedTime.IsSet() {
stop := instance.StoppedTime.Time()
if stop.Before(start) {
invalid = append(invalid, invalidWorkspaceInstance{
reason: "stop time is before start time",
workspaceInstanceID: instance.ID,
})
continue
}
}
valid = append(valid, instance)
}
return valid, invalid
}
// trimStartStopTime ensures that start time or stop time of an instance is never outside of specified start or stop time range.
func trimStartStopTime(instances []db.WorkspaceInstanceForUsage, maximumStart, minimumStop time.Time) []db.WorkspaceInstanceForUsage {
var updated []db.WorkspaceInstanceForUsage
for _, instance := range instances {
if instance.CreationTime.Time().Before(maximumStart) {
instance.CreationTime = db.NewVarcharTime(maximumStart)
}
if instance.StoppedTime.Time().After(minimumStop) {
instance.StoppedTime = db.NewVarcharTime(minimumStop)
}
updated = append(updated, instance)
}
return updated
}
func groupInstancesByAttributionID(instances []db.WorkspaceInstanceForUsage) UsageReport {
result := map[db.AttributionID][]db.WorkspaceInstanceForUsage{}
for _, instance := range instances {
if _, ok := result[instance.UsageAttributionID]; !ok {
result[instance.UsageAttributionID] = []db.WorkspaceInstanceForUsage{}
}
result[instance.UsageAttributionID] = append(result[instance.UsageAttributionID], instance)
}
return result
}
func usageReportToUsageRecords(report UsageReport, pricer *WorkspacePricer, now time.Time) []db.WorkspaceInstanceUsage {
var usageRecords []db.WorkspaceInstanceUsage
for attributionId, instances := range report {
for _, instance := range instances {
var stoppedAt sql.NullTime
if instance.StoppedTime.IsSet() {
stoppedAt = sql.NullTime{Time: instance.StoppedTime.Time(), Valid: true}
}
projectID := ""
if instance.ProjectID.Valid {
projectID = instance.ProjectID.String
}
usageRecords = append(usageRecords, db.WorkspaceInstanceUsage{
InstanceID: instance.ID,
AttributionID: attributionId,
WorkspaceID: instance.WorkspaceID,
ProjectID: projectID,
UserID: instance.OwnerID,
WorkspaceType: instance.Type,
WorkspaceClass: instance.WorkspaceClass,
StartedAt: instance.CreationTime.Time(),
StoppedAt: stoppedAt,
CreditsUsed: pricer.CreditsUsedByInstance(&instance, now),
GenerationID: 0,
})
}
}
return usageRecords
}