7
7
import { CostCenterDB } from "@gitpod/gitpod-db/lib" ;
8
8
import { User } from "@gitpod/gitpod-protocol" ;
9
9
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution" ;
10
- import { BillableSession , BillableSessionRequest , SortOrder } from "@gitpod/gitpod-protocol/lib/usage" ;
11
10
import { log } from "@gitpod/gitpod-protocol/lib/util/logging" ;
12
- import { CachingUsageServiceClientProvider , UsageService } from "@gitpod/usage-api/lib/usage/v1/sugar" ;
13
- import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb" ;
11
+ import { GetUpcomingInvoiceResponse } from "@gitpod/usage-api/lib/usage/v1/billing_pb" ;
12
+ import {
13
+ CachingUsageServiceClientProvider ,
14
+ CachingBillingServiceClientProvider ,
15
+ } from "@gitpod/usage-api/lib/usage/v1/sugar" ;
14
16
import { inject , injectable } from "inversify" ;
15
17
import { UserService } from "../../../src/user/user-service" ;
16
18
@@ -26,6 +28,8 @@ export class BillingService {
26
28
@inject ( CostCenterDB ) protected readonly costCenterDB : CostCenterDB ;
27
29
@inject ( CachingUsageServiceClientProvider )
28
30
protected readonly usageServiceClientProvider : CachingUsageServiceClientProvider ;
31
+ @inject ( CachingBillingServiceClientProvider )
32
+ protected readonly billingServiceClientProvider : CachingBillingServiceClientProvider ;
29
33
30
34
async checkSpendingLimitReached ( user : User ) : Promise < SpendingLimitReachedResult > {
31
35
const attributionId = await this . userService . getWorkspaceUsageAttributionId ( user ) ;
@@ -40,50 +44,45 @@ export class BillingService {
40
44
} ;
41
45
}
42
46
43
- const allSessions = await this . listBilledUsage ( {
44
- attributionId : AttributionId . render ( attributionId ) ,
45
- startedTimeOrder : SortOrder . Descending ,
46
- } ) ;
47
- const totalUsage = allSessions . map ( ( s ) => s . credits ) . reduce ( ( a , b ) => a + b , 0 ) ;
48
- if ( totalUsage >= costCenter . spendingLimit ) {
49
- return {
50
- reached : true ,
51
- attributionId,
52
- } ;
53
- } else if ( totalUsage > costCenter . spendingLimit * 0.8 ) {
54
- return {
55
- reached : false ,
56
- almostReached : true ,
57
- attributionId,
58
- } ;
47
+ if ( attributionId . kind === "team" ) {
48
+ const upcomingInvoice = await this . getUpcomingInvoice ( attributionId . teamId ) ;
49
+ const currentUsage = upcomingInvoice . getCredits ( ) ;
50
+ if ( currentUsage >= costCenter . spendingLimit ) {
51
+ log . info ( { userId : user . id } , "Spending limit reached" , {
52
+ attributionId,
53
+ currentUsage,
54
+ spendingLimit : costCenter . spendingLimit ,
55
+ } ) ;
56
+ return {
57
+ reached : true ,
58
+ attributionId,
59
+ } ;
60
+ } else if ( currentUsage > costCenter . spendingLimit * 0.8 ) {
61
+ log . info ( { userId : user . id } , "Spending limit almost reached" , {
62
+ attributionId,
63
+ currentUsage,
64
+ spendingLimit : costCenter . spendingLimit ,
65
+ } ) ;
66
+ return {
67
+ reached : false ,
68
+ almostReached : true ,
69
+ attributionId,
70
+ } ;
71
+ }
59
72
}
73
+
74
+ if ( attributionId . kind === "user" ) {
75
+ // TODO
76
+ }
77
+
60
78
return {
61
79
reached : false ,
62
80
attributionId,
63
81
} ;
64
82
}
65
83
66
- // TODO (gpl): Replace this with call to stripeService.getInvoice()
67
- async listBilledUsage ( req : BillableSessionRequest ) : Promise < BillableSession [ ] > {
68
- const { attributionId, startedTimeOrder, from, to } = req ;
69
- let timestampFrom ;
70
- let timestampTo ;
71
-
72
- if ( from ) {
73
- timestampFrom = Timestamp . fromDate ( new Date ( from ) ) ;
74
- }
75
- if ( to ) {
76
- timestampTo = Timestamp . fromDate ( new Date ( to ) ) ;
77
- }
78
- const usageClient = this . usageServiceClientProvider . getDefault ( ) ;
79
- const response = await usageClient . listBilledUsage (
80
- { } ,
81
- attributionId ,
82
- startedTimeOrder as number ,
83
- timestampFrom ,
84
- timestampTo ,
85
- ) ;
86
- const sessions = response . getSessionsList ( ) . map ( ( s ) => UsageService . mapBilledSession ( s ) ) ;
87
- return sessions ;
84
+ async getUpcomingInvoice ( teamId : string ) : Promise < GetUpcomingInvoiceResponse > {
85
+ const response = await this . billingServiceClientProvider . getDefault ( ) . getUpcomingInvoice ( teamId ) ;
86
+ return response ;
88
87
}
89
88
}
0 commit comments