Skip to content

Commit 402894b

Browse files
jankeromnesroboquat
authored andcommitted
[server] Attribute workspace usage to either the project's team, or default to the workspace owner
1 parent 71813fe commit 402894b

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
@@ -646,9 +646,9 @@ export class WorkspaceStarter {
646646
delete ideConfig.ideOptions.options["code-latest"];
647647
delete ideConfig.ideOptions.options["code-desktop-insiders"];
648648

649-
const migratted = migrationIDESettings(user);
650-
if (user.additionalData?.ideSettings && migratted) {
651-
user.additionalData.ideSettings = migratted;
649+
const migrated = migrationIDESettings(user);
650+
if (user.additionalData?.ideSettings && migrated) {
651+
user.additionalData.ideSettings = migrated;
652652
}
653653

654654
const ideChoice = user.additionalData?.ideSettings?.defaultIde;
@@ -722,6 +722,8 @@ export class WorkspaceStarter {
722722
configuration.featureFlags = featureFlags;
723723
}
724724

725+
const attributedTeamId = await this.userService.getWorkspaceUsageAttributionTeamId(user, workspace.projectId);
726+
725727
const now = new Date().toISOString();
726728
const instance: WorkspaceInstance = {
727729
id: uuidv4(),
@@ -735,6 +737,7 @@ export class WorkspaceStarter {
735737
phase: "preparing",
736738
},
737739
configuration,
740+
attributedTeamId,
738741
};
739742
if (WithReferrerContext.is(workspace.context)) {
740743
this.analytics.track({

0 commit comments

Comments
 (0)