Skip to content

Commit 10389ce

Browse files
committed
wip GetLatestInvoice
1 parent b8fd596 commit 10389ce

File tree

11 files changed

+230
-87
lines changed

11 files changed

+230
-87
lines changed

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
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";
10+
import { BillableSession, BillableSessionRequest } from "@gitpod/gitpod-protocol/lib/usage";
1111
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
12-
import { CachingUsageServiceClientProvider, UsageService } from "@gitpod/usage-api/lib/usage/v1/sugar";
12+
import { GetLatestInvoiceResponse } 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";
1318
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
1419
import { inject, injectable } from "inversify";
1520
import { UserService } from "../../../src/user/user-service";
@@ -26,6 +31,8 @@ export class BillingService {
2631
@inject(CostCenterDB) protected readonly costCenterDB: CostCenterDB;
2732
@inject(CachingUsageServiceClientProvider)
2833
protected readonly usageServiceClientProvider: CachingUsageServiceClientProvider;
34+
@inject(CachingBillingServiceClientProvider)
35+
protected readonly billingServiceClientProvider: CachingBillingServiceClientProvider;
2936

3037
async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> {
3138
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
@@ -40,23 +47,27 @@ export class BillingService {
4047
};
4148
}
4249

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 latestInvoice = await this.getLatestInvoice(attributionId.teamId);
52+
const currentUsage = latestInvoice.getCredits();
53+
if (currentUsage >= costCenter.spendingLimit) {
54+
return {
55+
reached: true,
56+
attributionId,
57+
};
58+
} else if (currentUsage > costCenter.spendingLimit * 0.8) {
59+
return {
60+
reached: false,
61+
almostReached: true,
62+
attributionId,
63+
};
64+
}
65+
}
66+
67+
if (attributionId.kind === "user") {
68+
// TODO
5969
}
70+
6071
return {
6172
reached: false,
6273
attributionId,
@@ -86,4 +97,9 @@ export class BillingService {
8697
const sessions = response.getSessionsList().map((s) => UsageService.mapBilledSession(s));
8798
return sessions;
8899
}
100+
101+
async getLatestInvoice(teamId: string): Promise<GetLatestInvoiceResponse> {
102+
const response = await this.billingServiceClientProvider.getDefault().getLatestInvoice(teamId);
103+
return response;
104+
}
89105
}

components/usage-api/go/v1/billing.pb.go

Lines changed: 65 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/usage-api/typescript/src/usage/v1/billing_pb.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export class GetLatestInvoiceResponse extends jspb.Message {
110110
setCurrency(value: string): GetLatestInvoiceResponse;
111111
getAmount(): number;
112112
setAmount(value: number): GetLatestInvoiceResponse;
113+
getCredits(): number;
114+
setCredits(value: number): GetLatestInvoiceResponse;
113115

114116
serializeBinary(): Uint8Array;
115117
toObject(includeInstance?: boolean): GetLatestInvoiceResponse.AsObject;
@@ -126,6 +128,7 @@ export namespace GetLatestInvoiceResponse {
126128
invoiceId: string,
127129
currency: string,
128130
amount: number,
131+
credits: number,
129132
}
130133
}
131134

components/usage-api/typescript/src/usage/v1/billing_pb.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,8 @@ proto.usage.v1.GetLatestInvoiceResponse.toObject = function(includeInstance, msg
822822
var f, obj = {
823823
invoiceId: jspb.Message.getFieldWithDefault(msg, 1, ""),
824824
currency: jspb.Message.getFieldWithDefault(msg, 2, ""),
825-
amount: jspb.Message.getFloatingPointFieldWithDefault(msg, 3, 0.0)
825+
amount: jspb.Message.getFloatingPointFieldWithDefault(msg, 3, 0.0),
826+
credits: jspb.Message.getFieldWithDefault(msg, 4, 0)
826827
};
827828

828829
if (includeInstance) {
@@ -871,6 +872,10 @@ proto.usage.v1.GetLatestInvoiceResponse.deserializeBinaryFromReader = function(m
871872
var value = /** @type {number} */ (reader.readDouble());
872873
msg.setAmount(value);
873874
break;
875+
case 4:
876+
var value = /** @type {number} */ (reader.readInt64());
877+
msg.setCredits(value);
878+
break;
874879
default:
875880
reader.skipField();
876881
break;
@@ -921,6 +926,13 @@ proto.usage.v1.GetLatestInvoiceResponse.serializeBinaryToWriter = function(messa
921926
f
922927
);
923928
}
929+
f = message.getCredits();
930+
if (f !== 0) {
931+
writer.writeInt64(
932+
4,
933+
f
934+
);
935+
}
924936
};
925937

926938

@@ -978,6 +990,24 @@ proto.usage.v1.GetLatestInvoiceResponse.prototype.setAmount = function(value) {
978990
};
979991

980992

993+
/**
994+
* optional int64 credits = 4;
995+
* @return {number}
996+
*/
997+
proto.usage.v1.GetLatestInvoiceResponse.prototype.getCredits = function() {
998+
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 4, 0));
999+
};
1000+
1001+
1002+
/**
1003+
* @param {number} value
1004+
* @return {!proto.usage.v1.GetLatestInvoiceResponse} returns this
1005+
*/
1006+
proto.usage.v1.GetLatestInvoiceResponse.prototype.setCredits = function(value) {
1007+
return jspb.Message.setProto3IntField(this, 4, value);
1008+
};
1009+
1010+
9811011

9821012

9831013

components/usage-api/typescript/src/usage/v1/sugar.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
1010
import * as opentracing from "opentracing";
1111
import { Metadata } from "@grpc/grpc-js";
1212
import { BilledSession, ListBilledUsageRequest, ListBilledUsageResponse } from "./usage_pb";
13-
import { SetBilledSessionRequest, SetBilledSessionResponse, System } from "./billing_pb";
13+
import {
14+
GetLatestInvoiceRequest,
15+
GetLatestInvoiceResponse,
16+
SetBilledSessionRequest,
17+
SetBilledSessionResponse,
18+
System,
19+
} from "./billing_pb";
1420
import { injectable, inject, optional } from "inversify";
1521
import { createClientCallMetricsInterceptor, IClientCallMetrics } from "@gitpod/gitpod-protocol/lib/util/grpc";
1622
import * as grpc from "@grpc/grpc-js";
@@ -254,4 +260,20 @@ export class PromisifiedBillingServiceClient {
254260
interceptors: this.interceptor,
255261
};
256262
}
263+
264+
public async getLatestInvoice(teamId: string) {
265+
const req = new GetLatestInvoiceRequest();
266+
req.setTeamId(teamId);
267+
268+
const response = await new Promise<GetLatestInvoiceResponse>((resolve, reject) => {
269+
this.client.getLatestInvoice(req, (err: grpc.ServiceError | null, response: GetLatestInvoiceResponse) => {
270+
if (err) {
271+
reject(err);
272+
return;
273+
}
274+
resolve(response);
275+
});
276+
});
277+
return response;
278+
}
257279
}

components/usage-api/usage/v1/billing.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ message GetLatestInvoiceResponse {
4545
string invoice_id = 1;
4646
string currency = 2;
4747
double amount = 3;
48+
int64 credits = 4;
4849
}
4950

5051
message FinalizeInvoiceRequest {

0 commit comments

Comments
 (0)