-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
371 lines (278 loc) · 10.2 KB
/
app.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var mysql = require('mysql');
var app = express();
//setup connetion to db here!!
function getMySQLConnection() {
return mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'telemetry',
multipleStatements: true
});
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//map /plugin to static html page
app.use('/plugin', express.static(path.join(__dirname, 'plugin')));
app.use('/', index);
app.get('/streamInfo', function(req, res) {
var streamInfoList = [];
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM stream_info', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Loop check on each row
for (var i = 0; i < rows.length; i++) {
// Create an object to save current row's data
var streamInfo = {
'id':rows[i].id,
'streamId':rows[i].streamId,
'manifestUrl':rows[i].manifestUrl,
'protocol':rows[i].protocol,
'availableVidBitrates': rows[i].availableVidBitrates,
'availableAudBitrates': rows[i].availableAudBitrates,
'availableSubs': rows[i].availableSubs,
'isLive': rows[i].isLive
}
// Add object into array
streamInfoList.push(streamInfo);
}
// Render index.pug page using array
res.render('streaminfo', {"streamInfoList": streamInfoList});
}
});
// Close the MySQL connection
connection.end();
});
app.get('/streamHistory', function(req, res) {
var streamHistory = [];
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM stream_history', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Render index.pug page using array
res.render('streamhistory', {"streamHistoryList": rows});
}
});
// Close the MySQL connection
connection.end();
});
app.get('/playerEvents', function(req, res) {
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM player_events', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Render index.pug page using array
res.render('playerevents', {"playerEvents": rows});
}
});
// Close the MySQL connection
connection.end();
});
app.get('/bitrateChanges', function(req, res) {
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM bitrate_changes', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Render index.pug page using array
res.render('bitratechanges', {"bitrateChanges": rows});
}
});
// Close the MySQL connection
connection.end();
});
app.get('/playerStatistics', function(req, res) {
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM player_statistics', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Render index.pug page using array
res.render('playerstatistics', {"playerStatistics": rows});
}
});
// Close the MySQL connection
connection.end();
});
app.get('/playerErrors', function(req, res) {
// Connect to MySQL database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT * FROM player_errors', function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Render index.pug page using array
res.render('playererrors', {"playerErrors": rows});
}
});
// Close the MySQL connection
connection.end();
});
app.post('/saveTelemetry', function(req,res){
var connection = getMySQLConnection();
connection.connect();
var sql ="";
//sql for stream info
var sqlStream = "INSERT INTO stream_info (streamId, manifestUrl, protocol, availableVidBitrates, availableAudBitrates, availableSubs, isLive) VALUES ?";
var streamInfo = req.body.streamInfo;
//saving player events first
var sqlPlayerEvents = "INSERT INTO player_events (streamId, eventType, time) VALUES ?";
var playerEvents = req.body.playerEvents;
//create nested array for multiple insert
var playerEventsVals = new Array();
for (var i = 0; i < playerEvents.length; i++) {
var itm = playerEvents[i];
//grab values manually, js cant assure obj keys order
playerEventsVals.push([streamInfo.streamId, itm.evTpe, itm.timeStamp]);
}
var avVidBitrates = streamInfo.availableVidBitrates;
//saving stream info
//concatenate values from available bitrates
//TODO: save in this in different table, use join for display
var vBitRatesString = "";
if (avVidBitrates.length > 0) {
for (var i = 0; i < avVidBitrates.length; i++) {
//{"bitrate":1490441,"height":540,"width":960}
var itm = avVidBitrates[i];
vBitRatesString += "bitrate:"+itm.bitrate + ", h:"+ itm.height + ", w:" + itm.width + ";";
}
}
var streamInfoArr = [streamInfo.streamId,streamInfo.manifestUrl, streamInfo.protocol, vBitRatesString, streamInfo.availableAudBitrates.toString(), streamInfo.availableSubs.toString(), streamInfo.isLive];
//sql for stream history
var sqlStreamHistory = "INSERT INTO stream_history (streamId, bitrate, bitrateType, downloadedFragments, failedFragments, bytesDownloaded) VALUES ?";
var strHistory= req.body.streamHistory;
var streamHistorysArray = [];
//constructuing array of values of each bitrate history
//again grab values manually to assure the right order
//bitrates were grouped by type
for (var brType in strHistory) {
var obj = strHistory[brType];
for (var key in obj){
if (obj.hasOwnProperty(key)) {
var bitrate = key;
var bitrateType = brType;
var downloadedFragments = obj[key]['fragmentsDownloaded'];
var failedFragments = obj[key]['fragmentsFailed'];
var bytesDownloaded = obj[key]['bytesDownloaded'];
var valsAsArray = [streamInfo.streamId, bitrate, bitrateType, downloadedFragments, failedFragments, bytesDownloaded];
streamHistorysArray.push(valsAsArray);
}
}
}
//prepare players stats for db
var sqlPlayerStats = "INSERT INTO player_statistics (streamId, timeBuffering, averageBuffer) VALUES ?";
var playerStats = req.body.playerStatistics;
var playerStatsAsArray = [streamInfo.streamId, playerStats.timeBuffering, playerStats.avgBufferAvailable];
console.log("playerStats", playerStatsAsArray);
//prepaere player errors for db
var playerErrors = req.body.playerErrors;
var sqlPlayerErrors = "";
var playerErrorsVals = new Array();
//create nested array for multiple insert
if (playerErrors.length > 0) {
sqlPlayerErrors = "INSERT INTO player_errors (streamId, errorCode, time) VALUES ?";
for (var i = 0; i < playerErrors.length; i++) {
var itm = playerErrors[i];
//grab values manually, js cant assure obj keys order
playerErrorsVals.push([streamInfo.streamId, itm.errorCode, itm.timeStamp]);
}
}
var bitrateChanged = req.body.bitrateChangeEvts;
var sqlBrChanged = "";
var brChangedVals = new Array();
//create nested array for multiple insert
if (bitrateChanged.length > 0) {
sqlBrChanged = "INSERT INTO bitrate_changes (streamId, type, time) VALUES ?";
for (var i = 0; i < bitrateChanged.length; i++) {
var itm = bitrateChanged[i];
//grab values manually, js cant assure obj keys order
brChangedVals.push([streamInfo.streamId, itm.type, itm.time]);
}
}
//constructing final sql + nested array for values, prevent errors if some arrays are empty
var valsToInsert = new Array();
var sql = "";
//adding stream info
sql += sqlStream + "; ";
valsToInsert.push([streamInfoArr]);
//adding player events if they exist
if (playerEventsVals.length > 0) {
sql += sqlPlayerEvents + "; ";
valsToInsert.push(playerEventsVals);
}
//adding stream info history
if (streamHistorysArray.length > 0) {
sql += sqlStreamHistory + "; ";
valsToInsert.push(streamHistorysArray);
}
sql += sqlPlayerStats + "; ";
valsToInsert.push([playerStatsAsArray]);
//adding bitrate change
if (brChangedVals.length > 0) {
sql += sqlBrChanged + "; ";
valsToInsert.push(brChangedVals);
}
//adding errors
if (playerErrorsVals.length > 0) {
sql += sqlPlayerErrors + "; ";
valsToInsert.push(playerErrorsVals);
}
connection.query(sql, valsToInsert, function(err) {
if (err) res.status(500).json({"status_code": 500,"status_message": "internal server error"});
connection.end();
res.json({success : "Saved Successfully", status : 200});
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;