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

class SortApiModel 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 api,avg(duration) as avg FROM slow_duration_new where type="api" ${query} group by api`, 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 api,count(route) as count FROM slow_duration_new where type="api" ${query} group by api`, queryVal);

        return data;
    }

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

module.exports = SortApiModel;