/usr/share/koji-web/static/js/watchlogs.js is in koji-servers 1.10.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | var MAX_ERRORS = 5; // errors before we just stop
var CHUNK_SIZE = 16384;
// General globals
var baseURL = window.location.href.substring(0, window.location.href.lastIndexOf("/"));
var logElement = null;
var headerElement = null;
var errorCount = 0;
var tasks = null;
var offsets = {};
var lastlog = "";
var tasksToProcess = null;
var currentTaskID = null;
var currentInfo = null;
var currentLogs = null;
var currentLog = null;
function parseTasklist() {
var tasklist = [];
var queryStr = unescape(window.location.search.substring(1));
var vars = queryStr.split('&');
for (var i=0; i<vars.length; i++) {
if (vars[i].split('=')[0] == 'taskID') {
tasklist.push(parseInt(vars[i].split('=')[1]));
}
}
return tasklist;
}
function maybeScroll(origHeight) {
if ((window.pageYOffset + window.innerHeight) >= origHeight) {
// Only scroll the window if we were already at the bottom
// of the document
window.scroll(window.pageXOffset, document.body.clientHeight);
}
}
function handleStatus(event) {
req = event.target;
if (req.readyState != 4) {
return;
}
if (req.status == 200) {
if (req.responseText.length > 0) {
var lines = req.responseText.split("\n");
var line = lines[0];
var data = line.split(":");
// var taskID = parseInt(data[0]);
var state = data[1];
var logs = {};
for (var i = 1; i < lines.length; i++) {
data = lines[i].split(":");
var filename = data[0];
var filesize = parseInt(data[1]);
if (filename.indexOf(".log") != -1) {
logs[filename] = filesize;
}
}
} else {
// task may not have started yet
var state = "UNKNOWN";
var logs = {};
}
currentInfo = {state: state, logs: logs};
if (!(state == "FREE" || state == "OPEN" ||
state == "ASSIGNED" || state == "UNKNOWN")) {
// remove tasks from the task list that are no longer running
for (var i = 0; i < tasks.length; i++) {
if (tasks[i] == currentTaskID) {
tasks.splice(i, 1);
break;
}
}
}
} else {
currentInfo = {state: "UNKNOWN", logs: {}};
popupError("Error checking status of task " + currentTaskID + ": " + req.statusText);
}
currentLogs = [];
for (var logname in currentInfo.logs) {
currentLogs.push(logname);
}
processLog();
}
function getStatus() {
if (tasksToProcess.length == 0) {
if (errorCount > MAX_ERRORS) {
return;
} else {
if (headerElement != null) {
headerElement.appendChild(document.createTextNode("."));
}
setTimeout(checkTasks, 5000);
return;
}
}
currentTaskID = tasksToProcess.shift();
var req = new XMLHttpRequest();
req.open("GET", baseURL + "/taskstatus?taskID=" + currentTaskID, true);
req.onreadystatechange = handleStatus;
req.send(null);
}
function checkTasks() {
if (tasks.length == 0) {
docHeight = document.body.clientHeight;
logElement.appendChild(document.createTextNode("\n==> Task has completed <==\n"));
maybeScroll(docHeight);
} else {
tasksToProcess = [];
for (var i = 0; i < tasks.length; i++) {
tasksToProcess.push(tasks[i]);
}
getStatus();
}
}
function processLog() {
if (currentLogs.length == 0) {
getStatus();
return;
}
currentLog = currentLogs.shift();
var taskOffsets = offsets[currentTaskID];
if (!(currentLog in taskOffsets)) {
taskOffsets[currentLog] = 0;
}
outputLog();
}
function outputLog() {
var currentOffset = offsets[currentTaskID][currentLog];
var currentSize = currentInfo.logs[currentLog];
if (currentSize > currentOffset) {
var chunkSize = CHUNK_SIZE;
if ((currentSize - currentOffset) < chunkSize) {
chunkSize = currentSize - currentOffset;
}
var req = new XMLHttpRequest();
req.open("GET", baseURL + "/getfile?taskID=" + currentTaskID + "&name=" + currentLog +
"&offset=" + currentOffset + "&size=" + chunkSize, true);
req.onreadystatechange = handleLog;
req.send(null);
if (headerElement != null) {
logElement.removeChild(headerElement);
headerElement = null;
}
} else {
processLog();
}
}
function handleLog(event) {
req = event.target;
if (req.readyState != 4) {
return;
}
if (req.status == 200) {
content = req.responseText;
offsets[currentTaskID][currentLog] += content.length;
if (content.length > 0) {
docHeight = document.body.clientHeight;
currlog = currentTaskID + ":" + currentLog;
if (currlog != lastlog) {
logElement.appendChild(document.createTextNode("\n==> " + currlog + " <==\n"));
lastlog = currlog;
}
logElement.appendChild(document.createTextNode(content));
maybeScroll(docHeight);
}
} else {
popupError("Error retrieving " + currentLog + " for task " + currentTaskID + ": " + req.statusText);
}
outputLog();
}
function popupError(msg) {
errorCount++;
alert(msg);
}
function watchLogs(element) {
logElement = document.getElementById(element);
headerElement = logElement.firstChild;
tasks = parseTasklist();
for (var i=0; i<tasks.length; i++) {
offsets[tasks[i]] = {};
}
setTimeout(checkTasks, 1000);
}
|