@@ -7,6 +7,7 @@ package apiv1
7
7
import (
8
8
context "context"
9
9
"github.com/gitpod-io/gitpod/common-go/log"
10
+ "github.com/gitpod-io/gitpod/usage/pkg/controller"
10
11
"time"
11
12
12
13
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
@@ -21,12 +22,15 @@ var _ v1.UsageServiceServer = (*UsageService)(nil)
21
22
22
23
type UsageService struct {
23
24
conn * gorm.DB
25
+
26
+ usageCtrl * controller.UsageReconciler
27
+
24
28
v1.UnimplementedUsageServiceServer
25
29
}
26
30
27
31
const maxQuerySize = 31 * 24 * time .Hour
28
32
29
- func (us * UsageService ) ListBilledUsage (ctx context.Context , in * v1.ListBilledUsageRequest ) (* v1.ListBilledUsageResponse , error ) {
33
+ func (s * UsageService ) ListBilledUsage (ctx context.Context , in * v1.ListBilledUsageRequest ) (* v1.ListBilledUsageResponse , error ) {
30
34
to := time .Now ()
31
35
if in .To != nil {
32
36
to = in .To .AsTime ()
@@ -52,7 +56,7 @@ func (us *UsageService) ListBilledUsage(ctx context.Context, in *v1.ListBilledUs
52
56
order = db .DescendingOrder
53
57
}
54
58
55
- usageRecords , err := db .ListUsage (ctx , us .conn , db .AttributionID (in .GetAttributionId ()), from , to , order )
59
+ usageRecords , err := db .ListUsage (ctx , s .conn , db .AttributionID (in .GetAttributionId ()), from , to , order )
56
60
if err != nil {
57
61
log .Log .
58
62
WithField ("attribution_id" , in .AttributionId ).
@@ -88,6 +92,54 @@ func (us *UsageService) ListBilledUsage(ctx context.Context, in *v1.ListBilledUs
88
92
}, nil
89
93
}
90
94
91
- func NewUsageService (conn * gorm.DB ) * UsageService {
92
- return & UsageService {conn : conn }
95
+ func (s * UsageService ) CollectUsage (ctx context.Context , req * v1.CollectUsageRequest ) (* v1.CollectUsageResponse , error ) {
96
+ from := req .GetStartTime ().AsTime ()
97
+ to := req .GetEndTime ().AsTime ()
98
+
99
+ if from .Before (to ) {
100
+ return nil , status .Errorf (codes .InvalidArgument , "End time must be after start time" )
101
+ }
102
+
103
+ _ , report , err := s .usageCtrl .ReconcileTimeRange (ctx , from , to )
104
+ if err != nil {
105
+ log .Log .WithError (err ).Error ("Failed to reconcile time range." )
106
+ return nil , status .Error (codes .Internal , "failed to reconcile time range" )
107
+ }
108
+
109
+ var sessions []* v1.BilledSession
110
+ for _ , instance := range report {
111
+ sessions = append (sessions , usageRecordToBilledUsageProto (instance ))
112
+ }
113
+
114
+ return & v1.CollectUsageResponse {
115
+ Sessions : sessions ,
116
+ }, nil
117
+
118
+ }
119
+
120
+ func NewUsageService (conn * gorm.DB , usageCtrl * controller.UsageReconciler ) * UsageService {
121
+ return & UsageService {
122
+ conn : conn ,
123
+ usageCtrl : usageCtrl ,
124
+ }
125
+ }
126
+
127
+ func usageRecordToBilledUsageProto (usageRecord db.WorkspaceInstanceUsage ) * v1.BilledSession {
128
+ var endTime * timestamppb.Timestamp
129
+ if usageRecord .StoppedAt .Valid {
130
+ endTime = timestamppb .New (usageRecord .StoppedAt .Time )
131
+ }
132
+ return & v1.BilledSession {
133
+ AttributionId : string (usageRecord .AttributionID ),
134
+ UserId : usageRecord .UserID .String (),
135
+ WorkspaceId : usageRecord .WorkspaceID ,
136
+ TeamId : "" ,
137
+ WorkspaceType : string (usageRecord .WorkspaceType ),
138
+ ProjectId : usageRecord .ProjectID ,
139
+ InstanceId : usageRecord .InstanceID .String (),
140
+ WorkspaceClass : usageRecord .WorkspaceClass ,
141
+ StartTime : timestamppb .New (usageRecord .StartedAt ),
142
+ EndTime : endTime ,
143
+ Credits : usageRecord .CreditsUsed ,
144
+ }
93
145
}
0 commit comments