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


const durationType = {
    dcl: 'domcontentload',
    ld: 'domreadyload',
    fs: 'firstscreen'
};

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

    async getFirstScreenTimeList(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="${durationType.fs}" ${query} group by route`, queryVal);

        return data;
    }

    async getDomcontentLoadList(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) FROM slow_duration_new where type="${durationType.dcl}" ${query} group by route`, queryVal);

        return data;
    }

    async getDomcontentReadyList(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) FROM slow_duration_new where type="${durationType.ld}" ${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="${durationType.fs}" ${query} group by route`, queryVal);

        return data;
    }
}

module.exports = SortClientRouteModel;