logs.js 1.56 KB
'use strict';
// const capi = new Capi({
//     SecretId: 'AKIDKXZR8e2JNGhy9AogEdZKuJXaSAot7X7W',
//     SecretKey: 'Mhuw11qY5KPK5fBYxDHAsQnzzpU4Xqzx',
//     serviceType: 'logset'
// });
const _ = require('lodash');
const moment = require('moment');
const rp = require('request-promise');
const Model = require('./model');
const qcloud = require('../../lib/qcloud');


let _convert = res => {
    res = JSON.parse(res);
    
    let message;
    _.each(res.results, (item, idx) => {
        message = JSON.parse(JSON.parse(item.content)['__CONTENT__'].message);
        res.results[idx].message = message.message;
        res.results[idx].level = message.level;
        // res.results[idx].t = moment(message.timestamp).format('YYYY-MM-DD HH:mm:ss');
        delete res.results[idx].content;

    });
    return res;
}; 

class Logs extends Model {
    constructor() {
        super('logs');
    }

    async init({query = '', limit = 10}) {
        const res = await rp(qcloud.sign({
            method: 'get',
            baseUrl: 'http://ap-beijing.cls.myqcloud.com',
            uri: '/searchlog',
            qs: {
                logset_id: '3a292186-9241-4d2c-98de-a13896ed7b25',
                topic_ids: '3cb90ed6-0f92-40cb-b064-42f36d13b11a',
                start_time: moment(Date.now() - 3600 * 1000 * 48).format('YYYY-MM-DD HH:mm:ss'),
                end_time: moment().format('YYYY-MM-DD HH:mm:ss'),
                query,
                limit
            },
            headers: {}
        }, {query, limit}));
        
        return _convert(res)
    }
}

module.exports = Logs;