Skip to content

Refactor workspace instance attributedTeamId to an explicit, not-team-specific usageAttributionId #10868

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 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ export class DBWorkspaceInstance implements WorkspaceInstance {
default: "",
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED,
})
attributedTeamId?: string;
usageAttributionId?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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 { MigrationInterface, QueryRunner } from "typeorm";
import { columnExists, indexExists } from "./helper/helper";

const TABLE_NAME = "d_b_workspace_instance";
const COLUMN_NAME = "usageAttributionId";
const INDEX_NAME = "ind_usageAttributionId";

export class WorkspaceInstanceUsageAttributionId1655988518555 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME} ADD COLUMN ${COLUMN_NAME} varchar(60) NOT NULL DEFAULT '', ALGORITHM=INPLACE, LOCK=NONE`,
);
}
if (!(await indexExists(queryRunner, TABLE_NAME, INDEX_NAME))) {
await queryRunner.query(`CREATE INDEX ${INDEX_NAME} ON ${TABLE_NAME} (${COLUMN_NAME})`);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
if (await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME)) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME} DROP COLUMN ${COLUMN_NAME}, ALGORITHM=INPLACE, LOCK=NONE`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

);
}
}
}
8 changes: 4 additions & 4 deletions components/gitpod-db/src/workspace-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
usageAttributionId: undefined,
};
readonly wsi2: WorkspaceInstance = {
workspaceId: this.ws.id,
Expand All @@ -89,7 +89,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
usageAttributionId: undefined,
};
readonly ws2: Workspace = {
id: "2",
Expand Down Expand Up @@ -127,7 +127,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
usageAttributionId: undefined,
};

readonly ws3: Workspace = {
Expand Down Expand Up @@ -165,7 +165,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
usageAttributionId: undefined,
};

async before() {
Expand Down
5 changes: 2 additions & 3 deletions components/gitpod-protocol/src/workspace-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ export interface WorkspaceInstance {
workspaceClass?: string;

/**
* Identifies the team to which this instance's runtime should be attributed to
* Identifies the team or user to which this instance's runtime should be attributed to
* (e.g. for usage analytics or billing purposes).
* If unset, the usage should be attributed to the workspace's owner (ws.ownerId).
*/
attributedTeamId?: string;
usageAttributionId?: string;
}

// WorkspaceInstanceStatus describes the current state of a workspace instance
Expand Down
11 changes: 5 additions & 6 deletions components/server/src/user/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,24 @@ export class UserService {
}

/**
* Identifies the team to which a workspace instance's running time should be attributed to
* Identifies the team or user to which a workspace instance's running time should be attributed to
* (e.g. for usage analytics or billing purposes).
* If no specific team is identified, the usage will be attributed to the user instead (default).
*
* @param user
* @param projectId
*/
async getWorkspaceUsageAttributionTeamId(user: User, projectId?: string): Promise<string | undefined> {
async getWorkspaceUsageAttributionId(user: User, projectId?: string): Promise<string | undefined> {
if (!projectId) {
// No project -- attribute to the user.
return undefined;
return `user:${user.id}`;
}
const project = await this.projectDb.findProjectById(projectId);
if (!project?.teamId) {
// The project doesn't exist, or it isn't owned by a team -- attribute to the user.
return undefined;
return `user:${user.id}`;
}
// Attribute workspace usage to the team that currently owns this project.
return project.teamId;
return `team:${project.teamId}`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ export class WorkspaceStarter {
configuration.featureFlags = featureFlags;
}

const attributedTeamId = await this.userService.getWorkspaceUsageAttributionTeamId(user, workspace.projectId);
const usageAttributionId = await this.userService.getWorkspaceUsageAttributionId(user, workspace.projectId);

const now = new Date().toISOString();
const instance: WorkspaceInstance = {
Expand All @@ -737,7 +737,7 @@ export class WorkspaceStarter {
phase: "preparing",
},
configuration,
attributedTeamId,
usageAttributionId,
};
if (WithReferrerContext.is(workspace.context)) {
this.analytics.track({
Expand Down