Skip to content

Commit b0b24d5

Browse files
committed
Use upcoming invoice
1 parent 1cd0fbf commit b0b24d5

File tree

14 files changed

+447
-308
lines changed

14 files changed

+447
-308
lines changed

components/server/ee/src/billing/billing-service.ts

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import { CostCenterDB } from "@gitpod/gitpod-db/lib";
88
import { User } from "@gitpod/gitpod-protocol";
99
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
10-
import { BillableSession, BillableSessionRequest, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
1110
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";
1416
import { inject, injectable } from "inversify";
1517
import { UserService } from "../../../src/user/user-service";
1618

@@ -26,6 +28,8 @@ export class BillingService {
2628
@inject(CostCenterDB) protected readonly costCenterDB: CostCenterDB;
2729
@inject(CachingUsageServiceClientProvider)
2830
protected readonly usageServiceClientProvider: CachingUsageServiceClientProvider;
31+
@inject(CachingBillingServiceClientProvider)
32+
protected readonly billingServiceClientProvider: CachingBillingServiceClientProvider;
2933

3034
async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> {
3135
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
@@ -40,50 +44,45 @@ export class BillingService {
4044
};
4145
}
4246

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+
}
5972
}
73+
74+
if (attributionId.kind === "user") {
75+
// TODO
76+
}
77+
6078
return {
6179
reached: false,
6280
attributionId,
6381
};
6482
}
6583

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;
8887
}
8988
}

0 commit comments

Comments
 (0)