Skip to content

[usage] use credit balance for limit check #12997

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

Merged
merged 1 commit into from
Sep 15, 2022
Merged
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
12 changes: 7 additions & 5 deletions components/server/ee/src/billing/billing-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { User } from "@gitpod/gitpod-protocol";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { BillingServiceClient, BillingServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/billing.pb";
import { UsageServiceClient, UsageServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/usage.pb";
import { inject, injectable } from "inversify";
import { UserService } from "../../../src/user/user-service";
Expand All @@ -23,8 +22,6 @@ export class BillingService {
@inject(UserService) protected readonly userService: UserService;
@inject(UsageServiceDefinition.name)
protected readonly usageService: UsageServiceClient;
@inject(BillingServiceDefinition.name)
protected readonly billingService: BillingServiceClient;

async checkUsageLimitReached(user: User): Promise<UsageLimitReachedResult> {
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
Expand All @@ -43,8 +40,13 @@ export class BillingService {
};
}

const upcomingInvoice = await this.billingService.getUpcomingInvoice(attributionId);
const currentInvoiceCredits = upcomingInvoice.credits | 0;
const now = new Date();
const currentBalance = await this.usageService.listUsage({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also specify Page Size 1 or 0 to avoid getting any results in the list response, we don't need it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use from: now, to: now which does also make sure we don't fetch usage entries

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. We could change the usage implementation that if page size is 0, we don't even make the (list) query in the first place which would then be a bit faster.

Copy link
Member Author

@svenefftinge svenefftinge Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather introduce a new function. There's too much information in the response that wouldn't make sense (e.g. balanceAtStart vs balanceAtEnd)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your proposal makes sense once we need balance for a period but are not interested in the entries.

attributionId: AttributionId.render(attributionId),
from: now,
to: now,
});
const currentInvoiceCredits = currentBalance.creditBalanceAtEnd | 0;
if (currentInvoiceCredits >= (costCenter.spendingLimit || 0)) {
log.info({ userId: user.id }, "Usage limit reached", {
attributionId,
Expand Down