errorModel.js
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;