yapi/exts/yapi-plugin-statistics/server.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-10-25 19:33:34 +08:00
/**
* Created by gxl.gao on 2017/10/24.
*/
const yapi = require('yapi.js')
const mongoose = require('mongoose');
const controller = require('./controller');
2017-10-27 12:13:59 +08:00
const statisModel = require('./statisMockModel.js');
const commons = require('./util.js');
2017-10-25 19:33:34 +08:00
2017-10-27 12:13:59 +08:00
module.exports = function () {
2017-10-25 19:33:34 +08:00
yapi.connect.then(function () {
let Col = mongoose.connection.db.collection('statis_mock');
Col.createIndex({
interface_id: 1
})
Col.createIndex({
project_id: 1
})
2017-10-27 12:13:59 +08:00
Col.createIndex({
group_id: 1
})
Col.createIndex({
time: 1
})
2017-10-27 15:20:28 +08:00
Col.createIndex({
date: 1
})
2017-10-25 19:33:34 +08:00
});
2017-10-27 12:13:59 +08:00
this.bindHook('add_router', function (addRouter) {
addRouter({
controller: controller,
method: 'get',
path: 'statismock/count',
action: 'getStatisCount'
})
2017-10-25 19:33:34 +08:00
addRouter({
controller: controller,
method: 'get',
path: 'statismock/get',
2017-10-27 12:13:59 +08:00
action: 'getMockDateList'
2017-10-25 19:33:34 +08:00
})
})
2017-10-27 12:13:59 +08:00
// MockServer生成mock数据后触发
2017-11-09 20:29:29 +08:00
this.bindHook('mock_after', function (context) {
2017-10-27 12:13:59 +08:00
let interfaceId = context.interfaceData._id;
let projectId = context.projectData._id;
let groupId = context.projectData.group_id;
2017-11-09 20:29:29 +08:00
//let ip = context.ctx.originalUrl;
let ip = context.ctx.ip.match(/\d+.\d+.\d+.\d+/)[0];
2017-10-27 12:13:59 +08:00
let data = {
interface_id: interfaceId,
project_id: projectId,
group_id: groupId,
time: yapi.commons.time(),
ip: ip,
date: commons.formatYMD(new Date())
};
let inst = yapi.getInst(statisModel);
try {
2017-11-09 20:29:29 +08:00
inst.save(data).then();
2017-10-27 12:13:59 +08:00
} catch (e) {
2017-10-27 17:13:38 +08:00
yapi.commons.log('mockStatisError', e);
2017-10-27 12:13:59 +08:00
}
})
2017-10-25 19:33:34 +08:00
};