-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTimeController.js
executable file
·449 lines (329 loc) · 15.8 KB
/
TimeController.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* Copyright (c) 2013 RIPE NCC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
"dnsmon.env.utils",
"dnsmon.controller.gesture-manager"
], function(utils, GesturesManager){
/**
* TimeController provides all the functions to manage the time in the tool.
*
* @class TimeController
* @constructor
* @module controller
*/
var TimeController = function(env){
var gesturesManager, config, subMinutes, addMinutes, updateTimer;
config = env.config;
gesturesManager = new GesturesManager(env);
addMinutes = utils.addMinutes;
subMinutes = utils.subMinutes;
this.init = function(){
gesturesManager.on('scrollRight', this.shiftRight, this);
gesturesManager.on('scrollLeft', this.shiftLeft, this);
gesturesManager.on('scrollUp', this.zoomOut, this);
gesturesManager.on('scrollDown', this.zoomIn, this);
};
this.isSelectionTooBig = function(startDate, endDate){
return this._getAggregationLevel(startDate, endDate) > env.maxAggregation;
};
/**
* Checks if it is possible to zoom-out with the current selection
*
* @method _isZoomableOut
* @private
* @param {Date} startDate The start date of the new selection
* @param {Date} endDate The end date of the new selection
* @return {Boolean} True if the zoom-out is applicable
*/
this._isZoomableOut = function(startDate, endDate){
var aggregationLevel;
aggregationLevel = this._getAggregationLevel(startDate, endDate);
return (aggregationLevel <= env.maxAggregation && startDate >= env.measurementStartTime && endDate <= env.measurementEndTime);
};
/**
* Checks if it is possible to zoom-in with the current selection
*
* @method _isZoomableIn
* @private
* @param {Date} startDate The start date of the new selection
* @param {Date} endDate The end date of the new selection
* @return {Boolean} True if the zoom-in is applicable
*/
this._isZoomableIn = function(startDate, endDate){
var aggregationLevel, virtualZoom;
aggregationLevel = this._getAggregationLevel(startDate, endDate);
virtualZoom = (aggregationLevel > (env.minAggregation * config.virtualZoomFactor));
return virtualZoom;
};
/**
* Checks if it is possible to shift left the time window
*
* @method _isTranslableLeft
* @private
* @param {Date} startDate The start date of the new selection
* @return {Boolean} True if the shift is applicable
*/
this._isTranslableLeft = function(startDate){
return (startDate >= env.measurementStartTime);
};
/**
* Checks if it is possible to shift right the time window
*
* @method _isTranslableRight
* @private
* @param {Date} endDate The end date of the new selection
* @return {Boolean} True if the shift is applicable
*/
this._isTranslableRight = function(endDate){
return (endDate <= env.measurementEndTime);
};
this.getMaxNumberOfCells = function(){
return env.container.chart.width() / (config.cellsMinWidth + config.xCellsMargin);
};
this._getAggregationLevel = function(startDate, endDate){
var aggregationSeconds, timeInterval;
timeInterval = Math.floor((endDate - startDate) / 1000);
aggregationSeconds = timeInterval / this.getMaxNumberOfCells();
return (aggregationSeconds > 0) ? parseFloat(aggregationSeconds.toFixed(2)) : 0;
};
/**
* Zoom in the chart
*
* @method zoomIn
* @param {Number} zoomLevel A value to be applied at the current zoom
*/
this.zoomIn = function(zoomLevel){
var startDate, endDate, minutes;
if (env.timeEventsActive == true) {
minutes = ((env.params.endDate - env.params.startDate) / 60000) * config.zoomProportion * Math.abs(zoomLevel);
startDate = addMinutes(env.params.startDate, minutes);
endDate = subMinutes(env.params.endDate, minutes);
if (this._isZoomableIn(startDate, endDate)) {
env.params.startDate = startDate;
env.params.endDate = endDate;
if (env.isUpdatedPeriodicallyActive) { // Disable the auto refresh function if active
env.mainView.controlPanel.keepUpdatedActive(false);
}
env.mainView.updateXDomain(true);
}
}
};
/**
* Zoom out the chart
*
* @method zoomOut
* @param {Number} zoomLevel A value to be applied at the current zoom
*/
this.zoomOut = function(zoomLevel){
var startDate, endDate, minutes, newTimeWindow;
if (env.timeEventsActive == true) {
minutes = ((env.params.endDate - env.params.startDate) / 60000) * config.zoomProportion * Math.abs(zoomLevel);
startDate = subMinutes(env.params.startDate, minutes);
endDate = addMinutes(env.params.endDate, minutes);
newTimeWindow = this.getBoundedWindow(startDate, endDate);
if (this._isZoomableOut(newTimeWindow.start, newTimeWindow.end)) {
env.params.startDate = newTimeWindow.start;
env.params.endDate = newTimeWindow.end;
if (env.isUpdatedPeriodicallyActive) { // Disable the auto refresh function if active
env.mainView.controlPanel.keepUpdatedActive(false);
}
env.mainView.updateXDomain(true);
}
}
};
/**
* Shift left the time window
*
* @method shiftLeft
*/
this.shiftLeft = function(){
var startDate, endDate, timeOffset, newBoundaries;
if (env.timeEventsActive == true) {
timeOffset = ((env.params.endDate - env.params.startDate) / 60000) * config.slideProportion;
startDate = subMinutes(env.params.startDate, timeOffset);
endDate = subMinutes(env.params.endDate, timeOffset);
newBoundaries = this.getBoundedWindow(startDate, endDate);
if (this._isTranslableLeft(startDate) || env.params.startDate.getTime() != newBoundaries.start.getTime()) {
env.params.startDate = newBoundaries.start;
env.params.endDate = newBoundaries.end;
if (env.isUpdatedPeriodicallyActive) { // Disable the auto refresh function if active
env.mainView.controlPanel.keepUpdatedActive(false);
}
env.mainView.updateXDomain(true);
}
}
};
/**
* Shift right the time window
*
* @method shiftRight
*/
this.shiftRight = function(){
var startDate, endDate, timeOffset, newBoundaries;
if (env.timeEventsActive == true) {
timeOffset = ((env.params.endDate - env.params.startDate) / 60000) * config.slideProportion;
startDate = addMinutes(env.params.startDate, timeOffset);
endDate = addMinutes(env.params.endDate, timeOffset);
newBoundaries = this.getBoundedWindow(startDate, endDate);
if (this._isTranslableRight(endDate) || env.params.endDate.getTime() != newBoundaries.end.getTime()) {
env.params.startDate = newBoundaries.start;
env.params.endDate = newBoundaries.end;
if (env.isUpdatedPeriodicallyActive) { // Disable the auto refresh function if active
env.mainView.controlPanel.keepUpdatedActive(false);
}
env.mainView.updateXDomain(true);
}
}
};
/**
* Check if the new selection is a sub-selection of the previous one
*
* @method isSelectionReduced
* @param {Array} selectedRows The new list of selected rows
* @return {Boolean} True if the new selection is a sub-selection of the previous one
*/
this.isSelectionReduced = function(selectedRows){
var isSelectionReduced;
isSelectionReduced = (selectedRows.length < env.params.selectedRows.length || env.params.selectedRows.length == 0);
return isSelectionReduced;
};
/**
* Check if the new selection can be applied
*
* @method isSubSelectable
* @param {Date} startDate The start date of the new selections
* @param {Date} endDate The end date of the new selection
* @param {Array} selectedRows The new list of selected rows
* @return {Boolean} True if the new selection can be applied
*/
this.isSubSelectable = function(startDate, endDate, selectedRows){
var isZoomableIn, isTimeChangend, isSelectionReduced;
isZoomableIn = this._isZoomableIn(startDate, endDate);
isTimeChangend = !((env.params.startDate == startDate) && (env.params.endDate == endDate));
isSelectionReduced = this.isSelectionReduced(selectedRows);
return isZoomableIn || (!isZoomableIn && !isTimeChangend && isSelectionReduced);
};
/**
* Check if the new selection can be reduced
*
* @method isReducible
* @param {Date} startDate The start date of the new selections
* @param {Date} endDate The end date of the new selection
* @return {Boolean} True if the new selection can be reduced
*/
this.isReducible = function(startDate, endDate, selectedRows){
var isZoomableIn, isTimeChangend, isSelectionReduced;
isZoomableIn = this._isZoomableIn(startDate, endDate);
isTimeChangend = !((env.params.startDate == startDate) && (env.params.endDate == endDate));
isSelectionReduced = this.isSelectionReduced(selectedRows);
return isZoomableIn || (!isZoomableIn && !isTimeChangend && isSelectionReduced);
};
/**
* Update the status of the controller parameters of the whole widget.
*
* @method updateStatus
*/
this.updateStatus = function(){
var startDate, endDate, timeOffset, zoomMinutes, offsetTmp, newTimeWindow;
startDate = env.params.startDate;
endDate = env.params.endDate;
offsetTmp = ((endDate - startDate) / 60000);
timeOffset = offsetTmp * config.slideProportion;
zoomMinutes = offsetTmp * config.zoomProportion * Math.abs(config.manualZoomFactor);
env.params.aggregationLevel = this._getAggregationLevel(startDate, endDate);
env.isZoomableIn = this._isZoomableIn(addMinutes(startDate, zoomMinutes), subMinutes(endDate, zoomMinutes));
newTimeWindow = this.getBoundedWindow(subMinutes(startDate, zoomMinutes), addMinutes(endDate, zoomMinutes));
env.isZoomableOut = this._isZoomableOut(newTimeWindow.start, newTimeWindow.end);
env.isTranslableLeft = this._isTranslableLeft(subMinutes(startDate, timeOffset));
env.isTranslableRight = this._isTranslableRight(addMinutes(endDate, timeOffset));
};
/**
* Update the chart with the latest available data
*
* @method getNewData
* @param {integer} timeWindow A time window express as seconds if null the current one will be used
*/
this.getNewData = function(timeWindow){
env.params.startDate = null;
env.params.endDate = null;
env.params.timeWindow = (timeWindow) ? timeWindow : env.timeWindowSeconds;
env.timeEventsActive = false;
env.mainView.redraw(function(){
env.timeEventsActive = true;
});
};
/**
* Given a time interval, this function checks if it is valid otherwise it returns the closer valid selection
*
* @method getBoundedWindow
* @param {Date} startDate The start date of the new selections
* @param {Date} endDate The end date of the new selection
* @return {Object} Returns start and end time (two Date Objects)
*/
this.getBoundedWindow = function(startDate, endDate){
var timeWindowMinutes, newStartDate, newEndDate;
timeWindowMinutes = (endDate - startDate) / 60000; // Time window in minutes
if (startDate < env.measurementStartTime && endDate <= env.measurementEndTime){
newStartDate = env.measurementStartTime;
newEndDate = addMinutes(newStartDate, timeWindowMinutes);
}else if (endDate > env.measurementEndTime && startDate >= env.measurementStartTime){
newEndDate = env.measurementEndTime;
newStartDate = subMinutes(newEndDate, timeWindowMinutes);
}else{
newStartDate = startDate;
newEndDate = endDate;
}
return {start: newStartDate, end: newEndDate};
};
/**
* Given a time interval, this function checks if it is valid otherwise it returns the closer valid zoomable selection
*
* @method getZoomableWindow
* @param {Date} startDate The start date of the new selections
* @param {Date} endDate The end date of the new selection
* @return {Object} Returns start and end time (two Date Objects)
*/
this.getZoomableWindow = function(startDate, endDate){
var minTimeWindow, newTimeWindow, newStartDate, newEndDate, timeWindowCenter, halfMinTimeWindow;
minTimeWindow = ((env.minAggregation * config.virtualZoomFactor) * env.maxNumberOfCellsPerRow) * 1000;
newTimeWindow = (endDate.getTime() - startDate.getTime());
timeWindowCenter = startDate.getTime() + (newTimeWindow / 2);
halfMinTimeWindow = (minTimeWindow / 2);
newStartDate = new Date(timeWindowCenter - halfMinTimeWindow);
newEndDate = new Date(timeWindowCenter + halfMinTimeWindow);
return {start: newStartDate, end: newEndDate};
};
/**
* This function starts the auto-update feature.
*
* @method keepUpdated
* @param {Boolean} keepUpdate If true the auto-update feature starts
*/
this.keepUpdated = function(keepUpdate){
var $this, interval;
$this = this;
interval = ((env.debugMode) ? 6000 : (config.updateEverySeconds * 1000));
if (keepUpdate){
this.getNewData(null); // First refresh
updateTimer = setInterval($this.getNewData, interval);
}else{
clearInterval(updateTimer);
}
};
};
return TimeController;
});