feat: statisticsPlugin add test

This commit is contained in:
suxiaoxin 2017-10-27 15:14:01 +08:00
parent ef79b8be77
commit 90f0f5d46b

View File

@ -0,0 +1,60 @@
const fs = require('fs-extra');
const yapi = require('../../server/yapi.js');
const commons = require('../../server/utils/commons');
const dbModule = require('../../server/utils/db.js');
const userModel = require('../../server/models/user.js');
const mongoose = require('mongoose');
yapi.commons = commons;
yapi.connect = dbModule.connect();
const convert2Decimal = num => (num > 9 ? num : `0${num}`)
const formatYMD = (val, joinStr = '-') => {
let date = val;
if (typeof val !== 'object') {
val = val * 1000;
date = new Date(val);
}
return `${[
date.getFullYear(),
convert2Decimal(date.getMonth() + 1),
convert2Decimal(date.getDate())
].join(joinStr)}`;
}
function run() {
let time = yapi.commons.time() - 1000000;
let data = i => {
time = time - yapi.commons.rand(100, 100000);
return {
interface_id: 94,
project_id: 25,
group_id: 19,
time: time ,
ip: '1.1.1.1',
date: formatYMD(time)
};
}
yapi.connect.then(function () {
let logCol = mongoose.connection.db.collection('statis_mock');
let arr = [];
for(let i=0; i< 11; i++){
if(arr.length >= 5){
logCol.insert(arr);
arr = [];
}
arr.push(data(i));
}
}).catch(function(err){
throw new Error(err.message);
})
}
run();