Skip to content

Commit 2df7e05

Browse files
committed
feat(sys): support func version; add save status in ide;
1 parent f67962b commit 2df7e05

File tree

9 files changed

+182
-34
lines changed

9 files changed

+182
-34
lines changed

packages/system-client/package-lock.json

+49-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/system-client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"js-cookie": "2.2.0",
2525
"laf-client-sdk": "^0.6.3",
2626
"lodash": "^4.17.21",
27+
"md5": "^2.3.0",
2728
"monaco-editor": "^0.25.0",
2829
"normalize.css": "7.0.0",
2930
"nprogress": "0.2.0",

packages/system-client/src/api/func.js

+27
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,33 @@ export function getFunctions(query, page, pageSize) {
2121
})
2222
}
2323

24+
/**
25+
* Get published functions by ids
26+
* @param {string[]} ids
27+
* @returns
28+
*/
29+
export function getPublishedFunctions(ids) {
30+
const appid = store.state.app.appid
31+
return request({
32+
url: `/apps/${appid}/function/published`,
33+
method: 'POST',
34+
data: {
35+
ids
36+
}
37+
})
38+
}
39+
40+
/**
41+
* Get published function by id
42+
* @param {string} id
43+
* @returns
44+
*/
45+
export async function getPublishedFunction(id) {
46+
const res = await getPublishedFunctions([id])
47+
const [func] = res?.data ?? []
48+
return func
49+
}
50+
2451
/**
2552
* Get all tags of cloud functions
2653
*/
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import md5 from 'md5'
3+
4+
export function hashString(str) {
5+
return md5(str)
6+
}

packages/system-client/src/views/cloudfunction/debug.vue

+42-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
<template>
22
<div class="app-container">
33
<div v-if="func" class="header">
4-
<span style="font-size: 22px;line-height: 40px;"><b>{{ func.label }}</b> </span>
4+
<span style="font-size: 22px;line-height: 40px;">
5+
<!-- <el-tag v-if="saved_code_diff" type="warning" size="mini" effect="plain">*</el-tag> -->
6+
<b>{{ func.label }}</b>
7+
<span v-if="saved_code_diff" style="margin-left: 8px; font-size: 18px; color: red">
8+
<i class="el-icon-edit" />
9+
</span>
10+
</span>
511
<el-tag v-clipboard:message="func.name" v-clipboard:success="onCopy" style="margin-left: 14px; " size="mini" type="success">{{ func.name }}</el-tag>
12+
613
<el-button
714
style="margin-left: 20px"
815
icon="el-icon-refresh"
916
type="text"
10-
:disabled="loading"
1117
size="default"
18+
:loading="loading"
1219
@click="getFunction"
1320
>刷新</el-button>
1421
<el-button
15-
type="success"
1622
size="mini"
1723
style="margin-left: 20px;"
18-
:disabled="loading || !func"
24+
:loading="loading"
25+
:disabled="!saved_code_diff"
26+
:type="saved_code_diff ? 'success' : 'success'"
1927
@click="updateFunc"
2028
>保存(S)</el-button>
2129
<el-button
22-
type="warning"
23-
plain
30+
:type="published_version_diff ? 'default' : 'text'"
2431
size="mini"
32+
:loading="loading"
2533
style="margin-left: 15px;"
26-
:disabled="loading || !func"
34+
:disabled="!published_version_diff"
2735
@click="publishFunction"
28-
>发布</el-button>
36+
>{{ published_version_diff ? '发布': '已发布' }}</el-button>
2937
<el-button size="small" style="float: right;" type="primary" @click="showDebugPanel = true">显示调试面板(J)</el-button>
3038
</div>
3139

@@ -69,7 +77,7 @@
6977
size="mini"
7078
type="success"
7179
style="margin-left: 10px"
72-
:disabled="loading || !func"
80+
:loading="loading"
7381
@click="launch"
7482
>运行(B)</el-button>
7583
</div>
@@ -114,9 +122,10 @@
114122
import FunctionLogDetail from './components/FunctionLogDetail'
115123
import FunctionEditor from '@/components/FunctionEditor'
116124
import jsonEditor from '@/components/JsonEditor/param'
117-
import { getFunctionById, getFunctionLogs, launchFunction, publishOneFunction, updateFunctionCode } from '../../api/func'
125+
import { getFunctionById, getFunctionLogs, getPublishedFunction, launchFunction, publishOneFunction, updateFunctionCode } from '../../api/func'
118126
import { showError, showSuccess } from '@/utils/show'
119127
import { debounce } from 'lodash'
128+
import { hashString } from '@/utils/hash'
120129
121130
const defaultParamValue = {
122131
code: 'laf'
@@ -130,6 +139,7 @@ export default {
130139
value: '',
131140
editorHeight: 500,
132141
func: null,
142+
published_func: null,
133143
func_id: '',
134144
invokeParams: defaultParamValue,
135145
// 调用云函数返回的值
@@ -156,6 +166,21 @@ export default {
156166
},
157167
app() {
158168
return this.$store.state.app.application
169+
},
170+
published_version_diff() {
171+
const cur = this.func?.version
172+
const pub = this.published_func?.version
173+
return cur !== pub
174+
},
175+
published_code_diff() {
176+
const cur = this.func?.hash
177+
const pub = this.published_func?.hash
178+
return cur !== pub
179+
},
180+
saved_code_diff() {
181+
const cur = hashString(this.value)
182+
const saved = this.func?.hash
183+
return cur !== saved
159184
}
160185
},
161186
watch: {
@@ -201,11 +226,16 @@ export default {
201226
this.value = this.func.code
202227
this.invokeParams = this.parseInvokeParam(this.func.debugParams) ?? defaultParamValue
203228
this.loading = false
229+
230+
this.published_func = await getPublishedFunction(func_id)
204231
},
205232
/**
206233
* 保存函数代码
207234
*/
208235
async updateFunc(showTip = true) {
236+
if (!this.saved_code_diff) {
237+
return
238+
}
209239
if (this.loading) { return }
210240
if (this.validate()) { return }
211241
@@ -224,12 +254,8 @@ export default {
224254
225255
if (r.error) { return showError('保存失败!') }
226256
227-
this.func = r.data
228-
this.value = this.func.code
229-
this.invokeParams = this.parseInvokeParam(this.func.debugParams) ?? defaultParamValue
230-
257+
await this.getFunction()
231258
if (showTip) {
232-
// await this.getFunction()
233259
showSuccess('已保存: ' + this.func.name)
234260
}
235261
},
@@ -268,6 +294,7 @@ export default {
268294
269295
if (r.error) { return showError('发布失败!') }
270296
297+
this.getFunction()
271298
showSuccess('已发布: ' + this.func.name)
272299
},
273300
async getLogByRequestId(requestId) {

packages/system-server/src/router/function/get.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-30 16:51:19
4-
* @LastEditTime: 2021-11-01 16:56:16
4+
* @LastEditTime: 2021-11-02 15:37:22
55
* @Description:
66
*/
77

88
import { CloudFunctionStruct } from 'cloud-function-engine'
99
import { Request, Response } from 'express'
1010
import { ObjectId } from 'mongodb'
11-
import { ApplicationStruct } from '../../api/application'
11+
import { ApplicationStruct, getApplicationDbAccessor } from '../../api/application'
1212
import { FunctionStruct, getFunctionById } from '../../api/function'
1313
import { checkPermission } from '../../api/permission'
1414
import { Constants } from '../../constants'
@@ -119,3 +119,35 @@ export async function handleGetAllFunctionTags(req: Request, res: Response) {
119119
data: docs
120120
})
121121
}
122+
123+
/**
124+
* Get published functions
125+
*/
126+
export async function handleGetPublishedFunctions(req: Request, res: Response) {
127+
const app: ApplicationStruct = req['parsed-app']
128+
129+
const func_ids = req.body?.ids
130+
if (!(func_ids instanceof Array) || !func_ids?.length) {
131+
return res.status(422).send('invalid param func_ids')
132+
}
133+
134+
// check permission
135+
const code = await checkPermission(req['auth']?.uid, FUNCTION_READ.name, app)
136+
if (code) {
137+
return res.status(code).send()
138+
}
139+
140+
// build query object
141+
const ids = func_ids.map(id => new ObjectId(id))
142+
const query = { appid: app.appid, _id: { $in: ids } }
143+
144+
const accessor = await getApplicationDbAccessor(app)
145+
const db = accessor.db
146+
const docs = await db.collection(Constants.function_collection)
147+
.find(query, {})
148+
.toArray()
149+
150+
return res.send({
151+
data: docs
152+
})
153+
}

packages/system-server/src/router/function/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*
22
* @Author: Maslow<[email protected]>
33
* @Date: 2021-08-29 11:35:05
4-
* @LastEditTime: 2021-11-02 14:25:55
4+
* @LastEditTime: 2021-11-02 15:28:21
55
* @Description:
66
*/
77

88
import { Router } from "express"
99
import { handleCreateFunction } from "./create"
10-
import { handleGetAllFunctionTags, handleGetFunctionById, handleGetFunctions } from "./get"
10+
import { handleGetAllFunctionTags, handleGetFunctionById, handleGetFunctions, handleGetPublishedFunctions } from "./get"
1111
import { handleGetFunctionLogs } from "./logs"
1212
import { handlePublishFunctions, handlePublishOneFunction } from "./publish"
1313
import { handleRemoveFunctionById } from "./remove"
@@ -68,6 +68,12 @@ FunctionRouter.post('/publish', handlePublishFunctions)
6868
*/
6969
FunctionRouter.post('/:func_id/publish', handlePublishOneFunction)
7070

71+
72+
/**
73+
* Get published functions by ids
74+
*/
75+
FunctionRouter.post('/published', handleGetPublishedFunctions)
76+
7177
/**
7278
* Get function logs
7379
*/

0 commit comments

Comments
 (0)