server.js
2.94 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const express = require('express');
const hbs = require('hbs');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const _ = require('lodash');
const checkReport = require('../libs/check-report')
const logger = require('debug')('tester');
const config = require('../config.json');
const app = express();
hbs.registerPartials(__dirname + '/views/partials');
hbs.registerHelper('json', function(context) {
return JSON.stringify(context, null, 2)
});
let currentScene = '';
const sceneData = {};
const dataReport = {};
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/start-scene-case', (req, res) => {
currentScene = req.body.scene;
return res.status(200).end();
});
app.post('/stop-scene-case', (req, res) => {
currentScene = '';
try {
const {notValidates, overLogs} = checkReport(req.body.scene, sceneData);
dataReport[req.body.scene] = {
notValidates, overLogs
}
if (notValidates.length) {
return res.json({
code: 400,
data: {notValidates, overLogs},
message: '验证失败'
})
}
return res.json({
code: 200,
data: {notValidates, overLogs},
message: '验证成功'
})
} catch (error) {
logger(chalk.red('检查埋点报错:', error.message));
return res.json({
code: 400,
data: {notValidates: [], overLogs: []},
message: '验证失败'
})
}
});
app.get('/report', (req, res) => {
const renderData = Object.keys(dataReport).map(scene => {
return {
scene,
status: dataReport[scene].notValidates.length === 0,
notValidates: dataReport[scene].notValidates,
overLogs: dataReport[scene].overLogs
}
})
res.locals = {data: renderData};
res.render('report');
});
app.get('/report-not-validate', (req, res) => {
const scene = req.query.scene;
res.locals = {scene: scene, data: dataReport[scene].notValidates};
res.render('report-not-validate');
});
app.get('/report-over-log', (req, res) => {
const scene = req.query.scene;
res.locals = {scene: scene, data: dataReport[scene].overLogs};
res.render('report-over-log');
});
app.post('/yas_mobile', (req, res) => {
const _mlogs = req.body._mlogs;
if (_mlogs) {
try {
const data = JSON.parse(_mlogs);
if (currentScene) {
if (!sceneData[currentScene]) {
sceneData[currentScene] = [];
}
if (data.events) {
sceneData[currentScene] = sceneData[currentScene].concat(data.events);
}
_.each(data.events, evt => {
logger(chalk.gray(`上报log, ${currentScene}, op: ${evt.op}`));
})
}
} catch (error) {
logger(chalk.red('上报解析错误', error.message));
}
} else {
logger(chalk.red('上报无内容'));
}
res.send('')
});
app.listen(config.port, () => {
logger(chalk.gray('reporter 启动成功'));
})