Skip to content

Commit b1ac08a

Browse files
committed
[server] Attribute workspace usage to either the project's team, or default to the workspace owner
1 parent 18923ec commit b1ac08a

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

components/server/src/user/user-service.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
WORKSPACE_TIMEOUT_EXTENDED,
1717
WORKSPACE_TIMEOUT_EXTENDED_ALT,
1818
} from "@gitpod/gitpod-protocol";
19-
import { TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib";
19+
import { ProjectDB, TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib";
2020
import { HostContextProvider } from "../auth/host-context-provider";
2121
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
2222
import { Config } from "../config";
@@ -63,6 +63,7 @@ export class UserService {
6363
@inject(Config) protected readonly config: Config;
6464
@inject(TermsAcceptanceDB) protected readonly termsAcceptanceDb: TermsAcceptanceDB;
6565
@inject(TermsProvider) protected readonly termsProvider: TermsProvider;
66+
@inject(ProjectDB) protected readonly projectDb: ProjectDB;
6667

6768
/**
6869
* Takes strings in the form of <authHost>/<authName> and returns the matching User
@@ -205,6 +206,28 @@ export class UserService {
205206
return false;
206207
}
207208

209+
/**
210+
* Identifies the team to which a workspace instance's running time should be attributed to
211+
* (e.g. for usage analytics or billing purposes).
212+
* If no specific team is identified, the usage will be attributed to the user instead (default).
213+
*
214+
* @param user
215+
* @param projectId
216+
*/
217+
async getWorkspaceUsageAttributionTeamId(user: User, projectId?: string): Promise<string | undefined> {
218+
if (!projectId) {
219+
// No project -- attribute to the user.
220+
return undefined;
221+
}
222+
const project = await this.projectDb.findProjectById(projectId);
223+
if (!project?.teamId) {
224+
// The project doesn't exist, or it isn't owned by a team -- attribute to the user.
225+
return undefined;
226+
}
227+
// Attribute workspace usage to the team that currently owns this project.
228+
return project.teamId;
229+
}
230+
208231
/**
209232
* This might throw `AuthException`s.
210233
*

components/server/src/workspace/workspace-starter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ export class WorkspaceStarter {
633633
delete ideConfig.ideOptions.options["code-latest"];
634634
delete ideConfig.ideOptions.options["code-desktop-insiders"];
635635

636-
const migratted = migrationIDESettings(user);
637-
if (user.additionalData?.ideSettings && migratted) {
638-
user.additionalData.ideSettings = migratted;
636+
const migrated = migrationIDESettings(user);
637+
if (user.additionalData?.ideSettings && migrated) {
638+
user.additionalData.ideSettings = migrated;
639639
}
640640

641641
const ideChoice = user.additionalData?.ideSettings?.defaultIde;
@@ -709,6 +709,8 @@ export class WorkspaceStarter {
709709
configuration.featureFlags = featureFlags;
710710
}
711711

712+
const attributedTeamId = await this.userService.getWorkspaceUsageAttributionTeamId(user, workspace.projectId);
713+
712714
const now = new Date().toISOString();
713715
const instance: WorkspaceInstance = {
714716
id: uuidv4(),
@@ -722,6 +724,7 @@ export class WorkspaceStarter {
722724
phase: "preparing",
723725
},
724726
configuration,
727+
attributedTeamId,
725728
};
726729
if (WithReferrerContext.is(workspace.context)) {
727730
this.analytics.track({

0 commit comments

Comments
 (0)