index.html 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test scheduler</title>
        <style>
body {
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}

table {
    border: 0px;
    border-spacing: 0px 3px;
}

td {
    padding: 1em;
    border: 0px;
    margin: 0px;
}

21 22 23 24
td.schedule-path, th.schedule-path {
    width: 20em;
}

25 26 27 28 29
.iconButton {
    height: 2.2em;
    width: 2.2em;
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
.menuEntry {
    display: block;
    color: whitesmoke;
    text-decoration: none;
    margin: .3em;
    padding: 1em;
}

.menuEntry:hover {
    background-color: darkslategray;
}

.menuView {
    position: absolute;
    display: block;
    background-color: black;
    padding: .5em;
    border-radius: .5em;
}

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
.logView {
    overflow-y: auto;
    border: 2px solid #ddd;
    top: 30px;
    bottom: 0px;
    right: 0px;
    left: 0px;
    padding: .4em;
    font-family: monospace;
    position: absolute;
}

.logViewWindow {
    position: fixed;
    float: left;
    left: 10%;
    right: 10%;
    top: 10%;
    bottom: 10%;
    background-color: white;
    width: 75%;
    height: 75%;
}

.logViewTab {
    height: 30px;
}
        </style>
    </head>
    <body>
        <script>
'use strict';

(() => {

const scheduleTable = document.createElement('table');
const schedule = {};
87 88
let logView = null;
let menuView = null;
89 90 91 92

function init() {
    scheduleTable.style.display = 'none';
    document.body.appendChild(scheduleTable);
93
    scheduleTable.appendChild(makeScheduleRow(['ID', 'Created At', 'Path', 'Template', 'State', 'Actions'], 'th'));
94 95 96

    document.addEventListener("keydown", function(event) {
        if (event.which == 27) {
97 98
            closeLogView();
            closeJobMenu();
99
        }
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    });
    const bodyClickListener = event => {
        const modals = [
            {element: menuView, callback: closeJobMenu},
            {element: logView, callback: closeLogView}
        ];
        for (const modal of modals) {
            if (modal.element === null || !document.body.contains(modal.element)) {
                continue;
            }
            if (!modal.element.contains(event.target)) {
                console.log("Clicked outside of " + modal.element.className);
                modal.callback();
            }
        }
    }

    document.addEventListener('click', bodyClickListener);
118 119
}

120

121 122 123
function onStatusGet(status) {
    scheduleTable.style.display = 'block';

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    const incomingIds = status.schedule.map(s => s.id);

    const newTasksIds = incomingIds.filter(i => !(i in schedule));
    const delTasksIds = Object.keys(schedule).filter(i => incomingIds.indexOf(i) == -1);

    // Delete tasks that are not on the server anymore
    for (const i of delTasksIds) {
        const row = schedule[i].row;
        row.parentElement.removeChild(row);
        delete schedule[i];
    }

    // Add rows for new incoming tasks
    for (const i of newTasksIds) {
139
        const row = makeScheduleRow([i, '', '', '', '', '']);
140 141 142 143 144
        const s = { id: i, row };
        schedule[s.id] = s;

        // Find out correct placement in table
        let ids = Object.keys(schedule);
145 146
        ids.sort(i => i | 0);
        
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
        const idx = ids.indexOf(s.id);
        if (idx == -1) {
            throw new Error(s.id + " NOT FOUND");
        } else if (idx == 0) {
            scheduleTable.appendChild(row);
        } else {
            const prevId = ids[idx-1];
            scheduleTable.insertBefore(row, schedule[prevId].row);
        }

        // Add buttons for actions
        const lastCell = row.cells[row.cells.length - 1];
        function makeButton(icon, onClick) {
            const button = document.createElement('button');
            button.innerHTML = icon;
            button.onclick = onClick;
            button.className = 'iconButton'
            return button;
        }
        lastCell.appendChild(makeButton('...', (e) => showJobMenu(e, s.id)));
    }

    // Update already existing tasks
    for (const s of status.schedule) {
        for (const key in s) {
            schedule[s.id][key] = s[key];
173 174 175
        }

        const row = schedule[s.id].row;
176 177 178 179 180
        updateInnerText(row.cells[1], s.added.substr(0, 16)
            + '\n' + s.added.substr(17));
        updateInnerText(row.cells[2], s.path);
        updateInnerText(row.cells[3], s.template);
        updateInnerText(row.cells[4], s.state);
181 182 183 184 185 186 187 188 189 190 191 192 193 194
        if (s.state == 'FAILED') {
            row.style.backgroundColor = '#fcc';
        } else if (s.state == 'SUCCESSFUL') {
            row.style.backgroundColor = '#cfc';
        } else if (s.state == 'RUNNING') {
            row.style.backgroundColor = '#ff9';
        } else {
            row.style.backgroundColor = '#eee';
        }
    }

}


195 196 197 198 199 200 201
function updateInnerText(element, text) {
    if (element.innerText !== text) {
        element.innerText = text;
    }
}


202 203 204 205 206 207 208 209 210 211 212 213 214
function showJobMenu(event, jobId) {
    closeJobMenu();

    menuView = document.createElement('div');
    menuView.className = 'menuView';
    menuView.style.left = 0;
    menuView.style.top = 0;
    menuView.style.width = 'auto';
    menuView.style.height = 'auto';
    function makeEntry(label, onClick) {
        const button = document.createElement('a');
        button.innerHTML = label;
        button.onclick = (e) => {closeJobMenu(); onClick(e);};
215
        button.href = 'javascript:;';
216 217 218 219 220 221
        button.className = 'menuEntry';
        return button;
    }
    const st = schedule[jobId].state;
    if (st == 'SUCCESSFUL' || st == 'FAILED') {
        menuView.appendChild(makeEntry('↺ Retry', () => restartJob(jobId)));
222 223
    }
    if (st == 'SUCCESSFUL' || st == 'FAILED' || st == 'SCHEDULED') {
224 225
        menuView.appendChild(makeEntry('🗑️ Delete', () => deleteJob(jobId)));
    }
226
    if (st == 'RUNNING') {
227 228 229 230 231
        menuView.appendChild(makeEntry('🗴 Cancel', () => cancelJob(jobId)));
    }
    menuView.appendChild(makeEntry('🗎 View Logs', () => viewJobLogs(jobId)));
    if (st == 'SUCCESSFUL') {
        menuView.appendChild(makeEntry('↓ Download results zip', () => getResultZip(jobId)));
232
        menuView.appendChild(makeEntry('↓ Download results JSON', () => getResultJSON(jobId)));
233 234 235 236 237 238 239
    }

    function onLayout() {
        // This gets executed after the browser did the layout for the menu,
        // we have the size of the menu view, now we have to place it close to
        // the bounding box of the button that was clicked, without putting it
        // ouside the screen:
240
        const a = document.body.getBoundingClientRect();
241 242 243 244 245
        const b = menuView.getBoundingClientRect();
        const r = event.target.getBoundingClientRect();
        const vh = window.innerHeight || document.documentElement.clientHeight;
        const vw = window.innerWidth || document.documentElement.clientWidth;

246

247 248
        menuView.style.width = b.width + 'px';
        menuView.style.height = b.height + 'px';
249 250
        menuView.style.top = (-a.top + Math.min(r.y, vh - b.height - 20)) + 'px';
        menuView.style.left = (-a.left + Math.min(r.x, vw - b.width - 20)) + 'px';
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    };

    setTimeout(() => {
        // Execution needs to be delayed to prevent the ouside click event
        // from closing this window
        document.body.appendChild(menuView);
        setTimeout(onLayout, 1);
    }, 0);

}


function closeJobMenu() {
    if (menuView === null) {
        return;
    }
    while (menuView.childNodes.length > 0)
        menuView.removeChild(menuView.lastChild);
    if (menuView.parentElement)
        menuView.parentElement.removeChild(menuView);
    menuView = null;
}


275 276 277 278 279 280 281
function restartJob(jobId) {
    const xhttp = new XMLHttpRequest();
    xhttp.open("GET", "/restart_test/" + jobId, true);
    xhttp.send();
}


282 283 284 285 286 287 288 289 290
function deleteJob(jobId) {
    if (confirm("Confirm deleting job " + jobId + "?")) {
        const xhttp = new XMLHttpRequest();
        xhttp.open("GET", "/delete_test/" + jobId, true);
        xhttp.send();
    }
}


291 292 293 294 295 296 297 298 299 300
function cancelJob(jobId) {
    const xhttp = new XMLHttpRequest();
    xhttp.open("GET", "/cancel_test/" + jobId, true);
    xhttp.send();
}


function getResultZip(jobId) {
    window.open('/results/' + jobId + '/results.zip');
}
301

302

303 304 305 306 307
function getResultJSON(jobId) {
    window.open('/results/' + jobId + '/results.json');
}


308
function viewJobLogs(jobId) {
309 310 311
    const logIds = [0,1,2,3];
    const logNames = ['make stdout', 'make stderr', 'test stdout', 'test stderr'];

312 313 314 315 316 317 318 319 320 321 322
    logView = document.createElement('div');
    logView.className = 'logViewWindow';
    const closeLogViewButton = document.createElement('button');
    logView.appendChild(closeLogViewButton);
    closeLogViewButton.innerText = "X";
    closeLogViewButton.style.display = 'block';
    closeLogViewButton.style.right = '0px';
    closeLogViewButton.style.top = '0px';
    closeLogViewButton.style.position = 'absolute';
    closeLogViewButton.onclick = closeLogView;

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
    const tabs = logIds.map(logId => {
        const v = document.createElement('button');
        v.innerText = logNames[logIds.indexOf(logId)];
        v.className = 'logViewTab';
        logView.appendChild(v);
        return v;
    });

    logView.appendChild(document.createElement('br'));

    const views = logIds.map(logId => {
        const v = document.createElement('div');
        v.style.display = logId == logIds[0] ? 'block' : 'none';
        v.className = 'logView';
        logView.appendChild(v);
        const xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                let log = this.responseText;
                v.innerText = log;
                log = v.innerHTML;
                log = log.replace(/\n/g, '<br/>');
                v.innerHTML = log;
            }
        };
        xhttp.open("GET", "/view_log/" + jobId + "/" + logId, true);
        xhttp.send();
        return v;
    });

    for (const i in logIds) {
        const tab = tabs[i];
        const view = views[i];
        tab.onclick = () => {
            views.forEach(v => {v.style.display = 'none';});
            view.style.display = 'block';
        };
    }
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

    setTimeout(() => {
        // Execution needs to be delayed to prevent the ouside click event
        // from closing this window
        document.body.appendChild(logView);
    }, 0);
}


function closeLogView() {
    if (logView === null) {
        return;
    }
    while (logView.childElementCount > 1) {
        logView.removeChild(logView.lastElementChild);
    }
    if (logView.parentElement) {
        logView.parentElement.removeChild(logView);
    }
    logView = null;
381 382 383 384 385 386
}


function requestStatus() {
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
387
        if (this.readyState == 4) {
388
            setTimeout(requestStatus, 1000);
389 390 391
            if (this.status == 200) {
                onStatusGet(JSON.parse(this.responseText));
            }
392 393 394 395 396 397 398
        }
    };
    xhttp.open("GET", "/status", true);
    xhttp.send();
}


399
function makeScheduleRow(list, cellTagName='td') {
400
    const headerRow = document.createElement('tr');
401 402 403 404 405 406 407 408
    const classes = ['schedule-id', 'schedule-date', 'schedule-path',
        'schedule-template', 'schedule-state', 'schedule-actions'];
    (list.map((text, idx) => {
        const cell = document.createElement(cellTagName);
        cell.className = classes[idx];
        cell.appendChild(document.createTextNode(text));
        return cell;
    })).forEach((n) => headerRow.appendChild(n));
409 410 411
    return headerRow;
};

412

413 414 415 416 417 418
init();

requestStatus();
})();
        </script>
    </body>
419
</html>