report

  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
var DEFAULT_PAGES_IN_SELECTION = 50;
var DEFAULT_ROWS_BY_PAGE = 500;
var MAX_PAGE_CACHE_SIZE = 50;
var Report = function(timeFrom, timeTo, resolution) {
this.currentPage = null;
this.amountOfPages = null;
this.tableHeaderString = null;
this.tableBodyString = null;
this.timeFrom = timeFrom;
this.timeTo = timeTo;
this.resolution = resolution;
this.cachedPageTableBodies = [];
this.cacheSize = 0;
this.initialize();
};
Report.prototype.initialize = function() {
for (var i = 0; i < MAX_PAGE_CACHE_SIZE; i++) {
this.cachedPageTableBodies[i] = undefined;
}
var lastUpdate = manager.lastUpdate;
var data = manager.getDataByTimespan(lastUpdate - 1000, lastUpdate, 10);
this.tableHeaderString = makeStringReportTableHead(data);
this.amountOfPages = Math.ceil((this.timeTo - this.timeFrom)/(this.resolution*DEFAULT_ROWS_BY_PAGE));
var pageStart = 1;
var pageTimespan = (DEFAULT_ROWS_BY_PAGE - 1)*this.resolution;
if (this.amountOfPages > DEFAULT_PAGES_IN_SELECTION) {
this.rebuildPagesSelector();
var firstSelection = {
'pageStart': pageStart,
'pageEnd': pageStart + DEFAULT_PAGES_IN_SELECTION,
'timeFrom': (this.timeTo - pageTimespan*DEFAULT_PAGES_IN_SELECTION),
'timeTo': this.timeTo
};
this.rebuildPagesForSelection(firstSelection);
}
else {
this.cleanPagesSelector();
var pageStart = 1;
var firstSelection = {
'pageStart': pageStart,
'pageEnd': pageStart + this.amountOfPages,
'timeFrom': this.timeTo
- pageTimespan*this.amountOfPages
+ pageTimespan
- (lastUpdate - this.timeFrom)%pageTimespan,
'timeTo': this.timeTo
};
this.rebuildPagesForSelection(firstSelection);
}
};
Report.prototype.rebuildTableForPage = function(pageIndex) {
this.currentPage = pageIndex - 1;
var lastUpdate = manager.lastUpdate;
var firstTime = manager.firstTime;
var pageTimespan = (DEFAULT_ROWS_BY_PAGE - 1)*this.resolution;
var currentPageTimespan = pageTimespan;
if (this.currentPage === this.amountOfPages - 1) {
currentPageTimespan = (this.timeTo - this.timeFrom)%pageTimespan;
}
if (!this.cachedPageTableBodies[this.currentPage]) {
var timeTo = this.timeTo - this.currentPage*pageTimespan;
var timeFrom = timeTo - currentPageTimespan;
this.cachedPageTableBodies[this.currentPage] = makeStringReportTableBody(
manager.getDataByTimespan(
timeFrom,
timeTo,
this.resolution
));
this.cacheSize++;
}
this.drawReport(this.cachedPageTableBodies[this.currentPage]);
if (this.cacheSize === MAX_PAGE_CACHE_SIZE)
this.cacheClean();
/*if (this.currentPage > 0 && !this.cachedPageTableBodies[this.currentPage - 1]) {
this.cachedPageTableBodies[this.currentPage - 1] = makeStringReportTableBody(
manager.getDataByTimespan(
timeFrom + pageTimespan,
timeTo + pageTimespan,
this.resolution
));
this.cacheSize++;
}
if (this.currentPage < this.amountOfPages - 2 && !this.cachedPageTableBodies[this.currentPage + 1]) {
this.cachedPageTableBodies[this.currentPage + 1] = makeStringReportTableBody(
manager.getDataByTimespan(
timeFrom - pageTimespan,
timeTo - pageTimespan,
this.resolution
));
this.cacheSize++;
}*/
};
Report.prototype.rebuildPagesForSelection = function(describer) {
var pageStart = describer.pageStart - 1;
var pageEnd = describer.pageEnd;
var timeFrom = describer.timeFrom;
var timeTo = describer.timeTo;
this.rebuildPageTable(describer);
this.rebuildTableForPage(pageStart + 1);
};
Report.prototype.rebuildPageTable = function(describer) {
var pageTable = document.getElementById('page_table');
var pageStart = describer.pageStart - 1;
var timeTo = describer.timeTo;
var timeFrom = describer.timeFrom;
var pageEnd = pageStart
+ Math.ceil(((timeTo - timeFrom)/this.resolution + 1)/DEFAULT_ROWS_BY_PAGE);
var pageTableString = '<tbody><tr>';
var rowStrings = [];
for (var i = pageStart, rowIndex = -1; i < pageEnd; i++) {
if ((i - pageStart)%10 === 0) {
rowIndex++;
rowStrings[rowIndex] = '';
}
rowStrings[rowIndex] += '<td>' + (i + 1) + '</td>';
}
for (var i = 0, len = rowStrings.length; i < len; i++) {
if (rowStrings[i] === '') contunue;
pageTableString += '<tr>' + rowStrings[i] + '</tr>';
}
removeChildren(pageTable);
pageTable.innerHTML = pageTableString;
var rows = pageTable.children[0].children;
for (var i = 0, rowCount = rows.length; i < rowCount; i++) {
var cells = rows[i].children;
for (var j = 0, cellCount = cells.length; j < cellCount; j++) {
var pageTableCell = cells[j];
pageTableCell.onclick = function clickOnPageSelection() {
var cell = this;
var pageIndex = parseInt(cell.innerHTML);
report.rebuildTableForPage(pageIndex);
}
}
}
};
Report.prototype.rebuildPagesSelector = function() {
var pageTimespan = (DEFAULT_ROWS_BY_PAGE - 1)*this.resolution;
var lastPageTimespan = (this.timeTo - this.timeFrom)%pageTimespan;
var optionCount = Math.ceil((this.amountOfPages)/DEFAULT_PAGES_IN_SELECTION);
var pagesSelectorPlace = document.getElementById('pages_selector_place');
var pagesSelector = document.createElement('select');
pagesSelector.setAttribute('id', 'pages_selector');
for (var i = 0; i < optionCount; i++) {
var pageStartIndex = i*DEFAULT_PAGES_IN_SELECTION + 1;
var pageEndIndex = pageStartIndex + DEFAULT_PAGES_IN_SELECTION - 1;
var indexAdder = DEFAULT_PAGES_IN_SELECTION - 1;
var currentTimeTo = this.timeTo - i*pageTimespan*DEFAULT_PAGES_IN_SELECTION;
var currentTimeFrom = currentTimeTo - pageTimespan*DEFAULT_PAGES_IN_SELECTION;
if (currentTimeFrom < this.timeFrom) {
currentTimeFrom = this.timeFrom;
pageEndIndex -= DEFAULT_PAGES_IN_SELECTION;
pageEndIndex += Math.ceil((currentTimeTo - currentTimeFrom)/pageTimespan);
}
var option = document.createElement('option');
var describer = {
'pageStart': pageStartIndex,
'pageEnd': pageEndIndex,
'timeFrom': currentTimeFrom,
'timeTo': currentTimeTo
};
var describerString = pageStartIndex + ' - ' + pageEndIndex
+ ' (' + (new Date(currentTimeFrom*1000))
+ ' - ' + (new Date(currentTimeTo*1000));
option.setAttribute('value', JSON.stringify(describer));
option.innerHTML = describerString;
pagesSelector.appendChild(option);
}
addPagesSelectorBehavior(pagesSelector);
removeChildren(pagesSelectorPlace);
pagesSelectorPlace.appendChild(pagesSelector);
};
Report.prototype.cleanPagesSelector = function() {
removeElementById('pages_selector');
};
Report.prototype.getDataByTimespan = function() {
//return manager.getDataByTimespan(this.timeFrom, this.timeTo, this.resolution);
};
Report.prototype.cacheClean = function() {
var safeRange = {from: this.currentPage - 5, to: this.currentPage + 5};
for (var i = 0; i < safeRange.from; i++) {
delete this.cachedPageTableBodies[i];
this.cacheSize--;
}
for (var i = safeRange.to, cachedLen = this.cachedPageTableBodies.length; i < cachedLen; i++) {
delete this.cachedPageTableBodies[i];
this.cacheSize--;
}
};
/**
* Rebuilds log input. Recreates input fields for 'timeFrom', 'timeTo'
* values and for 'resolution' value. Also recreates selector list with
* default resolutions.
*/
function rebuildLogInput() {
if (manager.fileCount() === 0) return;
var lastUpdate = manager.lastUpdate;
var logInput = document.getElementById('log_input');
var frag = document.createDocumentFragment();
var child = document.createElement('input');
child.setAttribute('id', 'from_picker');
child.setAttribute('type', 'text');
child.setAttribute('value', convertDateToText(new Date(lastUpdate*1000)));
frag.appendChild(child);
child = document.createElement('input');
child.setAttribute('id', 'to_picker');
child.setAttribute('type', 'text');
child.setAttribute('value', convertDateToText(new Date(lastUpdate*1000)));
frag.appendChild(child);
child = document.createElement('input');
child.setAttribute('id', 'get_rrd_data_button');
child.setAttribute('type', 'submit');
child.value = 'get report'
child.onclick = function getManagerData() {
getDataFromManager();
}
frag.appendChild(child);
child = document.createElement('select');
child.setAttribute('id', 'resolution_list');
var defaultElement = document.createElement('option');
defaultElement.setAttribute('value', 'default');
defaultElement.innerHTML = 'default';
child.appendChild(defaultElement);
var resolutions = manager.getDefaultResolutions();
for (var i = 0, len = resolutions.length; i < len; i++) {
var el = document.createElement('option');
el.setAttribute('value', resolutions[i]);
el.innerHTML = resolutions[i];
child.appendChild(el);
}
addResolutionListBehavior(child);
frag.appendChild(child);
child = document.createElement('input');
child.setAttribute('id', 'resolution_input');
child.setAttribute('type', 'text');
frag.appendChild(child);
clearLogInput();
logInput.appendChild(frag);
};
function addPagesSelectorBehavior(pagesSelector) {
pagesSelector.onchange = function() {
var selector = this;
var selectedOption = selector.options[selector.selectedIndex];
var describer = JSON.parse(selectedOption.value);
report.rebuildPagesForSelection(describer);
};
}
/**
* Adds a behavior of resolution list. This behavior is to change
* resolution values in resolution input field with id="resolution_input"
* using values from resolution list selector.
*
* @param{Object} resolutionList resolution list on that described behavior
* will be added.
*/
function addResolutionListBehavior(resolutionList) {
resolutionList.onchange = function changeResolutionOption() {
var list = this;
var resolutionInput = document.getElementById('resolution_input');
var textFieldFromList = list.options[list.selectedIndex].innerHTML;
if (textFieldFromList === 'default') {
var timeFrom = convertTextToDate(document.getElementById('from_picker').value);
var resolution = manager.getProperResolution(
convertTextToDate(timeFrom).getTime()/1000);
resolutionInput.value = resolution;
} else
resolutionInput.value = textFieldFromList;
}
};
/**
* Simply remove children of log input element with id="log_input"
*/
function clearLogInput() {
var logInput = document.getElementById('log_input');
removeChildren(logInput);
};
function getDataFromManager() {
var logInput = document.getElementById('log_input');
var timeFrom = convertTextToDate(document.getElementById('from_picker').value);
var timeTo = convertTextToDate(document.getElementById('to_picker').value);
var data = {};
var resolutionList = document.getElementById('resolution_list');
var textFieldFromList = resolutionList.options[resolutionList.selectedIndex].innerHTML;
var resolutionInput = document.getElementById('resolution_input');
var resolution = resolutionInput.value;
if (!resolution || textFieldFromList === 'default')
resolution = manager.getProperResolution(
timeFrom.getTime()/1000);
else
resolution = parseInt(resolution);
resolutionInput.value = resolution;
console.time('get_data_time');
report = new Report(
timeFrom.getTime()/1000,
timeTo.getTime()/1000,
resolution
);
//data = manager.getDataByTimespan(
// timeFrom.getTime()/1000,
// timeTo.getTime()/1000,
// resolution);
console.timeEnd('get_data_time');
//drawReport(data);
};
Report.prototype.drawReport = function() {
//console.log(data);
console.time('rebuild_report_table');
var stringTableInner = this.tableHeaderString
+ this.cachedPageTableBodies[this.currentPage];
var reportTable = document.getElementById('report_table');
removeChildren(reportTable);
reportTable.innerHTML = stringTableInner;
console.timeEnd('rebuild_report_table');
};
function makeStringReportTableHead(data) {
var tStr = '<thead class="fixed_header"><tr><td>Time</td>';
var rowCount = data.timestamps.length;
var idList = data.fileIds;
var outputsById = data.outputsById;
var cfNamesById = data.cfNamesById;
var dsNamesById = data.dsNamesById;
for (var i = 0, idCount = idList.length; i < idCount; i++) {
var id = idList[i];
var cfNames = cfNamesById[id];
var dsNames = dsNamesById[id];
var cfNamesConcated = '';
for (var j = 0, cfCountMinusOne = cfNames.length - 1; j < cfCountMinusOne; j++)
cfNamesConcated += cfNames[j] + '\t|\t';
cfNamesConcated += cfNames[cfNames.length - 1];
for (var k = 0, dsCount = dsNames.length; k < dsCount; k++) {
var dsName = dsNames[k];
tStr += '<td>' + cfNamesConcated + ' of ' + dsName
+ ' on<br>' + id + '</td>';
}
}
tStr += '</tr></thead>';
return tStr;
}
function makeStringReportTableBody(data) {
var tStr = '<tbody class="scrollable_body">';
var rowCount = data.timestamps.length;
var idList = data.fileIds;
var outputsById = data.outputsById;
var cfNamesById = data.cfNamesById;
var dsNamesById = data.dsNamesById;
for (var i = rowCount - 1; i >= 0; i--) {
tStr += '<tr><td class="time_cell">' +
convertDateToText(new Date(data.timestamps[i]*1000)) +
'</td>';
var tableRow = data.getTableRowByTimestampIndex(i);
var valueCount = 0;
var rowString = '';
for (var j = 0, idCount = idList.length; j < idCount; j++) {
var id = idList[j];
var outputs = outputsById[id];
var cfNames = cfNamesById[id];
var dsNames = dsNamesById[id];
for (var k = 0, dsCount = dsNames.length; k < dsCount; k++) {
rowString += '<td class="value_cell">';
var value = null;
for (var t = 0, cfCountMinusOne = cfNames.length - 1; t < cfCountMinusOne; t++) {
value = tableRow[valueCount++];
var text = 'unknown';
if (typeof(value) !== 'undefined' && value !== null) {
text = value.toFixed(2);
}
rowString += text + '<br>';
}
value = tableRow[valueCount++];
var text = 'unknown';
if (typeof(value) !== 'undefined' && value !== null) {
text = value.toFixed(2);
}
rowString += text + '</td>';
}
}
tStr += rowString + '</tr>';
}
tStr += '</tbody>';
return tStr;
};