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

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

    async getTimeList(app, time) {
        let query = '';
        let queryVal = [];

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

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

        let data = await this.mysql.query(`SELECT route,avg(duration) as avg FROM slow_duration_new where type="route" ${query} group by route`, queryVal);

        return data;
    }

    async getCountList(app, time) {
        let query = '';
        let queryVal = [];

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

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

        let data = await this.mysql.query(`SELECT route,count(route) as count FROM slow_duration_new where type="route" ${query} group by route`, queryVal);

        return data;
    }

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

module.exports = SortRouteModel;