UNPKG

yapi-plugin-pl-test-dashboard

Version:

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

188 lines (180 loc) 4.47 kB
const Config = require('../utils/config') const yapi = require('yapi.js') const { Client } = require('@elastic/elasticsearch') const ops = Config.instance class elasticsearch { constructor(ctx) { this.ctx = ctx if (!ops.esUsername) { yapi.commons.log('will use es with no login', 'pl-test-dashboard') this.client = new Client({ node: `http://${ops.esHost}` }) return } this.client = new Client({ node: `http://${ops.esHost}`, auth: { username: ops.esUsername, password: ops.esPwd, }, }) } groupDataSource(indexName, projectId, timeStart, timeEnd) { const queryBody = this.generateBodyQuery(projectId, timeStart, timeEnd) // callback API const promise = new Promise((resolve, reject) => { this.client.search( { index: indexName, body: { query: queryBody, aggs: { group_by_tags: { terms: { field: 'source', }, }, }, }, }, (err, result) => { if (err) { yapi.commons.log( 'error:' + JSON.stringify(err), 'pl-test-dashboard' ) reject(new Array()) } else { resolve(result.body.aggregations.group_by_tags.buckets) } } ) }) return promise } groupColTestAnalyze(indexName, projectId, timeStart, timeEnd) { const queryBody = this.generateBodyQuery(projectId, timeStart, timeEnd) yapi.commons.log( 'queryBody:' + JSON.stringify(queryBody), 'pl-test-dashboard' ) // callback API const promise = new Promise((resolve, reject) => { this.client.search( { index: indexName, body: { query: queryBody, aggs: { group_by_tags: { terms: { field: 'test_col_name', }, aggs: { group_by_tags: { terms: { field: 'status', }, }, }, }, }, }, }, (err, result) => { if (err) { yapi.commons.log( 'error:' + JSON.stringify(err), 'pl-test-dashboard' ) yapi.commons.log( 'es result:' + JSON.stringify(result.body), 'pl-test-dashboard' ) reject(new Array()) } else { resolve(result.body.aggregations.group_by_tags.buckets) } } ) }) return promise } groupTestCaseAnalyze(indexName, projectId, timeStart, timeEnd) { const queryBody = this.generateBodyQuery(projectId, timeStart, timeEnd) const promise = new Promise((resolve, reject) => { this.client.search( { index: indexName, body: { query: queryBody, aggs: { group_by_tags: { terms: { field: 'list.name', }, aggs: { group_by_tags: { terms: { field: 'list.code', }, }, }, }, }, }, }, (err, result) => { if (err) { yapi.commons.log( 'error:' + JSON.stringify(err), 'pl-test-dashboard' ) reject(new Array()) } else { resolve(result.body.aggregations.group_by_tags.buckets) } } ) }) return promise } generateBodyQuery(projectId, timeStart, timeEnd) { let timeRange = { test_start: { gte: timeStart, lt: timeEnd, }, } if (!timeStart) { timeRange = { test_start: { gte: 'now/d', }, } } if (!timeEnd) { timeRange = { test_start: { gte: timeStart, lt: 'now+1d/d', }, } } const query = { bool: { must: [ { match: { project_id: projectId, }, }, ], filter: { range: timeRange, }, }, } return query } } module.exports = elasticsearch