operation_log.js 1.28 KB
/**
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 16/8/22
 */

'use strict';

import Router from 'koa-router';
import moment from 'moment';

import {
    OperationLogger
} from '../../models';

const r = new Router();

r.get('/log', async(ctx) => {
    await ctx.render('action/operation_log');
});

r.post('/log/query', async(ctx) => {

    let q = ctx.request.body;

    let start = parseInt(q.start || 0, 10);
    let length = parseInt(q.length || 10, 10);
    let draw = parseInt(q.draw, 10);

    let sort = {time: -1};
    if (q.order && q.order.length > 0) {
        let col = q.order[0].column;
        let dir = q.order[0].dir === 'desc' ? -1 : 1;

        col = parseInt(col, 10);
        if (q.columns && q.columns.length > col) {
            sort = {};
            sort[q.columns[col].data] = dir;
        }
    }

    let logs = await OperationLogger.cfind({}).sort(sort).skip(start).limit(length).exec();
    let total = await OperationLogger.count();

    logs.forEach(l => {
        l.username = l.user.username;
        l.time = moment(l.time).format('YYYY-MM-DD HH:mm:ss.sss');
        l.meta = JSON.stringify(l.meta || {});
    });

    ctx.body = {
        draw: draw,
        recordsTotal: total,
        recordsFiltered: total,
        data: logs
    };

});

export default r;