Skip to content

[db][protocol] Implement a CostCenter entity to attribute workspace usage to #10788

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
Jul 20, 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
5 changes: 5 additions & 0 deletions components/gitpod-db/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import { TypeORMBlockedRepositoryDBImpl } from "./typeorm/blocked-repository-db-
import { BlockedRepositoryDB } from "./blocked-repository-db";
import { WebhookEventDB } from "./webhook-event-db";
import { WebhookEventDBImpl } from "./typeorm/webhook-event-db-impl";
import { CostCenterDB } from "./cost-center-db";
import { CostCenterDBImpl } from "./typeorm/cost-center-db-impl";

// THE DB container module that contains all DB implementations
export const dbContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
Expand Down Expand Up @@ -144,6 +146,9 @@ export const dbContainerModule = new ContainerModule((bind, unbind, isBound, reb
bind(WebhookEventDBImpl).toSelf().inSingletonScope();
bind(WebhookEventDB).toService(WebhookEventDBImpl);

bind(CostCenterDBImpl).toSelf().inSingletonScope();
bind(CostCenterDB).toService(CostCenterDBImpl);

// com concerns
bind(AccountingDB).to(TypeORMAccountingDBImpl).inSingletonScope();
bind(TransactionalAccountingDBFactory).toFactory((ctx) => {
Expand Down
13 changes: 13 additions & 0 deletions components/gitpod-db/src/cost-center-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { CostCenter } from "@gitpod/gitpod-protocol";

export const CostCenterDB = Symbol("CostCenterDB");
export interface CostCenterDB {
storeEntry(ts: CostCenter): Promise<void>;
findById(id: string): Promise<CostCenter | undefined>;
}
1 change: 1 addition & 0 deletions components/gitpod-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export * from "./project-db";
export * from "./team-db";
export * from "./installation-admin-db";
export * from "./webhook-event-db";
export * from "./cost-center-db";
6 changes: 6 additions & 0 deletions components/gitpod-db/src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ export class GitpodTableDescriptionProvider implements TableDescriptionProvider
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
{
name: "d_b_cost_center",
primaryKeys: ["id"],
deletionColumn: "deleted",
timeColumn: "_lastModified",
},
];

public getSortedTables(): TableDescription[] {
Expand Down
37 changes: 37 additions & 0 deletions components/gitpod-db/src/typeorm/cost-center-db-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { injectable, inject } from "inversify";
import { Repository } from "typeorm";

import { CostCenter } from "@gitpod/gitpod-protocol";

import { CostCenterDB } from "../cost-center-db";
import { DBCostCenter } from "./entity/db-cost-center";
import { TypeORM } from "./typeorm";

@injectable()
export class CostCenterDBImpl implements CostCenterDB {
@inject(TypeORM) protected readonly typeORM: TypeORM;

protected async getEntityManager() {
return (await this.typeORM.getConnection()).manager;
}

protected async getRepo(): Promise<Repository<DBCostCenter>> {
return (await this.getEntityManager()).getRepository(DBCostCenter);
}

async storeEntry(ts: CostCenter): Promise<void> {
const repo = await this.getRepo();
await repo.save(ts);
}

async findById(id: string): Promise<CostCenter | undefined> {
const repo = await this.getRepo();
return repo.findOne(id);
}
}
22 changes: 22 additions & 0 deletions components/gitpod-db/src/typeorm/entity/db-cost-center.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the Gitpod Enterprise Source Code License,
* See License.enterprise.txt in the project root folder.
*/

import { Entity, Column, PrimaryColumn } from "typeorm";
import { CostCenter } from "@gitpod/gitpod-protocol";

@Entity()
// on DB but not Typeorm: @Index("ind_lastModified", ["_lastModified"]) // DBSync
export class DBCostCenter implements CostCenter {
@PrimaryColumn()
id: string;

@Column()
spendingLimit: number;

// This column triggers the db-sync deletion mechanism. It's not intended for public consumption.
@Column()
deleted: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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";

export class CostCenter1658241900000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
"CREATE TABLE IF NOT EXISTS `d_b_cost_center` (`id` char(255) NOT NULL, `spendingLimit` int(11) NOT NULL DEFAULT '0', `deleted` tinyint(4) NOT NULL DEFAULT '0', `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (`id`), KEY `ind_dbsync` (`_lastModified`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
8 changes: 8 additions & 0 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,3 +1481,11 @@ export interface Terms {
readonly content: string;
readonly formElements?: object;
}

export interface CostCenter {
readonly id: string;
/**
* Unit: credits
*/
spendingLimit: number;
}
3 changes: 2 additions & 1 deletion components/server/src/user/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
WORKSPACE_TIMEOUT_EXTENDED,
WORKSPACE_TIMEOUT_EXTENDED_ALT,
} from "@gitpod/gitpod-protocol";
import { ProjectDB, TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib";
import { CostCenterDB, 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";
Expand Down Expand Up @@ -65,6 +65,7 @@ export class UserService {
@inject(TermsAcceptanceDB) protected readonly termsAcceptanceDb: TermsAcceptanceDB;
@inject(TermsProvider) protected readonly termsProvider: TermsProvider;
@inject(ProjectDB) protected readonly projectDb: ProjectDB;
@inject(CostCenterDB) protected readonly costCenterDb: CostCenterDB;

/**
* Takes strings in the form of <authHost>/<authName> and returns the matching User
Expand Down