-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Usage-based] Attribute workspace instances to a Team on start #10574
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 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 } from "./helper/helper"; | ||
|
||
const TABLE_NAME = "d_b_workspace_instance"; | ||
const COLUMN_NAME = "attributedTeamId"; | ||
|
||
export class WorkspaceInstanceAttributedTeam1654847406624 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} char(36) NOT NULL DEFAULT '', ALGORITHM=INPLACE, LOCK=NONE`, | ||
); | ||
} | ||
} | ||
|
||
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`, | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import { | |
WORKSPACE_TIMEOUT_EXTENDED, | ||
WORKSPACE_TIMEOUT_EXTENDED_ALT, | ||
} from "@gitpod/gitpod-protocol"; | ||
import { TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib"; | ||
import { ProjectDB, TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib"; | ||
import { HostContextProvider } from "../auth/host-context-provider"; | ||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { Config } from "../config"; | ||
|
@@ -63,6 +63,7 @@ export class UserService { | |
@inject(Config) protected readonly config: Config; | ||
@inject(TermsAcceptanceDB) protected readonly termsAcceptanceDb: TermsAcceptanceDB; | ||
@inject(TermsProvider) protected readonly termsProvider: TermsProvider; | ||
@inject(ProjectDB) protected readonly projectDb: ProjectDB; | ||
|
||
/** | ||
* Takes strings in the form of <authHost>/<authName> and returns the matching User | ||
|
@@ -205,6 +206,28 @@ export class UserService { | |
return false; | ||
} | ||
|
||
/** | ||
* Identifies the team 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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placing this method in My reason for placing it here was to allow an extension point where ee/UserService could detect if payment is enabled, and use a different logic (i.e. attribute all usage to a "cost center" team, instead of the project's team). I don't have any opinion on this -- feedback most appreciated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not strongly opinionated here either but your reasoning here makes sense; the subject of cost center vs team has already come up in discussions. |
||
if (!projectId) { | ||
// No project -- attribute to the user. | ||
return undefined; | ||
} | ||
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; | ||
} | ||
// Attribute workspace usage to the team that currently owns this project. | ||
return project.teamId; | ||
} | ||
|
||
/** | ||
* This might throw `AuthException`s. | ||
* | ||
|
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.
Rather than leaving this unset in this case, should we explicitly fill in the ownerId?
Uh oh!
There was an error while loading. Please reload this page.
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.
I believe leaving it unset for user attribution makes more sense:
teamId
(the billing is either delegated to a specific team, or it falls back to the natural owner -- delegating to a different specific user would not make sense, right? 🤔)userId
makes the design more complex (need a second columnattributedUserId
, or maybe a prefix to the ID e.g.team-1324
vsuser-1234
)Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, that's my understanding too.