Skip to content
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

feat(server): support cloud function history #1283

Merged
merged 1 commit into from
Jun 20, 2023
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
24 changes: 24 additions & 0 deletions server/src/function/entities/cloud-function-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ApiProperty } from '@nestjs/swagger'
import { ObjectId } from 'mongodb'

export class CloudFunctionHistorySource {
@ApiProperty()
code: string
}

export class CloudFunctionHistory {
@ApiProperty({ type: String })
_id?: ObjectId

@ApiProperty()
appid: string

@ApiProperty({ type: String })
functionId: ObjectId

@ApiProperty({ type: CloudFunctionHistorySource })
source: CloudFunctionHistorySource

@ApiProperty()
createdAt: Date
}
49 changes: 37 additions & 12 deletions server/src/function/function.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import {
} from '@nestjs/common'
import { CreateFunctionDto } from './dto/create-function.dto'
import { UpdateFunctionDto } from './dto/update-function.dto'
import { ResponseUtil } from '../utils/response'
import {
ApiBearerAuth,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
ApiResponseArray,
ApiResponseObject,
ResponseUtil,
} from '../utils/response'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { FunctionService } from './function.service'
import { IRequest } from '../utils/interface'
import { CompileFunctionDto } from './dto/compile-function.dto'
Expand All @@ -28,6 +27,8 @@ import { I18n, I18nContext, I18nService } from 'nestjs-i18n'
import { I18nTranslations } from '../generated/i18n.generated'
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'
import { ApplicationAuthGuard } from 'src/authentication/application.auth.guard'
import { CloudFunctionHistory } from './entities/cloud-function-history'
import { CloudFunction } from './entities/cloud-function'

@ApiTags('Function')
@ApiBearerAuth('Authorization')
Expand All @@ -44,7 +45,7 @@ export class FunctionController {
* @param dto
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Create a new function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
Expand Down Expand Up @@ -90,7 +91,7 @@ export class FunctionController {
* Query function list of an app
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseArray(CloudFunction)
@ApiOperation({ summary: 'Query function list of an app' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
Expand All @@ -104,7 +105,7 @@ export class FunctionController {
* @param appid
* @param name
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Get a function by its name' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name')
Expand All @@ -130,7 +131,7 @@ export class FunctionController {
* @param dto
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Update a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Patch(':name')
Expand Down Expand Up @@ -161,7 +162,7 @@ export class FunctionController {
* @param name
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Delete a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Delete(':name')
Expand Down Expand Up @@ -191,7 +192,7 @@ export class FunctionController {
* @param name
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Compile a function ' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post(':name/compile')
Expand All @@ -216,4 +217,28 @@ export class FunctionController {
const res = await this.functionsService.compile(func, dto)
return ResponseUtil.ok(res)
}

/**
* Get function history
*/
@ApiResponseArray(CloudFunctionHistory)
@ApiOperation({ summary: 'Get cloud function history' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name/history')
async getHistory(
@Param('appid') appid: string,
@Param('name') name: string,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}

const res = await this.functionsService.getHistory(func)
return ResponseUtil.ok(res)
}
}
57 changes: 57 additions & 0 deletions server/src/function/function.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ObjectId } from 'mongodb'
import { CloudFunction } from './entities/cloud-function'
import { ApplicationConfiguration } from 'src/application/entities/application-configuration'
import { FunctionLog } from 'src/log/entities/function-log'
import { CloudFunctionHistory } from './entities/cloud-function-history'

@Injectable()
export class FunctionService {
Expand Down Expand Up @@ -45,6 +46,8 @@ export class FunctionService {
})

const fn = await this.findOne(appid, dto.name)

await this.addOneHistoryRecord(fn)
await this.publish(fn)
return fn
}
Expand Down Expand Up @@ -94,6 +97,7 @@ export class FunctionService {
)

const fn = await this.findOne(func.appid, func.name)
await this.addOneHistoryRecord(fn)
await this.publish(fn)
return fn
}
Expand All @@ -104,6 +108,7 @@ export class FunctionService {
.collection<CloudFunction>('CloudFunction')
.findOneAndDelete({ appid, name })

await this.deleteHistory(res.value)
await this.unpublish(appid, name)
return res.value
}
Expand All @@ -113,6 +118,8 @@ export class FunctionService {
.collection<CloudFunction>('CloudFunction')
.deleteMany({ appid })

await this.deleteHistoryAll(appid)

return res
}

Expand Down Expand Up @@ -234,4 +241,54 @@ export class FunctionService {
await client.close()
}
}

async getHistory(func: CloudFunction) {
const history = await this.db
.collection<CloudFunctionHistory>('CloudFunctionHistory')
.find(
{
functionId: func._id,
},
{
limit: 30,
sort: {
createdAt: -1,
},
},
)
.toArray()

return history
}

async addOneHistoryRecord(func: CloudFunction) {
await this.db
.collection<CloudFunctionHistory>('CloudFunctionHistory')
.insertOne({
appid: func.appid,
functionId: func._id,
source: {
code: func.source.code,
},
createdAt: new Date(),
})
}

async deleteHistory(func: CloudFunction) {
const res = await this.db
.collection<CloudFunctionHistory>('CloudFunctionHistory')
.deleteMany({
functionId: func._id,
})
return res
}

async deleteHistoryAll(appid: string) {
const res = await this.db
.collection<CloudFunctionHistory>('CloudFunctionHistory')
.deleteMany({
appid,
})
return res
}
}