-
Notifications
You must be signed in to change notification settings - Fork 0
/
VnStatListener.js
238 lines (193 loc) · 7.46 KB
/
VnStatListener.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
import Moment from 'moment';
import ValidateJs, * as RequestConstraints from './validation';
import { parse as urlParse } from 'url';
import { exec as processExec } from 'child_process';
import jsonBody from 'body/json';
const DEFAULT_PATH_PREFIX = '/vnstat';
export default class VnStatListener {
constructor(apiPath) {
apiPath = apiPath || DEFAULT_PATH_PREFIX;
while (apiPath.endsWith('/')) {
apiPath = apiPath.substring(0, apiPath.length - 1);
}
if (!apiPath) {
apiPath = DEFAULT_PATH_PREFIX;
}
this.apiPath = apiPath;
return this.requestListener();
}
requestListener() {
return (req, res) => {
let urlObj = urlParse(req.url);
if (req.requestHandled === true || (urlObj.pathname !== this.apiPath && !urlObj.pathname.startsWith(`${this.apiPath}/`))) {
// we won't be handling this request at all
return;
}
// claim ownership of request ASAP, downstream requestListeners will know to ignore
req.requestHandled = true;
let validationConstraints;
let momentUnitOfTime;
let momentFormatStr;
let vnStatMode;
let vnStatUnitOfTime;
let datePrecision;
switch (urlObj.pathname) {
case `${this.apiPath}/years`:
validationConstraints = RequestConstraints.yearConstraints;
momentUnitOfTime = 'year';
momentFormatStr = 'YYYY-MM-DD';
// vnstat doesn't technically have years, we just use months and reduce down
vnStatMode = 'm';
vnStatUnitOfTime = 'months';
datePrecision = 'Year';
executeVnStatRequest(req, res, validationConstraints, momentUnitOfTime, momentFormatStr, vnStatMode, vnStatUnitOfTime, datePrecision);
break;
case `${this.apiPath}/months`:
validationConstraints = RequestConstraints.monthConstraints;
momentUnitOfTime = 'month';
momentFormatStr = 'YYYY-MM-DD';
vnStatMode = 'm';
vnStatUnitOfTime = 'months';
datePrecision = 'Month';
executeVnStatRequest(req, res, validationConstraints, momentUnitOfTime, momentFormatStr, vnStatMode, vnStatUnitOfTime, datePrecision);
break;
case `${this.apiPath}/days`:
validationConstraints = RequestConstraints.dayConstraints;
momentUnitOfTime = 'day';
momentFormatStr = 'YYYY-MM-DD';
vnStatMode = 'd';
vnStatUnitOfTime = 'days';
datePrecision = 'Day';
executeVnStatRequest(req, res, validationConstraints, momentUnitOfTime, momentFormatStr, vnStatMode, vnStatUnitOfTime, datePrecision);
break;
case `${this.apiPath}/hours`:
validationConstraints = RequestConstraints.hourConstraints;
momentUnitOfTime = 'hour';
momentFormatStr = 'YYYY-MM-DD[T]HH:00:00[Z]';
vnStatMode = 'h';
vnStatUnitOfTime = 'hours';
datePrecision = 'Hour';
executeVnStatRequest(req, res, validationConstraints, momentUnitOfTime, momentFormatStr, vnStatMode, vnStatUnitOfTime, datePrecision);
break;
default:
// because we are taking full responsibility of handling request, we will return 404
res.statusCode = 404;
res.end();
break;
}
};
}
}
function executeVnStatRequest(req, res, validationConstraints, momentUnitOfTime, momentFormatStr, vnStatMode, vnStatUnitOfTime, datePrecision) {
jsonBody(req, res, (err, body) => {
if (err) {
clientErrorResponse(res, err);
return;
}
let validateResult = ValidateJs.validate(body, validationConstraints);
if (validateResult) {
clientErrorResponse(res, validateResult);
return;
}
let startDate = Moment.utc(body['startDate']).startOf(momentUnitOfTime);
let stopDate = null;
let isRange = false;
if (body['stopDate']) {
// stopDate isn't always required
stopDate = Moment.utc(body['stopDate']).startOf(momentUnitOfTime);
isRange = true;
}
let processOptions = {};
processExec(`vnstat --json ${vnStatMode}`, processOptions, (err, stdout, _stderr) => {
if (err) {
serverErrorResponse(res, err);
return;
}
let vnStatJson = JSON.parse(stdout);
// insert moment objects into individual parts of arr (easier filtering/sorting later)
vnStatJson['interfaces'].forEach((intf) => {
intf['traffic'][vnStatUnitOfTime].forEach((trafficData) => {
let date = Object.assign({}, trafficData['date']);
// Moment uses zero ordered months
date['month']--;
// also if we are utilizing hours, the hour is the id key
if (vnStatUnitOfTime === 'hours') {
date['hour'] = trafficData['id'];
}
trafficData['moment'] = Moment.utc(date);
});
});
let usedTrafficData;
if (isRange) {
usedTrafficData = vnStatJson['interfaces'].reduce((intfAcc, intf) => {
intf['traffic'][vnStatUnitOfTime]
.filter((trafficData) => trafficData['moment'].isBetween(startDate, stopDate, momentUnitOfTime, '[)'))
.sort((a, b) => a['moment'].diff(b['moment']))
.map(({ rx, tx, moment }) => {
return { rx, tx, date: moment.startOf(momentUnitOfTime).format(momentFormatStr) };
})
.forEach(({ rx, tx, date }) => {
if (intfAcc[date]) {
// accumulate data between interfaces on same date (key)
intfAcc[date].rx += rx;
intfAcc[date].tx += tx;
} else {
// set new trafficData
intfAcc[date] = { rx, tx };
}
});
return intfAcc;
}, {});
} else {
usedTrafficData = vnStatJson['interfaces'].reduce((intfAcc, intf) => {
let usedData = intf['traffic'][vnStatUnitOfTime]
.filter((trafficData) => trafficData['moment'].isSame(startDate, momentUnitOfTime))
.map(({ rx, tx }) => {
return { rx, tx };
})
.reduce((acc, cur) => {
acc.rx += cur.rx;
acc.tx += cur.tx;
return acc;
}, { rx: 0, tx: 0 });
let date = startDate.format(momentFormatStr);
if (intfAcc[date]) {
// accumulate data between interfaces on same date
intfAcc[date].rx += usedData.rx;
intfAcc[date].tx += usedData.tx;
} else {
// set new trafficData
intfAcc[date] = usedData;
}
return intfAcc;
}, {});
}
let response = {
traffic: usedTrafficData,
precision: datePrecision,
startDate: startDate.format(momentFormatStr)
};
if (isRange) {
response.stopDate = stopDate.format(momentFormatStr);
}
successResponse(res, response);
});
});
}
function successResponse(res, payload) {
rawResponse(res, 200, payload);
}
function clientErrorResponse(res, error) {
rawResponse(res, 400, error);
}
function serverErrorResponse(res, error) {
rawResponse(res, 500, error);
}
function rawResponse(res, statusCode, payload) {
res.statusCode = statusCode;
res.setHeader('Content-Type', 'application/json');
let body = typeof payload === 'string'
? payload
: JSON.stringify(payload);
res.end(body);
}