/**
 * Created by gxl.gao on 2017/10/24.
 */
const yapi = require('yapi.js')
const mongoose = require('mongoose');
const controller = require('./controller');
const statisModel = require('./statisMockModel.js');
const commons = require('./util.js');

module.exports = function () {

    yapi.connect.then(function () {
        let Col = mongoose.connection.db.collection('statis_mock');
        Col.createIndex({
            interface_id: 1
        })
        Col.createIndex({
            project_id: 1
        })
        Col.createIndex({
            group_id: 1
        })
        Col.createIndex({
            time: 1
        })
        Col.createIndex({
            date: 1
        })

    });

    this.bindHook('add_router', function (addRouter) {
        addRouter({
            controller: controller,
            method: 'get',
            path: 'statismock/count',
            action: 'getStatisCount'
        })

        addRouter({
            controller: controller,
            method: 'get',
            path: 'statismock/get',
            action: 'getMockDateList'
        })
    })

    // MockServer生成mock数据后触发
    this.bindHook('mock_after', async function (context) {

        let interfaceId = context.interfaceData._id;
        let projectId = context.projectData._id;
        let groupId = context.projectData.group_id;
        let ip = context.ctx.originalUrl;

        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 {
            let result = await inst.save(data);
            result = yapi.commons.fieldSelect(result, ['interface_id', 'project_id', 'group_id', 'time', 'ip', 'date']);

        } catch (e) {
            yapi.commons.log('mockStatisError', e);
        }
    })
};
@@ -1158,14 +1158,8 @@
 er',
- async
  fun
@@ -1172,24 +1172,24 @@
 (context) %7B%0A
+
 %0A        let
@@ -1335,24 +1335,26 @@
 id;%0A        
+//
 let ip = con
@@ -1375,16 +1375,76 @@
 nalUrl;%0A
+        let ip = context.ctx.ip.match(/%5Cd+.%5Cd+.%5Cd+.%5Cd+/)%5B0%5D;
 %0A       
@@ -1757,162 +1757,29 @@
     
-let result = await inst.save(data);%0A            result = yapi.commons.fieldSelect(result, %5B'interface_id', 'project_id', 'group_id', 'time', 'ip', 'date'%5D
+inst.save(data).then(
 );%0A%0A

保存ip的 bug