-
Notifications
You must be signed in to change notification settings - Fork 1.3k
EntitlementService Usage-Based Pricing #11936
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the Gitpod Enterprise Source Code License, | ||
* See License.enterprise.txt in the project root folder. | ||
*/ | ||
|
||
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 { inject, injectable } from "inversify"; | ||
import { UserService } from "../../../src/user/user-service"; | ||
|
||
export interface SpendingLimitReachedResult { | ||
reached: boolean; | ||
almostReached?: boolean; | ||
attributionId: AttributionId; | ||
} | ||
|
||
@injectable() | ||
export class BillingService { | ||
@inject(UserService) protected readonly userService: UserService; | ||
@inject(CostCenterDB) protected readonly costCenterDB: CostCenterDB; | ||
@inject(CachingUsageServiceClientProvider) | ||
protected readonly usageServiceClientProvider: CachingUsageServiceClientProvider; | ||
|
||
async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> { | ||
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user); | ||
const costCenter = !!attributionId && (await this.costCenterDB.findById(AttributionId.render(attributionId))); | ||
if (!costCenter) { | ||
const err = new Error("No CostCenter found"); | ||
log.error({ userId: user.id }, err.message, err, { attributionId }); | ||
throw err; | ||
} | ||
|
||
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) { | ||
return { | ||
reached: true, | ||
attributionId, | ||
}; | ||
} else if (totalUsage > costCenter.spendingLimit * 0.8) { | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
components/server/ee/src/billing/entitlement-service-ubp.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { UserDB } from "@gitpod/gitpod-db/lib"; | ||
import { | ||
User, | ||
WorkspaceInstance, | ||
WorkspaceTimeoutDuration, | ||
WORKSPACE_TIMEOUT_DEFAULT_LONG, | ||
WORKSPACE_TIMEOUT_DEFAULT_SHORT, | ||
} from "@gitpod/gitpod-protocol"; | ||
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution"; | ||
import { inject, injectable } from "inversify"; | ||
import { | ||
EntitlementService, | ||
HitParallelWorkspaceLimit, | ||
MayStartWorkspaceResult, | ||
} from "../../../src/billing/entitlement-service"; | ||
import { Config } from "../../../src/config"; | ||
import { BillingModes } from "./billing-mode"; | ||
import { BillingService } from "./billing-service"; | ||
|
||
const MAX_PARALLEL_WORKSPACES_FREE = 4; | ||
const MAX_PARALLEL_WORKSPACES_PAID = 16; | ||
|
||
/** | ||
* EntitlementService implementation for Usage-Based Pricing (UBP) | ||
*/ | ||
@injectable() | ||
export class EntitlementServiceUBP implements EntitlementService { | ||
@inject(Config) protected readonly config: Config; | ||
@inject(UserDB) protected readonly userDb: UserDB; | ||
@inject(BillingModes) protected readonly billingModes: BillingModes; | ||
@inject(BillingService) protected readonly billingService: BillingService; | ||
|
||
async mayStartWorkspace( | ||
user: User, | ||
date: Date, | ||
runningInstances: Promise<WorkspaceInstance[]>, | ||
): Promise<MayStartWorkspaceResult> { | ||
const hasHitParallelWorkspaceLimit = async (): Promise<HitParallelWorkspaceLimit | undefined> => { | ||
const max = await this.getMaxParallelWorkspaces(user, date); | ||
const current = (await runningInstances).filter((i) => i.status.phase !== "preparing").length; | ||
if (current >= max) { | ||
return { | ||
current, | ||
max, | ||
}; | ||
} else { | ||
return undefined; | ||
} | ||
}; | ||
const [spendingLimitReachedOnCostCenter, hitParallelWorkspaceLimit] = await Promise.all([ | ||
this.checkSpendingLimitReached(user, date), | ||
hasHitParallelWorkspaceLimit(), | ||
]); | ||
const result = !spendingLimitReachedOnCostCenter && !hitParallelWorkspaceLimit; | ||
return { | ||
mayStart: result, | ||
spendingLimitReachedOnCostCenter, | ||
hitParallelWorkspaceLimit, | ||
}; | ||
} | ||
|
||
protected async checkSpendingLimitReached(user: User, date: Date): Promise<AttributionId | undefined> { | ||
const result = await this.billingService.checkSpendingLimitReached(user); | ||
if (result.reached) { | ||
return result.attributionId; | ||
} | ||
return undefined; | ||
} | ||
|
||
protected async getMaxParallelWorkspaces(user: User, date: Date): Promise<number> { | ||
if (await this.hasPaidSubscription(user, date)) { | ||
return MAX_PARALLEL_WORKSPACES_PAID; | ||
} else { | ||
return MAX_PARALLEL_WORKSPACES_FREE; | ||
} | ||
} | ||
|
||
async maySetTimeout(user: User, date: Date): Promise<boolean> { | ||
return this.hasPaidSubscription(user, date); | ||
} | ||
|
||
async getDefaultWorkspaceTimeout(user: User, date: Date): Promise<WorkspaceTimeoutDuration> { | ||
if (await this.hasPaidSubscription(user, date)) { | ||
return WORKSPACE_TIMEOUT_DEFAULT_LONG; | ||
} else { | ||
return WORKSPACE_TIMEOUT_DEFAULT_SHORT; | ||
} | ||
} | ||
|
||
async userGetsMoreResources(user: User, date: Date = new Date()): Promise<boolean> { | ||
return this.hasPaidSubscription(user, date); | ||
} | ||
|
||
protected async hasPaidSubscription(user: User, date: Date): Promise<boolean> { | ||
// TODO(gpl) UBP personal: implement! | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: As currently implemented, it reads like everyone on Usage-Based gets XL workspaces.