/
logviewer.js
424 lines (358 loc) · 16.3 KB
/
logviewer.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
'use strict';
logViewerApp.controller('LogviewerCtrl', [
'$anchorScroll', '$http', '$location', '$q', '$rootScope', '$scope',
'$timeout', 'ThJobArtifactModel', 'ThLog', 'ThLogSliceModel', 'ThJobModel', 'thNotify',
'dateFilter', 'thJobSearchStr', 'ThResultSetModel', 'thDateFormat', 'thReftestStatus',
function Logviewer(
$anchorScroll, $http, $location, $q, $rootScope, $scope,
$timeout, ThJobArtifactModel, ThLog, ThLogSliceModel, ThJobModel, thNotify,
dateFilter, thJobSearchStr, ThResultSetModel, thDateFormat, thReftestStatus) {
var $log = new ThLog('LogviewerCtrl');
// changes the size of chunks pulled from server
var LINE_BUFFER_SIZE = 100;
var LogSlice;
$rootScope.urlBasePath = $location.absUrl().split('logviewer')[0];
var query_string = $location.search();
if (query_string.repo !== "") {
$rootScope.repoName = query_string.repo;
}
if (query_string.job_id !== "") {
$scope.job_id= query_string.job_id;
LogSlice = new ThLogSliceModel($scope.job_id, LINE_BUFFER_SIZE);
}
$scope.displayedLogLines = [];
$scope.loading = false;
$scope.logError = false;
$scope.jobExists = true;
$scope.currentLineNumber = 0;
$scope.highestLine = 0;
$scope.showSuccessful = true;
$scope.willScroll = false;
$scope.$watch('artifact', function () {
if (!$scope.artifact) {
return;
}
$scope.showSuccessful = !$scope.hasFailedSteps();
});
$scope.$watch('[selectedBegin, selectedEnd]', function(newVal, oldVal) {
var newHash = (newVal[0] === newVal[1]) ? newVal[0] : newVal[0] + "-L" + newVal[1];
if (!isNaN(newVal[0])) {
$location.hash("L" + newHash);
} else if ($scope.artifact) {
$location.hash("");
}
});
$scope.$on("$locationChangeSuccess", function($event, $artifact) {
var oldLine = parseInt($scope.currentLineNumber);
getSelectedLines();
var newLine = parseInt($scope.selectedBegin);
var range = LINE_BUFFER_SIZE / 2;
if ((newLine <= (oldLine - range) || newLine >= oldLine + range) && !$scope.willScroll) {
if ($scope.artifact) {
moveScrollToLineNumber(newLine, $event);
}
}
$scope.willScroll = false;
});
$scope.click = function(line, $event) {
$scope.willScroll = true;
if ($event.shiftKey) {
if (line.index < $scope.selectedBegin) {
$scope.selectedEnd = $scope.selectedBegin;
$scope.selectedBegin = line.index;
} else {
$scope.selectedEnd = line.index;
}
} else {
$scope.selectedBegin = $scope.selectedEnd = line.index;
}
};
// Erase the value of selectedBegin, used to erase the hash value when
// the user clicks on the error step button
$scope.eraseSelected = function() {
$scope.selectedBegin = 'undefined';
};
$scope.hasFailedSteps = function () {
var steps = $scope.artifact.step_data.steps;
for (var i = 0; i < steps.length; i++) {
// We only recently generated step results as part of ingestion,
// so we have to check the results property is present.
// TODO: Remove this when the old data has expired, so long as
// other data submitters also provide a step result.
if ('result' in steps[i] && steps[i].result !== 'success' &&
steps[i].result !== 'skipped') {
return true;
}
}
return false;
};
// Get the css class for the result, step buttons and other general use
$scope.getShadingClass = function(result) {
return "result-status-shading-" + result;
};
$scope.loadMore = function(bounds, element) {
var deferred = $q.defer(), range, req, above, below;
if (!$scope.loading) {
// move the line number either up or down depending which boundary was hit
$scope.currentLineNumber = moveLineNumber(bounds);
range = {
start: $scope.currentLineNumber,
end: $scope.currentLineNumber
};
if (bounds.top) {
above = getChunkAbove(range);
} else if (bounds.bottom) {
below = getChunkBelow(range);
} else {
range = getChunksSurrounding($scope.currentLineNumber);
}
// dont do the call if we already have all the lines
if (range.start === range.end) {
return deferred.promise;
}
$scope.loading = true;
var lineRangeParams = {
job_id: $scope.job_id,
start_line: range.start,
end_line: range.end
};
if ($scope.artifact.logname) {
lineRangeParams.name = $scope.artifact.logname;
}
LogSlice.get_line_range(lineRangeParams, {
buffer_size: LINE_BUFFER_SIZE
}).then(function(data) {
drawErrorLines(data);
if (bounds.top) {
for (var i = data.length - 1; i >= 0; i--) {
// make sure we are inserting at the right place
if ($scope.displayedLogLines[0].index !== data[i].index + 1) {
continue;
}
$scope.displayedLogLines.unshift(data[i]);
}
$timeout(function () {
if (above) {
removeChunkBelow();
}
}, 100);
} else if (bounds.bottom) {
var sh = element.scrollHeight;
var lines = $scope.displayedLogLines;
for (var j = 0; j < data.length; j++) {
// make sure we are inserting at the right place
if (lines[lines.length - 1].index !== data[j].index - 1) {
continue;
}
$scope.displayedLogLines.push(data[j]);
}
$timeout(function () {
if (below) {
removeChunkAbove();
element.scrollTop -= element.scrollHeight - sh;
}
}, 100);
} else {
$scope.displayedLogLines = data;
}
$scope.loading = false;
deferred.resolve();
}, function (error) {
$scope.loading = false;
$scope.logError = true;
thNotify.send("The log no longer exists or has expired", 'warning', true);
deferred.reject();
});
} else {
deferred.reject();
}
return deferred.promise;
};
// @@@ it may be possible to do this with the angular date filter?
$scope.formatTime = function(sec) {
var h = Math.floor(sec/3600);
var m = Math.floor(sec%3600/60);
var s = Math.floor(sec%3600 % 60);
var secStng = sec.toString();
var ms = secStng.substr(secStng.indexOf(".")+1, 2);
return ((h > 0 ? h + "h " : "") + (m > 0 ? m + "m " : "") +
(s > 0 ? s + "s " : "") + (ms > 0 ? ms + "ms " : "00ms"));
};
$scope.displayTime = function(started, finished) {
var start = started ? started.substr(started.indexOf(" ")+1, 8) : '?';
var end = finished ? finished.substr(finished.indexOf(" ")+1, 8) : '?';
return start + "-" + end;
};
$scope.init = function() {
$scope.logProperties = [];
ThJobModel.get($scope.repoName, $scope.job_id).then(function(job) {
var jobStr = thJobSearchStr(job);
// set the title of the browser window/tab
$scope.logViewerTitle = "Log for " + jobStr;
// set the result value and shading color class
$scope.result = {label: "Result", value: job.result};
$scope.resultStatusShading = $scope.getShadingClass(job.result);
// other properties, in order of appearance
$scope.logProperties = [
{label: "Job", value: jobStr},
{label: "Machine", value: job.machine_name},
{label: "Start", value: dateFilter(job.start_timestamp*1000, thDateFormat)},
{label: "End", value: dateFilter(job.end_timestamp*1000, thDateFormat)}
];
// Test to expose the reftest button in the logviewer actionbar
$scope.isReftest = function() {
if (job.job_group_name) {
return thReftestStatus(job);
}
};
// get the revision and linkify it
ThResultSetModel.getResultSet($scope.repoName, job.result_set_id).then(function(data){
var revision = data.data.revision;
$scope.logProperties.push({label: "Revision", value: revision});
});
}, function (error) {
$scope.loading = false;
$scope.jobExists = false;
thNotify.send("The job does not exist or has expired", 'danger', true);
});
// Make the log and job artifacts available
ThJobArtifactModel.get_list({job_id: $scope.job_id, name__in: 'text_log_summary,Job Info'})
.then(function(artifactList) {
artifactList.forEach(function(artifact, $event) {
if (artifact.name === 'text_log_summary') {
$scope.artifact = artifact.blob;
$scope.step_data = artifact.blob.step_data;
// If the log contains no errors load the head otherwise
// load the first failure step line in the artifact. We
// also need to test for the 0th element for outlier jobs.
if ($scope.step_data.steps[0]) {
if ($scope.step_data.all_errors.length == 0) {
angular.element(document).ready(function () {
if (isNaN($scope.selectedBegin)) {
$scope.displayLog($scope.step_data.steps[0], 'initialLoad');
} else {
moveScrollToLineNumber($scope.selectedBegin, $event);
}
});
} else {
$timeout(function() {
if (isNaN($scope.selectedBegin)) {
angular.element('.lv-error-line').first().trigger('click');
} else {
moveScrollToLineNumber($scope.selectedBegin, $event);
}
}, 100);
}
}
} else if (artifact.name === 'Job Info') {
$scope.job_details = artifact.blob.job_details;
}
});
});
};
/** utility functions **/
function moveScrollToLineNumber(linenumber, $event) {
$scope.currentLineNumber = linenumber;
$scope.displayedStep = getStepFromLine(linenumber);
$scope.loadMore({}).then(function () {
$timeout(function () {
var raw = $('.lv-log-container')[0];
var line = $('.lv-log-line[line="' + linenumber + '"]');
raw.scrollTop += line.offset().top - $('.run-data').outerHeight() -
$('.navbar').outerHeight() - 120;
});
});
}
function getStepFromLine(linenumber) {
var steps = $scope.artifact.step_data.steps;
for (var i = 1; i < steps.length; i++) {
if (steps[i].started_linenumber >= linenumber) {
return steps[i-1];
}
}
}
function getSelectedLines () {
var urlHash = $location.hash();
var regexSelectedlines = /L(\d+)(-L(\d+))?$/;
if (regexSelectedlines.test(urlHash)) {
var matchSelectedLines = urlHash.match(regexSelectedlines);
if (isNaN(matchSelectedLines[3])) {
matchSelectedLines[3] = matchSelectedLines[1];
}
$scope.selectedBegin = matchSelectedLines[1];
$scope.selectedEnd = matchSelectedLines[3];
}
}
function logFileLineCount () {
var steps = $scope.artifact.step_data.steps;
return steps[ steps.length - 1 ].finished_linenumber + 1;
}
function moveLineNumber (bounds) {
var lines = $scope.displayedLogLines, newLine;
if (bounds.top) {
return lines[0].index;
} else if (bounds.bottom) {
newLine = lines[lines.length - 1].index + 1;
return (newLine > logFileLineCount()) ? logFileLineCount(): newLine;
}
return $scope.currentLineNumber;
}
function drawErrorLines (data) {
if (data.length === 0) {
return;
}
var min = data[0].index;
var max = data[ data.length - 1 ].index;
$scope.artifact.step_data.steps.forEach(function(step) {
step.errors.forEach(function(err) {
var line = err.linenumber;
if (line < min || line > max) {
return;
}
var index = line - min;
data[index].hasError = true;
});
});
}
function getChunksSurrounding(line) {
var request = {start: null, end: null};
getChunkContaining(line, request);
getChunkAbove(request);
getChunkBelow(request);
return request;
}
function getChunkContaining (line, request) {
var index = Math.floor(line/LINE_BUFFER_SIZE);
request.start = index * LINE_BUFFER_SIZE;
request.end = (index + 1) * LINE_BUFFER_SIZE;
}
function getChunkAbove (request) {
request.start -= LINE_BUFFER_SIZE;
request.start = Math.floor(request.start/LINE_BUFFER_SIZE)*LINE_BUFFER_SIZE;
if (request.start >= 0) {
return true;
} else {
request.start = 0;
return false;
}
}
function getChunkBelow (request) {
var lastLine = logFileLineCount();
request.end += LINE_BUFFER_SIZE;
request.end = Math.ceil(request.end/LINE_BUFFER_SIZE)*LINE_BUFFER_SIZE;
if (request.end <= lastLine) {
return true;
} else {
request.end = lastLine;
return false;
}
}
function removeChunkAbove (request) {
$scope.displayedLogLines = $scope.displayedLogLines.slice(LINE_BUFFER_SIZE);
}
function removeChunkBelow (request) {
var endSlice = $scope.displayedLogLines.length - LINE_BUFFER_SIZE;
$scope.displayedLogLines = $scope.displayedLogLines.slice(0, endSlice);
}
}
]);