forked from fippo/rtcstats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.js
More file actions
163 lines (153 loc) · 6.7 KB
/
extract.js
File metadata and controls
163 lines (153 loc) · 6.7 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
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
const fs = require('fs');
const config = require('config');
const canUseProcessSend = !!process.send;
const isProduction = process.env.NODE_ENV && process.env.NODE_ENV === 'production';
function capitalize(str) {
return str[0].toUpperCase() + str.substr(1);
}
function safeFeature(feature) {
if (typeof feature === 'number' && isNaN(feature)) feature = -1;
if (typeof feature === 'number' && !isFinite(feature)) feature = -2;
if (feature === false) feature = 0;
if (feature === true) feature = 1;
return feature;
}
const features = require('./features');
const statsDecompressor = require('./getstats-deltacompression').decompress;
const statsMangler = require('./getstats-mangle');
// dumps all peerconnections.
function dump(url, client, clientid, data) {
// ignore connections that never send getUserMedia or peerconnection events.
if (client.getUserMedia.length === 0 && Object.keys(client.peerConnections).length === 0) return;
if (!isProduction) {
let total = 0;
Object.keys(client.peerConnections).forEach(id => {
total += client.peerConnections[id].length;
});
console.log('DUMP', client.getUserMedia.length, Object.keys(client.peerConnections).length, total);
return;
}
}
// Feature generation
function generateFeatures(url, client, clientid) {
// ignore connections that never send getUserMedia or peerconnection events.
if (client.getUserMedia.length === 0 && Object.keys(client.peerConnections).length === 0) return;
// clientFeatures are the same for all peerconnections but are saved together
// with each peerconnection anyway to make correlation easier.
const clientFeatures = {};
Object.keys(features).forEach(fname => {
if (features[fname].length === 1) {
let feature = features[fname].apply(null, [client]);
if (feature !== undefined) {
if (typeof feature === 'object') {
Object.keys(feature).forEach(subname => {
feature[subname] = safeFeature(feature[subname]);
if (!isProduction) {
console.log('PAGE', 'FEATURE', fname + capitalize(subname), '=>', safeFeature(feature[subname]));
}
clientFeatures[fname + capitalize(subname)] = feature[subname];
});
} else {
feature = safeFeature(feature);
if (!isProduction) {
console.log('PAGE', 'FEATURE', fname, '=>', feature);
}
clientFeatures[fname] = feature;
}
}
}
});
Object.keys(client.peerConnections).forEach(connid => {
if (connid === 'null') return; // ignore the null connid
const conn = client.peerConnections[connid];
const connectionFeatures = {};
Object.keys(features).forEach(fname => {
if (features[fname].length === 2) {
let feature = features[fname].apply(null, [client, conn]);
if (feature !== undefined) {
if (typeof feature === 'object' && Object.keys(feature || {}).length > 0) {
Object.keys(feature).forEach(subname => {
feature[subname] = safeFeature(feature[subname]);
if (!isProduction) {
console.log(connid, 'FEATURE', fname + capitalize(subname), '=>', safeFeature(feature[subname]));
}
connectionFeatures[fname + capitalize(subname)] = feature[subname];
});
} else {
feature = safeFeature(feature);
if (!isProduction) {
console.log(connid, 'FEATURE', fname, '=>', safeFeature(feature));
}
connectionFeatures[fname] = feature;
}
}
}
});
delete client.peerConnections[connid]; // save memory
if (!isProduction) return;
if (canUseProcessSend) {
process.send({url, clientid, connid, clientFeatures, connectionFeatures});
} else {
console.log(url, clientid, connid, clientFeatures, connectionFeatures);
}
});
}
var clientid = process.argv[2];
const path = 'temp/' + clientid;
fs.readFile(path, {encoding: 'utf-8'}, (err, data) => {
if (!err) {
const baseStats = {};
const lines = data.split('\n');
const client = JSON.parse(lines.shift());
client.peerConnections = {};
client.getUserMedia = [];
lines.forEach(line => {
if (line.length) {
const data = JSON.parse(line);
const time = new Date(data.time || data[3]);
delete data.time;
switch(data[0]) {
case 'location':
client.location = data[2];
break;
case 'userfeedback': // TODO: might be renamed
client.feedback = data[2];
break;
case 'getUserMedia':
case 'getUserMediaOnSuccess':
case 'getUserMediaOnFailure':
case 'navigator.mediaDevices.getUserMedia':
case 'navigator.mediaDevices.getUserMediaOnSuccess':
case 'navigator.mediaDevices.getUserMediaOnFailure':
client.getUserMedia.push({
time: time,
type: data[0],
value: data[2]
});
break;
default:
if (!client.peerConnections[data[1]]) {
client.peerConnections[data[1]] = [];
baseStats[data[1]] = {};
}
if (data[0] === 'getstats') { // delta-compressed
data[2] = statsDecompressor(baseStats[data[1]], data[2]);
baseStats[data[1]] = JSON.parse(JSON.stringify(data[2]));
}
if (data[0] === 'getStats' || data[0] === 'getstats') {
data[2] = statsMangler(data[2]);
data[0] = 'getStats';
}
client.peerConnections[data[1]].push({
time: time,
type: data[0],
value: data[2]
});
break;
}
}
});
dump(client.url, client, clientid, data);
generateFeatures(client.url, client, clientid);
}
});