Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetUpcomingInvoice for BillingService (2) #12377

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions components/server/ee/src/billing/billing-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import { CostCenterDB } from "@gitpod/gitpod-db/lib";
import { User } from "@gitpod/gitpod-protocol";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { BillableSession, BillableSessionRequest, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { CachingUsageServiceClientProvider, UsageService } from "@gitpod/usage-api/lib/usage/v1/sugar";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { GetUpcomingInvoiceResponse } from "@gitpod/usage-api/lib/usage/v1/billing_pb";
import {
CachingUsageServiceClientProvider,
CachingBillingServiceClientProvider,
} from "@gitpod/usage-api/lib/usage/v1/sugar";
import { inject, injectable } from "inversify";
import { UserService } from "../../../src/user/user-service";

Expand All @@ -26,6 +28,8 @@ export class BillingService {
@inject(CostCenterDB) protected readonly costCenterDB: CostCenterDB;
@inject(CachingUsageServiceClientProvider)
protected readonly usageServiceClientProvider: CachingUsageServiceClientProvider;
@inject(CachingBillingServiceClientProvider)
protected readonly billingServiceClientProvider: CachingBillingServiceClientProvider;

async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> {
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
Expand All @@ -40,50 +44,39 @@ export class BillingService {
};
}

const allSessions = await this.listBilledUsage({
attributionId: AttributionId.render(attributionId),
startedTimeOrder: SortOrder.Descending,
});
const totalUsage = allSessions.map((s) => s.credits).reduce((a, b) => a + b, 0);
if (totalUsage >= costCenter.spendingLimit) {
const upcomingInvoice = await this.getUpcomingInvoice(attributionId);
const currentInvoiceCredits = upcomingInvoice.getCredits();
if (currentInvoiceCredits >= costCenter.spendingLimit) {
log.info({ userId: user.id }, "Spending limit reached", {
attributionId,
currentInvoiceCredits,
spendingLimit: costCenter.spendingLimit,
});
return {
reached: true,
attributionId,
};
} else if (totalUsage > costCenter.spendingLimit * 0.8) {
} else if (currentInvoiceCredits > costCenter.spendingLimit * 0.8) {
log.info({ userId: user.id }, "Spending limit almost reached", {
attributionId,
currentInvoiceCredits,
spendingLimit: costCenter.spendingLimit,
});
return {
reached: false,
almostReached: true,
attributionId,
};
}

return {
reached: false,
attributionId,
};
}

// TODO (gpl): Replace this with call to stripeService.getInvoice()
async listBilledUsage(req: BillableSessionRequest): Promise<BillableSession[]> {
const { attributionId, startedTimeOrder, from, to } = req;
let timestampFrom;
let timestampTo;

if (from) {
timestampFrom = Timestamp.fromDate(new Date(from));
}
if (to) {
timestampTo = Timestamp.fromDate(new Date(to));
}
const usageClient = this.usageServiceClientProvider.getDefault();
const response = await usageClient.listBilledUsage(
{},
attributionId,
startedTimeOrder as number,
timestampFrom,
timestampTo,
);
const sessions = response.getSessionsList().map((s) => UsageService.mapBilledSession(s));
return sessions;
async getUpcomingInvoice(attributionId: AttributionId): Promise<GetUpcomingInvoiceResponse> {
const response = await this.billingServiceClientProvider.getDefault().getUpcomingInvoice(attributionId);
return response;
}
}
234 changes: 122 additions & 112 deletions components/usage-api/go/v1/billing.pb.go

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions components/usage-api/go/v1/billing_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions components/usage-api/typescript/src/usage/v1/billing_grpc_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as usage_v1_usage_pb from "../../usage/v1/usage_pb";

interface IBillingServiceService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
updateInvoices: IBillingServiceService_IUpdateInvoices;
getLatestInvoice: IBillingServiceService_IGetLatestInvoice;
getUpcomingInvoice: IBillingServiceService_IGetUpcomingInvoice;
finalizeInvoice: IBillingServiceService_IFinalizeInvoice;
setBilledSession: IBillingServiceService_ISetBilledSession;
}
Expand All @@ -31,14 +31,14 @@ interface IBillingServiceService_IUpdateInvoices extends grpc.MethodDefinition<u
responseSerialize: grpc.serialize<usage_v1_billing_pb.UpdateInvoicesResponse>;
responseDeserialize: grpc.deserialize<usage_v1_billing_pb.UpdateInvoicesResponse>;
}
interface IBillingServiceService_IGetLatestInvoice extends grpc.MethodDefinition<usage_v1_billing_pb.GetLatestInvoiceRequest, usage_v1_billing_pb.GetLatestInvoiceResponse> {
path: "/usage.v1.BillingService/GetLatestInvoice";
interface IBillingServiceService_IGetUpcomingInvoice extends grpc.MethodDefinition<usage_v1_billing_pb.GetUpcomingInvoiceRequest, usage_v1_billing_pb.GetUpcomingInvoiceResponse> {
path: "/usage.v1.BillingService/GetUpcomingInvoice";
requestStream: false;
responseStream: false;
requestSerialize: grpc.serialize<usage_v1_billing_pb.GetLatestInvoiceRequest>;
requestDeserialize: grpc.deserialize<usage_v1_billing_pb.GetLatestInvoiceRequest>;
responseSerialize: grpc.serialize<usage_v1_billing_pb.GetLatestInvoiceResponse>;
responseDeserialize: grpc.deserialize<usage_v1_billing_pb.GetLatestInvoiceResponse>;
requestSerialize: grpc.serialize<usage_v1_billing_pb.GetUpcomingInvoiceRequest>;
requestDeserialize: grpc.deserialize<usage_v1_billing_pb.GetUpcomingInvoiceRequest>;
responseSerialize: grpc.serialize<usage_v1_billing_pb.GetUpcomingInvoiceResponse>;
responseDeserialize: grpc.deserialize<usage_v1_billing_pb.GetUpcomingInvoiceResponse>;
}
interface IBillingServiceService_IFinalizeInvoice extends grpc.MethodDefinition<usage_v1_billing_pb.FinalizeInvoiceRequest, usage_v1_billing_pb.FinalizeInvoiceResponse> {
path: "/usage.v1.BillingService/FinalizeInvoice";
Expand All @@ -63,7 +63,7 @@ export const BillingServiceService: IBillingServiceService;

export interface IBillingServiceServer extends grpc.UntypedServiceImplementation {
updateInvoices: grpc.handleUnaryCall<usage_v1_billing_pb.UpdateInvoicesRequest, usage_v1_billing_pb.UpdateInvoicesResponse>;
getLatestInvoice: grpc.handleUnaryCall<usage_v1_billing_pb.GetLatestInvoiceRequest, usage_v1_billing_pb.GetLatestInvoiceResponse>;
getUpcomingInvoice: grpc.handleUnaryCall<usage_v1_billing_pb.GetUpcomingInvoiceRequest, usage_v1_billing_pb.GetUpcomingInvoiceResponse>;
finalizeInvoice: grpc.handleUnaryCall<usage_v1_billing_pb.FinalizeInvoiceRequest, usage_v1_billing_pb.FinalizeInvoiceResponse>;
setBilledSession: grpc.handleUnaryCall<usage_v1_billing_pb.SetBilledSessionRequest, usage_v1_billing_pb.SetBilledSessionResponse>;
}
Expand All @@ -72,9 +72,9 @@ export interface IBillingServiceClient {
updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
Expand All @@ -88,9 +88,9 @@ export class BillingServiceClient extends grpc.Client implements IBillingService
public updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
public updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
public updateInvoices(request: usage_v1_billing_pb.UpdateInvoicesRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.UpdateInvoicesResponse) => void): grpc.ClientUnaryCall;
public getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
public getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
public getLatestInvoice(request: usage_v1_billing_pb.GetLatestInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetLatestInvoiceResponse) => void): grpc.ClientUnaryCall;
public getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
public getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
public getUpcomingInvoice(request: usage_v1_billing_pb.GetUpcomingInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.GetUpcomingInvoiceResponse) => void): grpc.ClientUnaryCall;
public finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
public finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
public finalizeInvoice(request: usage_v1_billing_pb.FinalizeInvoiceRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: usage_v1_billing_pb.FinalizeInvoiceResponse) => void): grpc.ClientUnaryCall;
Expand Down
Loading