index.html 10.9 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 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
        const s = { id: i, row };
        schedule[s.id] = s;

        // Find out correct placement in table
        let ids = Object.keys(schedule);
        ids.sort();
        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];
172 173 174
        }

        const row = schedule[s.id].row;
175 176
        row.cells[1].innerText = s.added.substr(0, 16)
            + '\n' + s.added.substr(17);
177 178
        row.cells[2].innerText = s.path;
        row.cells[3].innerText = s.template;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        row.cells[4].innerText = s.state;
        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';
        }
    }

}


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
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);};
        button.href = '#';
        button.className = 'menuEntry';
        return button;
    }
    const st = schedule[jobId].state;
    if (st == 'SUCCESSFUL' || st == 'FAILED') {
        menuView.appendChild(makeEntry('↺ Retry', () => restartJob(jobId)));
        menuView.appendChild(makeEntry('🗑️ Delete', () => deleteJob(jobId)));
    }
    if (st == 'RUNNING' || st == 'SCHEDULED') {
        menuView.appendChild(makeEntry('🗴 Cancel', () => cancelJob(jobId)));
    }
    menuView.appendChild(makeEntry('🗎 View Logs', () => viewJobLogs(jobId)));
    if (st == 'SUCCESSFUL') {
        menuView.appendChild(makeEntry('↓ Download results zip', () => getResultZip(jobId)));
222
        menuView.appendChild(makeEntry('↓ Download results JSON', () => getResultJSON(jobId)));
223 224 225 226 227 228 229
    }

    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:
230
        const a = document.body.getBoundingClientRect();
231 232 233 234 235
        const b = menuView.getBoundingClientRect();
        const r = event.target.getBoundingClientRect();
        const vh = window.innerHeight || document.documentElement.clientHeight;
        const vw = window.innerWidth || document.documentElement.clientWidth;

236

237 238
        menuView.style.width = b.width + 'px';
        menuView.style.height = b.height + 'px';
239 240
        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';
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    };

    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;
}


265 266 267 268 269 270 271
function restartJob(jobId) {
    const xhttp = new XMLHttpRequest();
    xhttp.open("GET", "/restart_test/" + jobId, true);
    xhttp.send();
}


272 273 274 275 276 277 278 279 280 281
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');
}
282

283

284 285 286 287 288
function getResultJSON(jobId) {
    window.open('/results/' + jobId + '/results.json');
}


289
function viewJobLogs(jobId) {
290 291 292
    const logIds = [0,1,2,3];
    const logNames = ['make stdout', 'make stderr', 'test stdout', 'test stderr'];

293 294 295 296 297 298 299 300 301 302 303
    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;

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
    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';
        };
    }
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    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;
362 363 364 365 366 367
}


function requestStatus() {
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
368
        if (this.readyState == 4) {
369
            setTimeout(requestStatus, 1000);
370 371 372
            if (this.status == 200) {
                onStatusGet(JSON.parse(this.responseText));
            }
373 374 375 376 377 378 379
        }
    };
    xhttp.open("GET", "/status", true);
    xhttp.send();
}


380
function makeScheduleRow(list, cellTagName='td') {
381
    const headerRow = document.createElement('tr');
382 383 384 385 386 387 388 389
    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));
390 391 392
    return headerRow;
};

393

394 395 396 397 398 399 400
init();

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