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" ;
10
+ import { BillableSession , BillableSessionRequest } from "@gitpod/gitpod-protocol/lib/usage" ;
11
11
import { log } from "@gitpod/gitpod-protocol/lib/util/logging" ;
12
- import { CachingUsageServiceClientProvider , UsageService } from "@gitpod/usage-api/lib/usage/v1/sugar" ;
12
+ import { GetUpcomingInvoiceResponse } from "@gitpod/usage-api/lib/usage/v1/billing_pb" ;
13
+ import {
14
+ CachingUsageServiceClientProvider ,
15
+ UsageService ,
16
+ CachingBillingServiceClientProvider ,
17
+ } from "@gitpod/usage-api/lib/usage/v1/sugar" ;
13
18
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb" ;
14
19
import { inject , injectable } from "inversify" ;
15
20
import { UserService } from "../../../src/user/user-service" ;
@@ -26,6 +31,8 @@ export class BillingService {
26
31
@inject ( CostCenterDB ) protected readonly costCenterDB : CostCenterDB ;
27
32
@inject ( CachingUsageServiceClientProvider )
28
33
protected readonly usageServiceClientProvider : CachingUsageServiceClientProvider ;
34
+ @inject ( CachingBillingServiceClientProvider )
35
+ protected readonly billingServiceClientProvider : CachingBillingServiceClientProvider ;
29
36
30
37
async checkSpendingLimitReached ( user : User ) : Promise < SpendingLimitReachedResult > {
31
38
const attributionId = await this . userService . getWorkspaceUsageAttributionId ( user ) ;
@@ -40,50 +47,45 @@ export class BillingService {
40
47
} ;
41
48
}
42
49
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
- } ;
50
+ if ( attributionId . kind === "team" ) {
51
+ const upcomingInvoice = await this . getUpcomingInvoice ( attributionId . teamId ) ;
52
+ const currentUsage = upcomingInvoice . getCredits ( ) ;
53
+ if ( currentUsage >= costCenter . spendingLimit ) {
54
+ log . info ( { userId : user . id } , "Spending limit reached" , {
55
+ attributionId,
56
+ currentUsage,
57
+ spendingLimit : costCenter . spendingLimit ,
58
+ } ) ;
59
+ return {
60
+ reached : true ,
61
+ attributionId,
62
+ } ;
63
+ } else if ( currentUsage > costCenter . spendingLimit * 0.8 ) {
64
+ log . info ( { userId : user . id } , "Spending limit almost reached" , {
65
+ attributionId,
66
+ currentUsage,
67
+ spendingLimit : costCenter . spendingLimit ,
68
+ } ) ;
69
+ return {
70
+ reached : false ,
71
+ almostReached : true ,
72
+ attributionId,
73
+ } ;
74
+ }
59
75
}
76
+
77
+ if ( attributionId . kind === "user" ) {
78
+ // TODO
79
+ }
80
+
60
81
return {
61
82
reached : false ,
62
83
attributionId,
63
84
} ;
64
85
}
65
86
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 ;
87
+ async getUpcomingInvoice ( teamId : string ) : Promise < GetUpcomingInvoiceResponse > {
88
+ const response = await this . billingServiceClientProvider . getDefault ( ) . getUpcomingInvoice ( teamId ) ;
89
+ return response ;
88
90
}
89
91
}
0 commit comments