UNPKG

yapi-plugin-pl-test-dashboard

Version:

YAPI自动化测试结果数据面板插件,支持在YAPI中通过数据可视化的方式查看自动化测试结果。

153 lines (143 loc) 3.89 kB
const baseController = require('controllers/base.js') const yapi = require('yapi.js') const elasticsearch = require('./elasticsearch') const Config = require('../utils/config') const ops = Config.instance class dashboardController extends baseController { constructor(ctx) { super(ctx) this.elasticsearch = yapi.getInst(elasticsearch) this.indexName = `${ops.indexName}` } async getDashboardSource(ctx) { const projectId = ctx.request.query.projectId const startTime = ctx.request.query.startTime const endTime = ctx.request.query.endTime const bucketArray = await this.elasticsearch.groupDataSource( this.indexName, projectId, startTime, endTime ) let data = new Array() bucketArray.forEach((item) => { data.push({ type: item.key, value: item.doc_count, }) }) const res = { percent: data, count: data, } return (ctx.body = res) } async getDashboardSets(ctx) { const projectId = ctx.request.query.projectId const startTime = ctx.request.query.startTime const endTime = ctx.request.query.endTime const bucketArray = await this.elasticsearch.groupColTestAnalyze( this.indexName, projectId, startTime, endTime ) let percentageData = new Array() const countData = new Array() bucketArray.forEach((item) => { const colName = item.key const colData = item.group_by_tags.buckets const rate = this.getPassRate(colData, 'success', 'fail') this.setCountData(colName, colData, countData, 'success', 'fail') percentageData.push({ type: colName, value: rate, }) }) const res = { percent: percentageData, count: countData, } return (ctx.body = res) } async getDashboardCases(ctx) { const projectId = ctx.request.query.projectId const startTime = ctx.request.query.startTime const endTime = ctx.request.query.endTime const bucketArray = await this.elasticsearch.groupTestCaseAnalyze( this.indexName, projectId, startTime, endTime ) let percentageData = new Array() const countData = new Array() bucketArray.forEach((item) => { const colName = item.key const caseData = item.group_by_tags.buckets const rate = this.getPassRate(caseData, 0, 1) this.setCountData(colName, caseData, countData, 0, 1) percentageData.push({ type: colName, value: rate, }) }) const res = { percent: percentageData, count: countData, } return (ctx.body = res) } getPassRate(colData, successValue, failValue) { let success = 0 let fail = 0 colData.forEach((item) => { if (item.key === successValue) { success = item.doc_count } if (item.key === failValue) { fail = item.doc_count } }) const rate = success / (success + fail) return Number.parseFloat(rate.toFixed(2)) } setCountData(colName, colData, countData, successValue, failValue) { let success = false let fail = false colData.forEach((item) => { if (item.key === successValue) { countData.push({ type: colName, value: item.doc_count, series: '通过', }) success = true } if (item.key === failValue) { countData.push({ type: colName, value: item.doc_count, series: '失败', }) fail = true } }) //verify data has both success and fail field if (!success) { countData.push({ type: colName, value: 0, series: '通过', }) } if (!fail) { countData.push({ type: colName, value: 0, series: '失败', }) } } } module.exports = dashboardController