'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.middleware = exports.requestTime = undefined;

var _config = require('./config');

var _config2 = _interopRequireDefault(_config);

var _influxdbNodejs = require('influxdb-nodejs');

var _influxdbNodejs2 = _interopRequireDefault(_influxdbNodejs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var influxdb = false;
if ((0, _config2.default)('INFLUXDB')) {
    influxdb = new _influxdbNodejs2.default((0, _config2.default)('INFLUXDB'));
}

const onData = data => {
    if (!influxdb) return;
    if (data.RESPONSE_TIME) {
        if (data.ROUTE_ACCESS) {
            data.RESPONSE_TIME.path = data.ROUTE_ACCESS.path;
        }
        influxdb.write('responses').field(data.RESPONSE_TIME).then();
    } else if (data.REQUEST_TIME) {
        influxdb.write('requests').field(data.REQUEST_TIME).then();
    }
};

const routeAccess = router => {
    return (() => {
        var _ref = _asyncToGenerator(function* (ctx, next) {
            const startTime = Date.now();

            try {
                yield next();
            } catch (err) {
                const endTime = Date.now();
                const responseTime = endTime - startTime;
                const matched = router.match(ctx.request.url, ctx.request.method);
                const layer = matched.pathAndMethod[matched.pathAndMethod.length - 1];

                ctx.monitor.ROUTE_ACCESS = {
                    path: layer.path,
                    responseTime
                };
                throw err;
            }

            const endTime = Date.now();
            const responseTime = endTime - startTime;
            const matched = router.match(ctx.request.url, ctx.request.method);
            const layer = matched.pathAndMethod[matched.pathAndMethod.length - 1];

            ctx.monitor.ROUTE_ACCESS = {
                path: layer.path,
                responseTime
            };
        });

        return function (_x, _x2) {
            return _ref.apply(this, arguments);
        };
    })();
};

const responseTime = () => {
    return (() => {
        var _ref2 = _asyncToGenerator(function* (ctx, next) {
            const startTime = Date.now();

            try {
                yield next();
            } catch (err) {
                const endTime = Date.now();
                const responseTime = endTime - startTime;

                ctx.monitor.RESPONSE_TIME = {
                    url: ctx.request.url,
                    method: ctx.request.method,
                    status: 500,
                    responseTime
                };
                throw err;
            }

            const endTime = Date.now();
            const responseTime = endTime - startTime;

            ctx.monitor.RESPONSE_TIME = {
                url: ctx.request.url,
                method: ctx.request.method,
                status: ctx.response.status,
                responseTime
            };
        });

        return function (_x3, _x4) {
            return _ref2.apply(this, arguments);
        };
    })();
};

const requestTime = response => {
    const requestTime = response.config.endTime - response.config.startTime;
    onData({
        REQUEST_TIME: {
            name: response.config.name || null,
            url: response.config.url,
            method: response.config.method,
            status: response.status,
            requestTime
        }
    });
};

const middleware = {
    responseTime,
    routeAccess
};

exports.requestTime = requestTime;
exports.middleware = middleware;

exports.default = () => {
    return (() => {
        var _ref3 = _asyncToGenerator(function* (ctx, next) {
            ctx.monitor = {};
            try {
                yield next();
            } catch (err) {
                onData(ctx.monitor);
                throw err;
            }
        });

        return function (_x5, _x6) {
            return _ref3.apply(this, arguments);
        };
    })();
};
@@ -1577,68 +1577,41 @@
     
-try %7B%0A                yield next();%0A            %7D catch (err
+const setMonitorData = function (
 ) %7B%0A
@@ -2034,412 +2034,192 @@
     
-    throw err;%0A            %7D%0A%0A            const endTime = Date.now();%0A            const responseTime = endTime - startTime;%0A            const matched = router.match(ctx.request.url, ctx.request.method);%0A            const layer = matched.pathAndMethod%5Bmatched.pathAndMethod.length - 1%5D;%0A%0A            ctx.monitor.ROUTE_ACCESS = %7B%0A                path: layer.path,%0A                responseTime%0A            %7D
+%7D;%0A%0A            try %7B%0A                yield next();%0A            %7D catch (err) %7B%0A                setMonitorData();%0A                throw err;%0A            %7D%0A%0A            setMonitorData()
 ;%0A  
@@ -2508,68 +2508,47 @@
     
-try %7B%0A                yield next();%0A            %7D catch (err
+const setMonitorData = function (status
 ) %7B%0A
@@ -2818,11 +2818,37 @@
 us: 
-500
+status %7C%7C ctx.response.status
 ,%0A  
@@ -2913,133 +2913,92 @@
     
-    throw err;%0A            %7D%0A%0A            const endTime = Date.now();%0A            const responseTime = endTime - startTime;%0A%0A
+%7D;%0A%0A            try %7B%0A                yield next();%0A            %7D catch (err) %7B%0A    
     
@@ -3009,207 +3009,99 @@
     
-ctx.m
+setM
 onitor
-.RESPONSE_TIME = %7B%0A                url: ctx.request.url,%0A                method: ctx.request.method,%0A                status: ctx.response.status,%0A                responseTime%0A            %7D
+Data(500);%0A                throw err;%0A            %7D%0A%0A            setMonitorData()
 ;%0A  

monitor 500 errors