Skip to content

Commit

Permalink
feat(platform-server): take snapshot restful api
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Oct 17, 2022
1 parent d9acf02 commit 787f98c
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
150 changes: 150 additions & 0 deletions packages/platform-server/src/modules/snapshot/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { BadRequestException, Controller, ForbiddenException, Get, NotFoundException, Query, Req } from '@nestjs/common'
import { Request } from 'express'
import { mapKeys, pick } from 'lodash'

import { required } from '@perfsee/platform-server/error'

import { AuthService } from '../auth/auth.service'
import { EnvironmentService } from '../environment/service'
import { PageService } from '../page/service'
import { Permission, PermissionProvider } from '../permission'
import { ProfileService } from '../profile/service'
import { ProjectService } from '../project/service'

import { SnapshotService } from './service'
import { SnapshotReportService } from './snapshot-report/service'

interface TakeSnapshotParams {
projectId: string
hash: string
title: string
env: string[] | string
page: string[] | string
profile: string[] | string
}

function parseArrayParams(p: string | string[] | null): string[] {
if (p) {
if (typeof p === 'string') {
return [p]
} else {
return p
}
} else {
return []
}
}

@Controller('/v1')
export class SnapshotController {
constructor(
private readonly service: SnapshotService,
private readonly snapshotReportService: SnapshotReportService,
private readonly auth: AuthService,
private readonly permission: PermissionProvider,
private readonly projectService: ProjectService,
private readonly pageService: PageService,
private readonly environmentService: EnvironmentService,
private readonly profileService: ProfileService,
) {}

@Get('/take-snapshot')
async takeSnapshot(@Req() req: Request, @Query() params: TakeSnapshotParams) {
required(params, 'projectId')

const envFilter = parseArrayParams(params.env)
const pageFilter = parseArrayParams(params.page)
const profileFilter = parseArrayParams(params.profile)

const project = await this.projectService.getProject(params.projectId)

if (!project) {
throw new NotFoundException(`Project ${params.projectId} not found. Did you forget to create it?`)
}

const user = await this.auth.getUserFromRequest(req)
if (!user || !(await this.permission.check(user, project.id, Permission.Admin))) {
throw new ForbiddenException('Invalid access token.')
}

const pages = await this.pageService.getPages(project.id)
const environments = await this.environmentService.getEnvironments(project.id)
const profiles = await this.profileService.getProfiles(project.id)

const pageIids = pageFilter.map((pageName) => {
const page = pages.find((p) => p.name === pageName)
if (!page) {
throw new BadRequestException(`Page "${pageName}" not found`)
}
return page.iid
})

const envIids = envFilter.map((envName) => {
const env = environments.find((p) => p.name === envName)
if (!env) {
throw new BadRequestException(`Environment "${envName}" not found`)
}
return env.iid
})

const profileIids = profileFilter.map((profileName) => {
const profile = profiles.find((p) => p.name === profileName)
if (!profile) {
throw new BadRequestException(`Profile "${profileName}" not found`)
}
return profile.iid
})

const snapshot = await this.service.takeSnapshot({
projectId: project.id,
pageIids,
profileIids,
envIids,
issuer: user.email,
hash: params.hash,
title: params.title,
})

const reports = await this.snapshotReportService.getReportsBySnapshotId(snapshot.id)

return {
id: snapshot.iid,
...pick(snapshot, 'status', 'startedAt', 'createdAt', 'issuer', 'hash', 'title', 'trigger'),
reports: reports.map((report) => ({
id: report.iid,
...pick(report, 'createdAt', 'status', 'host'),
page: mapKeys(
pick(
pages.find((p) => p.id === report.pageId),
'iid',
'name',
'url',
'isCompetitor',
'isTemp',
'isE2e',
),
(_, k) => (k === 'iid' ? 'id' : k),
),
profile: mapKeys(
pick(
profiles.find((p) => p.id === report.profileId),
'iid',
'name',
'device',
'bandWidth',
),
(_, k) => (k === 'iid' ? 'id' : k),
),
env: mapKeys(
pick(
environments.find((e) => e.id === report.envId),
'iid',
'name',
'zone',
),
(_, k) => (k === 'iid' ? 'id' : k),
),
})),
}
}
}
6 changes: 6 additions & 0 deletions packages/platform-server/src/modules/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import { StorageModule } from '@perfsee/platform-server/storage'

import { AppVersionModule } from '../app-version'
import { CheckSuiteModule } from '../checksuite'
import { EnvironmentModule } from '../environment'
import { NotificationModule } from '../notification'
import { PageModule } from '../page'
import { ProfileModule } from '../profile'
import { ProjectModule } from '../project'
import { SourceModule } from '../source'

import { SnapshotController } from './controller'
import { ProjectSnapshotResolver, SnapshotResolver } from './resolver'
import { SnapshotService } from './service'
import { SnapshotReportModule } from './snapshot-report'
Expand All @@ -41,7 +44,10 @@ import { SnapshotReportModule } from './snapshot-report'
CheckSuiteModule,
NotificationModule,
AppVersionModule,
EnvironmentModule,
ProfileModule,
],
controllers: [SnapshotController],
providers: [SnapshotResolver, ProjectSnapshotResolver, SnapshotService],
exports: [SnapshotService],
})
Expand Down

0 comments on commit 787f98c

Please sign in to comment.