Skip to content

Commit 29c0700

Browse files
authored
Revert "[server] Merge WorkspaceDeletionService into WorkspaceGC (#18410)"
This reverts commit 9acc185.
1 parent e99800d commit 29c0700

15 files changed

+284
-276
lines changed

components/dashboard/src/service/service-mock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { createServiceMock, Event, Project, Team, User } from "@gitpod/gitpod-protocol";
8+
import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode";
89

910
const u1: User = {
1011
id: "1234",
@@ -253,6 +254,9 @@ const gitpodServiceMock = createServiceMock({
253254
},
254255
};
255256
},
257+
getBillingModeForUser: async () => {
258+
return BillingMode.NONE;
259+
},
256260
});
257261

258262
export { gitpodServiceMock };

components/gitpod-protocol/src/gitpod-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
236236

237237
listUsage(req: ListUsageRequest): Promise<ListUsageResponse>;
238238

239+
getBillingModeForUser(): Promise<BillingMode>;
239240
getBillingModeForTeam(teamId: string): Promise<BillingMode>;
240241

241242
getLinkedInClientId(): Promise<string>;

components/server/src/auth/rate-limiter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ const defaultFunctions: FunctionsConfig = {
177177
getPriceInformation: { group: "default", points: 1 },
178178
listUsage: { group: "default", points: 1 },
179179
getBillingModeForTeam: { group: "default", points: 1 },
180+
getBillingModeForUser: { group: "default", points: 1 },
180181
getLinkedInClientId: { group: "default", points: 1 },
181182
connectWithLinkedIn: { group: "default", points: 1 },
182183

components/server/src/billing/billing-mode.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { inject, injectable } from "inversify";
88

9+
import { User } from "@gitpod/gitpod-protocol";
910
import { Config } from "../config";
1011
import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode";
1112
import { CostCenter_BillingStrategy } from "@gitpod/usage-api/lib/usage/v1/usage.pb";
@@ -21,7 +22,7 @@ export class BillingModes {
2122
@inject(UsageService) private readonly usageService: UsageService,
2223
) {}
2324

24-
public async getBillingMode(userId: string, organizationId: string): Promise<BillingMode> {
25+
public async getBillingMode(userId: string, organizationId: string, now: Date): Promise<BillingMode> {
2526
if (!this.config.enablePayment) {
2627
// Payment is not enabled. E.g. Dedicated
2728
return { mode: "none" };
@@ -32,11 +33,7 @@ export class BillingModes {
3233
return { mode: "usage-based", paid };
3334
}
3435

35-
/**
36-
* @deprecated use getBillingMode(userId, organizationId) instead
37-
* @returns
38-
*/
39-
async getBillingModeForUser(): Promise<BillingMode> {
36+
async getBillingModeForUser(user: User, now: Date): Promise<BillingMode> {
4037
if (!this.config.enablePayment) {
4138
// Payment is not enabled. E.g. Self-Hosted.
4239
return { mode: "none" };

components/server/src/billing/entitlement-service-ubp.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
import { TeamDB } from "@gitpod/gitpod-db/lib";
88
import {
9+
BillingTier,
10+
Team,
11+
User,
912
WorkspaceInstance,
1013
WorkspaceTimeoutDuration,
1114
WORKSPACE_TIMEOUT_DEFAULT_LONG,
1215
WORKSPACE_TIMEOUT_DEFAULT_SHORT,
1316
WORKSPACE_LIFETIME_LONG,
1417
WORKSPACE_LIFETIME_SHORT,
15-
User,
16-
BillingTier,
17-
Team,
1818
} from "@gitpod/gitpod-protocol";
1919
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
2020
import { inject, injectable } from "inversify";
@@ -38,10 +38,11 @@ export class EntitlementServiceUBP implements EntitlementService {
3838
async mayStartWorkspace(
3939
user: User,
4040
organizationId: string,
41+
date: Date,
4142
runningInstances: Promise<WorkspaceInstance[]>,
4243
): Promise<MayStartWorkspaceResult> {
4344
const hasHitParallelWorkspaceLimit = async (): Promise<HitParallelWorkspaceLimit | undefined> => {
44-
const max = await this.getMaxParallelWorkspaces(user.id, organizationId);
45+
const max = await this.getMaxParallelWorkspaces(user, date);
4546
const current = (await runningInstances).filter((i) => i.status.phase !== "preparing").length;
4647
if (current >= max) {
4748
return {
@@ -53,7 +54,7 @@ export class EntitlementServiceUBP implements EntitlementService {
5354
}
5455
};
5556
const [usageLimitReachedOnCostCenter, hitParallelWorkspaceLimit] = await Promise.all([
56-
this.checkUsageLimitReached(user.id, organizationId),
57+
this.checkUsageLimitReached(user, organizationId, date),
5758
hasHitParallelWorkspaceLimit(),
5859
]);
5960
return {
@@ -62,63 +63,69 @@ export class EntitlementServiceUBP implements EntitlementService {
6263
};
6364
}
6465

65-
private async checkUsageLimitReached(userId: string, organizationId: string): Promise<AttributionId | undefined> {
66-
const result = await this.usageService.checkUsageLimitReached(userId, organizationId);
66+
private async checkUsageLimitReached(
67+
user: User,
68+
organizationId: string,
69+
date: Date,
70+
): Promise<AttributionId | undefined> {
71+
const result = await this.usageService.checkUsageLimitReached(user.id, organizationId);
6772
if (result.reached) {
6873
return result.attributionId;
6974
}
7075
return undefined;
7176
}
7277

73-
private async getMaxParallelWorkspaces(userId: string, organizationId: string): Promise<number> {
74-
if (await this.hasPaidSubscription(userId, organizationId)) {
78+
private async getMaxParallelWorkspaces(user: User, date: Date): Promise<number> {
79+
if (await this.hasPaidSubscription(user, date)) {
7580
return MAX_PARALLEL_WORKSPACES_PAID;
7681
} else {
7782
return MAX_PARALLEL_WORKSPACES_FREE;
7883
}
7984
}
8085

81-
async maySetTimeout(userId: string, organizationId?: string): Promise<boolean> {
82-
return this.hasPaidSubscription(userId, organizationId);
86+
async maySetTimeout(user: User, date: Date): Promise<boolean> {
87+
return this.hasPaidSubscription(user, date);
8388
}
8489

85-
async getDefaultWorkspaceTimeout(userId: string, organizationId: string): Promise<WorkspaceTimeoutDuration> {
86-
if (await this.hasPaidSubscription(userId, organizationId)) {
90+
async getDefaultWorkspaceTimeout(user: User, date: Date): Promise<WorkspaceTimeoutDuration> {
91+
if (await this.hasPaidSubscription(user, date)) {
8792
return WORKSPACE_TIMEOUT_DEFAULT_LONG;
8893
} else {
8994
return WORKSPACE_TIMEOUT_DEFAULT_SHORT;
9095
}
9196
}
9297

93-
async getDefaultWorkspaceLifetime(userId: string, organizationId: string): Promise<WorkspaceTimeoutDuration> {
94-
if (await this.hasPaidSubscription(userId, organizationId)) {
98+
async getDefaultWorkspaceLifetime(user: User, date: Date): Promise<WorkspaceTimeoutDuration> {
99+
if (await this.hasPaidSubscription(user, date)) {
95100
return WORKSPACE_LIFETIME_LONG;
96101
} else {
97102
return WORKSPACE_LIFETIME_SHORT;
98103
}
99104
}
100105

106+
/**
107+
* DEPRECATED: With usage-based billing, users can choose exactly how many resources they want to get.
108+
* Thus, we no longer need to "force" extra resources via the `userGetsMoreResources` mechanism.
109+
*/
110+
async userGetsMoreResources(user: User, date: Date = new Date()): Promise<boolean> {
111+
return false;
112+
}
113+
101114
/**
102115
* Returns true if network connections should be limited
103116
* @param user
104117
*/
105-
async limitNetworkConnections(userId: string, organizationId: string): Promise<boolean> {
118+
async limitNetworkConnections(user: User, date: Date): Promise<boolean> {
106119
// gpl: Because with the current payment handling (pay-after-use) having a "paid" plan is not a good enough classifier for trushworthyness atm.
107120
// We're looking into improving this, but for the meantime we limit network connections for everybody to reduce the impact of abuse.
108121
return true;
109122
}
110123

111-
private async hasPaidSubscription(userId: string, organizationId?: string): Promise<boolean> {
112-
if (organizationId) {
113-
// This is the "stricter", more correct version: We only allow privileges on the Organization that is paying for it
114-
const { billingStrategy } = await this.usageService.getCostCenter(userId, organizationId);
115-
return billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE;
116-
}
117-
// This is the old behavior, stemming from our transition to PAYF, where our API did-/doesn't pass organizationId, yet
124+
private async hasPaidSubscription(user: User, date: Date): Promise<boolean> {
118125
// Member of paid team?
119-
const teams = await this.teamDB.findTeamsByUser(userId);
126+
const teams = await this.teamDB.findTeamsByUser(user.id);
120127
const isTeamSubscribedPromises = teams.map(async (team: Team) => {
121-
const { billingStrategy } = await this.usageService.getCostCenter(userId, team.id);
128+
const { billingStrategy } = await this.usageService.getCostCenter(user.id, team.id);
122129
return billingStrategy === CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE;
123130
});
124131
// Return the first truthy promise, or false if all the promises were falsy.
@@ -140,8 +147,8 @@ export class EntitlementServiceUBP implements EntitlementService {
140147
});
141148
}
142149

143-
async getBillingTier(userId: string, organizationId: string): Promise<BillingTier> {
144-
const hasPaidPlan = await this.hasPaidSubscription(userId, organizationId);
150+
async getBillingTier(user: User): Promise<BillingTier> {
151+
const hasPaidPlan = await this.hasPaidSubscription(user, new Date());
145152
return hasPaidPlan ? "paid" : "free";
146153
}
147154
}

0 commit comments

Comments
 (0)