File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
components/usage/pkg/controller Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ package controller
6
6
7
7
import (
8
8
"context"
9
+ "database/sql"
9
10
"encoding/json"
10
11
"fmt"
11
12
"io/ioutil"
@@ -85,6 +86,11 @@ func (u *UsageReconciler) Reconcile() (err error) {
85
86
}
86
87
log .Infof ("Wrote usage report into %s" , filepath .Join (dir , stat .Name ()))
87
88
89
+ err = db .CreateUsageRecords (ctx , u .conn , usageReportToUsageRecords (report ))
90
+ if err != nil {
91
+ return fmt .Errorf ("failed to write usage records to database: %s" , err )
92
+ }
93
+
88
94
return nil
89
95
}
90
96
@@ -225,3 +231,28 @@ func groupInstancesByAttributionID(instances []db.WorkspaceInstanceForUsage) Usa
225
231
226
232
return result
227
233
}
234
+
235
+ func usageReportToUsageRecords (report UsageReport ) []db.WorkspaceInstanceUsage {
236
+ var usageRecords []db.WorkspaceInstanceUsage
237
+
238
+ for attributionId , instances := range report {
239
+ for _ , instance := range instances {
240
+ var stoppedAt sql.NullTime
241
+ if instance .StoppedTime .IsSet () {
242
+ stoppedAt .Time = instance .StoppedTime .Time ()
243
+ }
244
+
245
+ usageRecords = append (usageRecords , db.WorkspaceInstanceUsage {
246
+ WorkspaceID : instance .ID ,
247
+ AttributionID : attributionId ,
248
+ StartedAt : instance .CreationTime .Time (),
249
+ StoppedAt : stoppedAt ,
250
+ CreditsUsed : 0 ,
251
+ GenerationId : 0 ,
252
+ Deleted : false ,
253
+ })
254
+ }
255
+ }
256
+
257
+ return usageRecords
258
+ }
You can’t perform that action at this time.
0 commit comments