From 0023ced4a09f492795287588ac12c8fd7070c26c Mon Sep 17 00:00:00 2001 From: Sven Efftinge Date: Thu, 15 Sep 2022 11:46:28 +0000 Subject: [PATCH] [usage] use credit balance for limit check --- components/server/ee/src/billing/billing-service.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/components/server/ee/src/billing/billing-service.ts b/components/server/ee/src/billing/billing-service.ts index de98f317c83c2a..1b417c05aa4ca0 100644 --- a/components/server/ee/src/billing/billing-service.ts +++ b/components/server/ee/src/billing/billing-service.ts @@ -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"; @@ -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 { const attributionId = await this.userService.getWorkspaceUsageAttributionId(user); @@ -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({ + 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,