Skip to content

Commit

Permalink
feat(system): impl functin logs api & pages;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 5, 2021
1 parent 49fd049 commit b11e17e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 83 deletions.
2 changes: 1 addition & 1 deletion docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
SYS_SERVER_SECRET_SALT: Rewrite_Your_Own_Secret_Salt_abcdefg1234567
SHARED_NETWORK: laf_shared_network
LOG_LEVEL: debug
command: npm run start
command: node dist/index.js
volumes:
- ./packages/system-server:/app
- /var/run/docker.sock:/var/run/docker.sock
Expand Down
21 changes: 21 additions & 0 deletions packages/system-client/src/api/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,24 @@ export async function loadPackageTypings(packageName) {

return res.data
}

/**
* Get cloud function logs
* @param {*} query
* @param {*} page
* @param {*} pageSize
*/
export async function getFunctionLogs(query, page, pageSize) {
const appid = store.state.app.appid
const res = await request({
url: `/apps/${appid}/function/logs/query`,
method: 'get',
params: {
...query,
page,
limit: pageSize
}
})

return res
}
19 changes: 6 additions & 13 deletions packages/system-client/src/views/cloudfunction/debug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
import FunctionLogDetail from './components/FunctionLogDetail'
import FunctionEditor from '@/components/FunctionEditor'
import jsonEditor from '@/components/JsonEditor/param'
import { getFunctionById, launchFunction, updateFunctionCode } from '../../api/func'
import { getFunctionById, getFunctionLogs, launchFunction, updateFunctionCode } from '../../api/func'
import { publishFunctions } from '../../api/func'
const defaultParamValue = {
Expand Down Expand Up @@ -276,18 +276,11 @@ export default {
* 获取最近日志
*/
async getLatestLogs() {
// this.loading = true
// const db = dbm_cloud.database()
// // 执行数据查询
// const res = await db.collection('__function_logs')
// .where({ func_id: this.func_id })
// .limit(20)
// .skip(0)
// .orderBy('created_at', 'desc')
// .get()
// .finally(() => { this.loading = false })
// this.lastestLogs = res.data || []
this.loading = true
const res = await getFunctionLogs({ func_id: this.func_id }, 1, 30)
.finally(() => { this.loading = false })
this.lastestLogs = res.data || []
},
showLogDetailDlg(log) {
this.logDetail = log
Expand Down
26 changes: 18 additions & 8 deletions packages/system-client/src/views/cloudfunction/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,22 @@
highlight-current-row
style="width: 100%;"
>
<el-table-column label="函数标识" min-width="100">
<el-table-column label="函数标识" min-width="120">
<template slot-scope="{row}">
<div v-clipboard:message="row.name" class="func-name">
{{ row.name }}
<div style="display: flex;align-items: center;justify-content: space-between;">
<div class="func-name">
{{ row.name }}
</div>
<i v-clipboard:message="row.name" v-clipboard:success="onCopy" class="el-icon-document-copy copy-btn" />
</div>
</template>
</el-table-column>
<el-table-column label="函数标题" min-width="100px">
<el-table-column label="函数标题" min-width="120px">
<template slot-scope="{row}">
<span class="link-type" @click="showUpdateForm(row)">{{ row.label }}</span>
</template>
</el-table-column>
<el-table-column label="标签" min-width="200">
<el-table-column label="标签" min-width="140">
<template slot-scope="{row}">
<el-tag v-for="tag in row.tags" :key="tag" style="margin-right: 6px;" type="primary" size="mini">{{ tag }}</el-tag>
</template>
Expand Down Expand Up @@ -94,7 +97,6 @@
</el-table-column>
<el-table-column fixed="right" label="操作" align="center" width="380" class-name="small-padding">
<template slot-scope="{row,$index}">

<el-button plain type="success" size="mini" @click="handleShowDetail(row)">
调试
</el-button>
Expand Down Expand Up @@ -437,7 +439,7 @@ export default {
},
// 查看日志详情
async handleShowLogs(row) {
this.$router.push(`function-logs/${row._id}`)
this.$router.push(`logs/${row._id}`)
},
// 设置触发器
async handleTriggers(row) {
Expand Down Expand Up @@ -471,6 +473,9 @@ export default {
this.form.tags.push(val)
}
this.form._tag_input = ''
},
onCopy() {
this.$message.success('已复制')
}
}
}
Expand Down Expand Up @@ -499,8 +504,13 @@ export default {
.func-name {
color: black;
// font-weight: bold;
font-size: 16px;
font-size: 14px;
}
.copy-btn {
cursor: pointer;
}
.input-new-tag {
width: 120px;
margin-left: 10px;
Expand Down
69 changes: 9 additions & 60 deletions packages/system-client/src/views/cloudfunction/logs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
highlight-current-row
style="width: 100%;"
>
<el-table-column label="RequestId" prop="id" align="center" width="200">
<el-table-column label="RequestId" prop="id" align="center" width="280">
<template slot-scope="{row}">
<span>{{ row.requestId }}</span>
</template>
</el-table-column>
<el-table-column label="函数名" width="220px" align="center">
<el-table-column label="函数名" width="200px" align="center">
<template slot-scope="{row}">
<el-tag type="primary">{{ row.func_name }}</el-tag>
</template>
</el-table-column>
<el-table-column label="函数ID" min-width="140px" align="center">
<el-table-column label="函数ID" min-width="150px" align="center">
<template slot-scope="{row}">
<span class="link-type">{{ row.func_id }}</span>
</template>
</el-table-column>
<el-table-column label="执行用时" min-width="140px" align="center">
<el-table-column label="执行用时" min-width="100px" align="center">
<template slot-scope="{row}">
<span v-if="row.time_usage" class="link-type">{{ row.time_usage }}ms</span>
<span v-else>-</span>
Expand All @@ -52,25 +52,16 @@
<span v-else>-</span>
</template>
</el-table-column>
<el-table-column label="更新时间" width="160px" align="center">
<template slot-scope="{row}">
<span v-if="row.updated_at">{{ row.updated_at | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
<span v-else>-</span>
</template>
</el-table-column>
<el-table-column label="调用者ID" class-name="status-col" width="240">
<template slot-scope="{row}">
{{ row.created_by }}
{{ row.created_by || '-' }}
</template>
</el-table-column>
<el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
<template slot-scope="{row,$index}">
<template slot-scope="{row}">
<el-button type="info" size="mini" @click="handleShowDetail(row)">
查看调用日志
</el-button>
<el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
删除
</el-button>
</template>
</el-table-column>
</el-table>
Expand All @@ -94,9 +85,7 @@
<script>
import FunctionLogDetail from './components/FunctionLogDetail'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { dbm_cloud } from '@/api/cloud'
const db = dbm_cloud.database()
import { getFunctionLogs } from '@/api/func'
export default {
name: 'FunctionlogsListPage',
Expand Down Expand Up @@ -157,58 +146,18 @@ export default {
}
// 执行数据查询
const res = await db.collection('__function_logs')
.where(query)
.limit(limit)
.skip((page - 1) * limit)
.orderBy('created_at', 'desc')
.get()
const res = await getFunctionLogs(query, page, limit)
.catch(() => { this.listLoading = false })
this.list = res.data
// 获取数据总数
const { total } = await db.collection('__function_logs')
.where(query)
.limit(limit)
.skip((page - 1) * limit)
.count()
.catch(() => { this.listLoading = false })
this.total = total
this.total = res.total
this.listLoading = false
},
// 搜索
handleFilter() {
this.listQuery.page = 1
this.getList()
},
// 删除请求
async handleDelete(row, index) {
await this.$confirm('确认要删除此数据?', '删除确认')
// 执行删除请求
const r = await db.collection('__function_logs')
.where({ _id: row._id })
.remove()
if (!r.ok) {
this.$notify({
type: 'error',
title: '操作失败',
message: '删除失败!' + r.error
})
return
}
this.$notify({
type: 'success',
title: '操作成功',
message: '删除成功!'
})
this.list.splice(index, 1)
},
// 查看详情
async handleShowDetail(row) {
this.detail = row
Expand Down
7 changes: 6 additions & 1 deletion packages/system-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:05
* @LastEditTime: 2021-09-05 02:41:42
* @LastEditTime: 2021-09-05 23:17:32
* @Description:
*/

import { Router } from "express"
import { handleCreateFunction } from "./create"
import { handleGetFunctionById, handleGetFunctions } from "./get"
import { handleGetFunctionLogs } from "./logs"
import { handlePublishFunctions } from "./publish"
import { handleRemoveFunctionById } from "./remove"
import { handlePublishTriggers } from "./trigger"
Expand Down Expand Up @@ -56,6 +57,10 @@ FunctionRouter.post('/:func_id/debug')
*/
FunctionRouter.post('/publish', handlePublishFunctions)

/**
* Get function logs
*/
FunctionRouter.get('/logs/query', handleGetFunctionLogs)

/**
* Create a trigger to the function
Expand Down
71 changes: 71 additions & 0 deletions packages/system-server/src/router/function/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-05 23:24:48
* @Description:
*/

import { Request, Response } from 'express'
import { getDb } from 'less-api'
import { ApplicationStruct, getApplicationDbAccessor } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { permissions } from '../../constants/permissions'

const { FUNCTION_READ } = permissions


/**
* Get function logs
*/
export async function handleGetFunctionLogs(req: Request, res: Response) {
const app: ApplicationStruct = req['parsed-app']
const accessor = await getApplicationDbAccessor(app)
const db = getDb(accessor)

// check permission
const code = await checkPermission(req['auth']?.uid, FUNCTION_READ.name, app)
if (code) {
return res.status(code).send()
}

// build query object
const { requestId, func_id, triggerId } = req.body
const limit = req.body?.limit ?? 10
const page = req.body?.page ?? 1

const query = {}

if (requestId) {
query['requestId'] = requestId
}

if (func_id) {
query['func_id'] = func_id
}

if (triggerId) {
query['trigger_id'] = triggerId
}

const coll = db.collection('__function_logs')

// do db query
const ret = await coll
.where(query)
.limit(limit)
.skip((page - 1) * limit)
.orderBy('created_at', 'desc')
.get()

// get the count
const { total } = await coll
.where(query)
.count()

return res.send({
data: ret.data,
total: total,
limit: limit,
page
})
}

0 comments on commit b11e17e

Please sign in to comment.