-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.js
345 lines (297 loc) · 11.7 KB
/
util.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
/*global $, moment, GLOBE, jsSHA */
/*eslint new-cap: 0 */
GLOBE.Util = {
/**
* Checks if a given string is a 40 char hex string
* @param {String} string String to check
* @returns {Boolean} If the checked string is a 40 char hex
*/
is40CharHex: function(string){
var hex40CharRegex = /^[a-f0-9]{40}/i,
result;
result = string.match(hex40CharRegex);
return result !== null;
},
/**
* Creates an sha1 hashed string based on a given fingerprint.
* @see {@link https://trac.torproject.org/projects/tor/ticket/6320#comment:1}
* @param {String} fingerprint
* @returns {String} hashed fingerprint
*/
hashFingerprint: function(fingerprint){
var fingerBin = new jsSHA(fingerprint, 'HEX'),
hashed = fingerBin.getHash('SHA-1', 'HEX');
return hashed.toUpperCase();
},
/**
* Calculates the difference from now to a given utc time.
* @param {String} value UTC Timestamp
* @returns {{h: Number, m: Number, s: Number, d: Number}} hour, minute, second, day
*/
UtcDiff: function(value){
var momentDate = moment.utc(value, 'YYYY-MM-DD HH:mm:ss'),
diff,
// default result
result = {
h:-1,
m:-1,
s:-1,
d:-1
},
fl = Math.floor;
if (momentDate.isValid()) {
diff = moment.utc().diff(momentDate);
result.s = Math.round(diff / 1000);
result.m = fl(result.s / 60);
result.h = fl(result.m / 60);
result.d = fl(result.h / 24);
result.s %= 60;
result.m %= 60;
result.h %= 24;
}
return result;
},
/**
* Calculates the uptime (Time difference between now and given timestamp) from a UTC-timestamp.
* Result is an array with the first 2 full time units.
*
* @param {String} value UTC-Timestamp
* @param {String} type "short" or something else
* @returns {Array} first 2 full time units from uptime
* @example
* if uptime < days the function returns [ hours, minutes ]
*/
UptimeCalculator: function(value, type){
// if not a valid length return empty data message
if (value.length !== 19) {
return [GLOBE.static.messages.dataEmpty];
}
var beforeUnit = '<span>',
afterUnit = '</span>';
var diff = GLOBE.Util.UtcDiff(value),
units = [diff.d, diff.h, diff.m, diff.s],
digits = 0,
shortVersion = type === 'short',
pluralize = !shortVersion,
labels = shortVersion ? ['d', 'h', 'm', 's'] : ['day', 'hour', 'minute', 'second'],
uptimeArray = [];
for(var i = 0, max = units.length; i < max; i++){
if(labels[i] && labels[i].length && units[i] > 0){
digits += 1;
uptimeArray[i] = units[i] + beforeUnit + (pluralize && units[i] > 1 ? labels[i] + 's' : labels[i]) + afterUnit;
if(digits > 1){
break;
}
}else{
labels[i] = '';
}
}
return uptimeArray;
},
/**
* Converts UTC-Timestamp ( YYYY-MM-DD hh:mm:ss ) to JavaScript Date-Object.
* @param {String} timestamp UTC-Timestamp
* @returns {Date} converted date Object
* @throws {String} will throw an error if the parsed timestamp is invalid
*/
utcToDate: function(timestamp){
var timeMoment = moment.utc(timestamp, 'YYYY-MM-DD HH:mm:ss');
if (!timeMoment.isValid()) {
throw 'Are you sure this is a UTC timestamp? expected: YYYY-MM-DD hh:mm:ss got:' + timestamp;
}
return timeMoment.toDate();
},
/**
* Generates history data
* @param {Object} historyObject
* @throws {String} throws an error if there is no interval or there is something wrong with start and end date
* @returns {*}
*/
buildTimeValuePairs: function(historyObject){
if(historyObject.first && historyObject.last && historyObject.interval){
var startDate = this.utcToDate(historyObject.first),
endDate = this.utcToDate(historyObject.last);
// check if Date creation was successfull
if(!isNaN(startDate.getTime()) && !isNaN(endDate.getTime())){
// everything worked
var sum = 0,
newValues = [],
values = historyObject.values,
// interval is in seconds, multiply 1000 to get millisecs
interval = historyObject.interval * 1000,
currentTime = startDate.getTime();
for(var i = 0, max = values.length; i < max; i++){
var realValue = values[i] * historyObject.factor;
newValues.push([
currentTime,
realValue
]);
sum += realValue;
currentTime += interval;
}
historyObject.avg = (sum / values.length);
historyObject.values = newValues;
}else{
throw 'There was an error parsing the history object timestamps. Check if ' + historyObject.first + ' or ' + historyObject.last + ' are correct.';
}
}else{
throw 'Cannot generate time value pairs if there is no time interval given';
}
return historyObject;
},
/**
*
* @param history
* @param {Object} toBuild
* @returns {Array}
*/
prepareHistoryItems: function(history, toBuild){
var periods = [];
for (var build in toBuild) {
if(toBuild.hasOwnProperty(build)){
var buildHistory = toBuild[build];
for (var buildKey in buildHistory) {
if (buildHistory.hasOwnProperty(buildKey)) {
// push buildKey to periods if not already set
if ($.inArray(buildKey ,periods) === -1) {
periods.push(buildKey);
}
var keyObj = $.extend({}, GLOBE.defaults.History, buildHistory[buildKey]);
history[build][buildKey] = GLOBE.Util.buildTimeValuePairs(keyObj);
}
}
}
}
return periods;
},
/**
* Function that takes a ip address and decides if it could be a ipv6 or ipv4 address.
* Do not use this as validation for ip addresses.
* @param {String} address
* @return {undefined|String} 6, 4 or undefined (if address is no string).
*/
looksLikeIpV: function(address) {
var looksLike,
v6Result,
v4Result;
if (typeof address === 'string') {
// I used an assignment with boolean check because .match can return null
if ((v6Result = address.match(/:/g)) && v6Result.length > 1) {
looksLike = '6';
} else if ((v4Result = address.match(/\./g)) && v4Result.length === 3) {
looksLike = '4';
}
}
return looksLike;
},
processHistoryResponse: function(fieldMapping, response){
var hasRelays = response && response.relays && response.relays.length,
hasBridges = response && response.bridges && response.bridges.length,
relays = {
history: {},
periods: []
},
bridges = {
history: {},
periods: []
},
relayToBuild = {},
bridgeToBuild = {},
relay = hasRelays ? response.relays[0] : undefined,
bridge = hasBridges ? response.bridges[0] : undefined;
if (hasRelays || hasBridges) {
for (var field in fieldMapping) {
if (fieldMapping.hasOwnProperty(field)) {
if (hasRelays) {
relays.history[field] = {};
relayToBuild[field] = relay[fieldMapping[field]];
}
if (hasBridges) {
bridges.history[field] = {};
bridgeToBuild[field] = bridge[fieldMapping[field]];
}
}
}
if (hasRelays) {
relays.periods = GLOBE.Util.prepareHistoryItems(relays.history, relayToBuild);
}
if (hasBridges) {
bridges.periods = GLOBE.Util.prepareHistoryItems(bridges.history, bridgeToBuild);
}
}
return {
relays: relays,
bridges: bridges
};
},
/**
* This is a wrapper that calls historyValuesFromNowUntil with specific values.
* It computed a 3_days field using the 1_week values and 3 days ago.
* @param processedHistoryResponse
*/
compute3DaysHistory: function(processedHistoryResponse) {
var bridges = processedHistoryResponse.bridges,
relays = processedHistoryResponse.relays;
// compute 3_days period from 1_week
if (bridges && bridges.periods.length) {
// compute bridges 3_days
GLOBE.Util.historyValuesFromNowUntil({
history: bridges.history,
timeAgo: GLOBE.static.numbers.DAY * 3,
sourceField: '1_week',
destField: '3_days'
});
// add 3_days to periods array
processedHistoryResponse.bridges.periods.unshift('3_days');
}
if (processedHistoryResponse.relays && processedHistoryResponse.relays.periods.length) {
// compute relays 3_days
GLOBE.Util.historyValuesFromNowUntil({
history: relays.history,
timeAgo: GLOBE.static.numbers.DAY * 3,
sourceField: '1_week',
destField: '3_days'
});
// add 3_days to periods array
processedHistoryResponse.relays.periods.unshift('3_days');
}
return processedHistoryResponse;
},
historyValuesFromNowUntil: function(cfg){
var history = cfg.history,
timeAgo = cfg.timeAgo,
source = cfg.sourceField,
dest = cfg.destField;
Object.keys(history).forEach(function(historyField){
if (history[historyField][source]) {
// get first timestamp
var sum = 0,
earliestValue = Infinity,
sourceValues = history[historyField][source].values,
// get youngest dataset from source
now = moment.utc(),
timeFromComputedNowAgo = now - timeAgo,
filteredSourceValues = sourceValues.filter(function(valuePair){
if (valuePair[0] > timeFromComputedNowAgo) {
if (valuePair[0] < earliestValue){
earliestValue = valuePair[0];
}
sum += valuePair[1];
return true;
}
});
// cut > 3 days from values array
history[historyField][dest] = {
first: earliestValue,
last: now,
values: filteredSourceValues,
avg: sum / filteredSourceValues.length
};
}
});
},
nowMinusPeriod: function(period){
var periodObject = GLOBE.static.periodObject[period];
return moment.utc().subtract(periodObject[0], periodObject[1]);
}
};