errorModel.js 3.17 KB
const model = require('../../../lib/model');
const mysqlPromise = require('../../../lib/mysql-apm');

class errorModel extends model {
    constructor(ctx) {
        super(ctx);
        this.mysql = new mysqlPromise();
    }

    async getList(app, host, type, time, code, api, route ,start, length) {
        length = length || 10;

        let query = '';
        const queryVal = [];

        if (app) {
            query += 'and app = ?';
            queryVal.push(app);
        }

        if (host) {
            query += 'and hostname = ?';
            queryVal.push(host);
        }

        if (type) {
            query += 'and type = ?';
            queryVal.push(type);
        }

        if (time) {
            let [start,end] = time.split(':');
            query += 'and create_time > ? and create_time < ?';
            queryVal.push(start);
            queryVal.push(end);
        }

        if (code) {
            query += 'and code = ?';
            queryVal.push(code);
        }

        if (api) {
            query += 'and api = ?';
            queryVal.push(api);
        }

        if (route) {
            query += 'and route = ?';
            queryVal.push(route);
        }

        const fields = 'SELECT id, app, type, preqid, reqid, uid, udid, code, route, api, line, `column` ,script, message, stack, useragent, create_time as time, hostname, "detail"';

        let recordsTotal = await this.mysql.query(`SELECT COUNT(*) as count from error_report where 1=1 ${query}`, queryVal).then(([r]) => r.count);
        let data = await this.mysql.query(`${fields} from error_report where 1=1 ${query} order by create_time desc limit ?, ? `, [...queryVal, start, length]);

        return {
            recordsTotal,
            recordsFiltered: recordsTotal,
            data
        }
    }

    async getApp() {
        return await this.mysql.query('SELECT DISTINCT app FROM error_report');
    }

    async getHost() {
        return await this.mysql.query('SELECT DISTINCT hostname FROM error_report');
    }

    async getType() {
        return await this.mysql.query('SELECT DISTINCT type FROM error_report');
    }

    async getCode() {
        return await this.mysql.query('SELECT DISTINCT code FROM error_report');
    }

    async getRoute() {
        return await this.mysql.query('SELECT DISTINCT route FROM error_report');
    }

    async getApi() {
        return await this.mysql.query('SELECT DISTINCT api FROM error_report');
    }

    getClientDayCount(app, start, end) {
        return this.mysql.query(`SELECT COUNT(*) as count FROM error_report where app = ? and create_time >= ? and create_time < ? and type = ?`, [app, start, end, 'client']).then(([r]) => r.count)
    }

    getServerDayCount(app, start, end) {
        return this.mysql.query(`SELECT COUNT(*) as count FROM error_report where app = ? and create_time >= ? and create_time < ? and type = ?`, [app, start, end, 'server']).then(([r]) => r.count);
    }

    getApiDayCount(app, start, end) {
        return this.mysql.query(`SELECT COUNT(*) as count FROM error_report where app = ? and create_time >= ? and create_time < ? and type = ?`, [app, start, end, 'api']).then(([r]) => r.count);
    }
}

module.exports = errorModel;