Skip to content

Commit

Permalink
feat(platform-server): usage packs and project usage limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyi.levi committed Nov 22, 2022
1 parent cf05254 commit 2277911
Show file tree
Hide file tree
Showing 53 changed files with 1,429 additions and 89 deletions.
18 changes: 8 additions & 10 deletions ava.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ module.exports = {
function startServer() {
const { execSync, fork } = require('child_process')
const path = require('path')
const MYSQL_DB = 'perfsee_testing'

console.info('initializing testing database...')
execSync(
'yarn typeorm query "CREATE DATABASE IF NOT EXISTS perfsee_testing;" && yarn typeorm migration:run && yarn typeorm query "SET GLOBAL FOREIGN_KEY_CHECKS = 0;"',
{
stdio: 'inherit',
env: {
...process.env,
MYSQL_DB: 'perfsee_testing',
},
execSync(`yarn typeorm migration:run && yarn typeorm query "SET GLOBAL FOREIGN_KEY_CHECKS = 0;"`, {
stdio: 'inherit',
env: {
...process.env,
MYSQL_DB,
},
)
})

const server = fork(`${path.resolve(__dirname, 'tools/e2e-server.entry.js')}`, {
stdio: 'inherit',
Expand All @@ -43,7 +41,7 @@ function startServer() {
TS_NODE_PROJECT: './tsconfigs/tsconfig.cjs.json',
NODE_ENV: 'test',
PERFSEE_SERVER_PORT: 3001,
MYSQL_DB: 'perfsee_testing',
MYSQL_DB,
},
})

Expand Down
39 changes: 39 additions & 0 deletions db/migrations/1668670838492-foreign-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class foreignKeys1668670838492 implements MigrationInterface {
name = 'foreignKeys1668670838492'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE \`job\`
ADD CONSTRAINT \`FK_ca5fac3e39d6e93df14903d1072\` FOREIGN KEY (\`runner_id\`) REFERENCES \`runner\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION
`)
await queryRunner.query(`
ALTER TABLE \`page_with_competitor\`
ADD CONSTRAINT \`FK_b0f452aef10b0019e00eaa927e5\` FOREIGN KEY (\`competitor_id\`) REFERENCES \`page\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION
`)
await queryRunner.query(`
ALTER TABLE \`source_issue\`
ADD CONSTRAINT \`FK_4835a8bf4d0ae06e03541edfa89\` FOREIGN KEY (\`snapshot_report_id\`) REFERENCES \`snapshot_report\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION
`)
await queryRunner.query(`
ALTER TABLE \`user_connected_account\`
ADD CONSTRAINT \`FK_e956657dfdd875f5bc44c412c17\` FOREIGN KEY (\`user_id\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE \`user_connected_account\` DROP FOREIGN KEY \`FK_e956657dfdd875f5bc44c412c17\`
`)
await queryRunner.query(`
ALTER TABLE \`source_issue\` DROP FOREIGN KEY \`FK_4835a8bf4d0ae06e03541edfa89\`
`)
await queryRunner.query(`
ALTER TABLE \`page_with_competitor\` DROP FOREIGN KEY \`FK_b0f452aef10b0019e00eaa927e5\`
`)
await queryRunner.query(`
ALTER TABLE \`job\` DROP FOREIGN KEY \`FK_ca5fac3e39d6e93df14903d1072\`
`)
}
}
110 changes: 110 additions & 0 deletions db/migrations/1668670929409-project-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class projectUsage1668670929409 implements MigrationInterface {
name = 'projectUsage1668670929409'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE \`usage_pack\` (
\`id\` int NOT NULL AUTO_INCREMENT,
\`name\` varchar(255) NOT NULL,
\`desc\` varchar(255) NOT NULL,
\`is_public\` tinyint NOT NULL DEFAULT 0,
\`is_default\` tinyint NOT NULL DEFAULT 0,
\`job_count_monthly\` int NOT NULL,
\`job_duration_monthly\` int NOT NULL,
\`storage\` int NOT NULL,
\`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
\`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (\`id\`)
) ENGINE = InnoDB
`)
await queryRunner.query(`
CREATE TABLE \`project_job_usage\` (
\`id\` int NOT NULL AUTO_INCREMENT,
\`project_id\` int NOT NULL,
\`year\` int NOT NULL,
\`month\` int NOT NULL,
\`job_count\` int NOT NULL DEFAULT '0',
\`job_duration\` decimal(10, 2) NOT NULL DEFAULT '0.00',
\`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
INDEX \`idx_da16e6d19467189502a62a0d99\` (\`project_id\`),
PRIMARY KEY (\`id\`)
) ENGINE = InnoDB
`)
await queryRunner.query(`
CREATE TABLE \`project_storage_usage\` (
\`id\` int NOT NULL AUTO_INCREMENT,
\`project_id\` int NOT NULL,
\`used\` decimal(10, 2) NOT NULL DEFAULT '0.00',
\`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
INDEX \`idx_44711059df507f55fb11a5d68e\` (\`project_id\`),
UNIQUE INDEX \`uniq_rel_44711059df507f55fb11a5d68e\` (\`project_id\`),
PRIMARY KEY (\`id\`)
) ENGINE = InnoDB
`)
await queryRunner.query(`
ALTER TABLE \`artifact\`
ADD \`upload_size\` int NOT NULL DEFAULT '0'
`)
await queryRunner.query(`
ALTER TABLE \`project\`
ADD \`usage_pack_id\` int NULL
`)
await queryRunner.query(`
ALTER TABLE \`snapshot_report\`
ADD \`upload_size\` int NOT NULL DEFAULT '0'
`)
await queryRunner.query(`
ALTER TABLE \`project\`
ADD CONSTRAINT \`FK_89f29f98d1c234d1db6f47b1bb2\` FOREIGN KEY (\`usage_pack_id\`) REFERENCES \`usage_pack\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION
`)
await queryRunner.query(`
ALTER TABLE \`project_job_usage\`
ADD CONSTRAINT \`FK_da16e6d19467189502a62a0d993\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION
`)
await queryRunner.query(`
ALTER TABLE \`project_storage_usage\`
ADD CONSTRAINT \`FK_44711059df507f55fb11a5d68e3\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE \`project_storage_usage\` DROP FOREIGN KEY \`FK_44711059df507f55fb11a5d68e3\`
`)
await queryRunner.query(`
ALTER TABLE \`project_job_usage\` DROP FOREIGN KEY \`FK_da16e6d19467189502a62a0d993\`
`)
await queryRunner.query(`
ALTER TABLE \`project\` DROP FOREIGN KEY \`FK_89f29f98d1c234d1db6f47b1bb2\`
`)
await queryRunner.query(`
ALTER TABLE \`snapshot_report\` DROP COLUMN \`upload_size\`
`)
await queryRunner.query(`
ALTER TABLE \`project\` DROP COLUMN \`usage_pack_id\`
`)
await queryRunner.query(`
ALTER TABLE \`artifact\` DROP COLUMN \`upload_size\`
`)
await queryRunner.query(`
DROP INDEX \`uniq_rel_44711059df507f55fb11a5d68e\` ON \`project_storage_usage\`
`)
await queryRunner.query(`
DROP INDEX \`idx_44711059df507f55fb11a5d68e\` ON \`project_storage_usage\`
`)
await queryRunner.query(`
DROP TABLE \`project_storage_usage\`
`)
await queryRunner.query(`
DROP INDEX \`idx_da16e6d19467189502a62a0d99\` ON \`project_job_usage\`
`)
await queryRunner.query(`
DROP TABLE \`project_job_usage\`
`)
await queryRunner.query(`
DROP TABLE \`usage_pack\`
`)
}
}
4 changes: 4 additions & 0 deletions packages/platform-server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import {
ApplicationSettingModule,
RunnerScriptModule,
ScriptFileModule,
UsagePackModule,
ProjectUsageModule,
} from './modules'
import { RedisModule } from './redis'
import { RestfulModule } from './restful.module'
Expand Down Expand Up @@ -99,6 +101,8 @@ const businessModules: ModuleMetadata['imports'] = [
RunnerModule,
JobModule,
ScriptFileModule,
UsagePackModule,
ProjectUsageModule,
]

@Module({
Expand Down
17 changes: 16 additions & 1 deletion packages/platform-server/src/db/fixtures/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import {
PageWithProfile,
Profile,
Project,
UsagePack,
User,
} from '../mysql'

import { create } from './factory'

export async function seed() {
return Promise.all([create(ApplicationSetting), seedUsers(), seedProject()])
return Promise.all([create(ApplicationSetting), seedUsers(), seedUsagePack(), seedProject()])
}

export async function seedUsers() {
Expand Down Expand Up @@ -62,6 +63,20 @@ export async function seedUsers() {
return [admin, user]
}

export async function seedUsagePack() {
const usagePack = await create(UsagePack, {
name: 'unlimited',
desc: 'unlimited',
jobCountMonthly: -1,
jobDurationMonthly: -1,
storage: -1,
isDefault: true,
isPublic: true,
})

return [usagePack]
}

export async function seedProject() {
const project = await create(Project, {
id: 1,
Expand Down
4 changes: 4 additions & 0 deletions packages/platform-server/src/db/mysql/artifact.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class Artifact extends BaseEntity {
@Column({ type: 'varchar', nullable: true })
contentKey!: string | null

@Field(() => Int, { description: 'total size introduced by build, report and content files, in bytes' })
@Column({ default: 0 })
uploadSize!: number

@Field(() => Int, { description: 'bundle audit score', nullable: true })
@Column({ type: 'int', nullable: true })
score!: number | null
Expand Down
3 changes: 3 additions & 0 deletions packages/platform-server/src/db/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ export * from './internal-id.entity'
export * from './github.entity'
export * from './script-file.entity'
export * from './snapshot-report-with-artifact.entity'
export * from './project-usage-job.entity'
export * from './project-usage-storage.entity'
export * from './usage-pack.entity'
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export class PageWithCompetitor extends BaseEntity {
@RelationId('competitor')
competitorId!: number // competitor page id

@ManyToOne('Page')
@ManyToOne('Page', { onDelete: 'CASCADE' })
competitor!: Page
}
59 changes: 59 additions & 0 deletions packages/platform-server/src/db/mysql/project-usage-job.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2022 ByteDance and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import {
Entity,
Column,
PrimaryGeneratedColumn,
BaseEntity,
RelationId,
Index,
JoinColumn,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'

import { Project } from './project.entity'

@Entity()
export class ProjectJobUsage extends BaseEntity {
@PrimaryGeneratedColumn('increment')
id!: number

@Column()
@Index()
@RelationId('project')
projectId!: number

@ManyToOne(() => Project, { onDelete: 'CASCADE' })
@JoinColumn()
project!: Project

@Column()
year!: number

@Column()
month!: number

@Column({ default: 0 })
jobCount!: number

@Column({ default: 0, type: 'decimal', precision: 10, scale: 2 })
jobDuration!: string

@UpdateDateColumn({ type: 'timestamp' })
updatedAt!: Date
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2022 ByteDance and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import {
Entity,
Column,
PrimaryGeneratedColumn,
BaseEntity,
RelationId,
Index,
OneToOne,
JoinColumn,
UpdateDateColumn,
} from 'typeorm'

import { Project } from './project.entity'

@Entity()
export class ProjectStorageUsage extends BaseEntity {
@PrimaryGeneratedColumn('increment')
id!: number

@Column()
@Index()
@RelationId('project')
projectId!: number

@OneToOne(() => Project, { onDelete: 'CASCADE' })
@JoinColumn()
project!: Project

@Column({ default: 0, type: 'decimal', precision: 10, scale: 2 })
used!: string

@UpdateDateColumn({ type: 'timestamp' })
updatedAt!: Date
}

0 comments on commit 2277911

Please sign in to comment.