index.html 10.5 KB
Newer Older
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
<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;
}

.iconButton {
    height: 2.2em;
    width: 2.2em;
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
.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;
}

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
.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 = {};
83 84
let logView = null;
let menuView = null;
85 86 87 88

function init() {
    scheduleTable.style.display = 'none';
    document.body.appendChild(scheduleTable);
89
    scheduleTable.appendChild(makeRow(['ID', 'Created At', 'Path', 'Template', 'State', 'Actions'], 'th'));
90 91 92

    document.addEventListener("keydown", function(event) {
        if (event.which == 27) {
93 94
            closeLogView();
            closeJobMenu();
95
        }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    });
    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);
114 115
}

116

117 118 119
function onStatusGet(status) {
    scheduleTable.style.display = 'block';

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

    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) {
        const row = makeRow([i, '', '', '', '', '']);
        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];
168 169 170
        }

        const row = schedule[s.id].row;
171 172 173
        row.cells[1].innerText = s.added;
        row.cells[2].innerText = s.path;
        row.cells[3].innerText = s.template;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        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';
        }
    }

}


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
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)));
        menuView.appendChild(makeEntry('↓ Download results sql', () => getResultSql(jobId)));
    }

    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:
        const b = menuView.getBoundingClientRect();
        const r = event.target.getBoundingClientRect();
        const vh = window.innerHeight || document.documentElement.clientHeight;
        const vw = window.innerWidth || document.documentElement.clientWidth;

        menuView.style.width = b.width + 'px';
        menuView.style.height = b.height + 'px';
        menuView.style.top = Math.min(r.y, vh - b.height - 20) + 'px';
        menuView.style.left = Math.min(r.x, vw - b.width - 20) + 'px';
    };

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


258 259 260 261 262 263 264
function restartJob(jobId) {
    const xhttp = new XMLHttpRequest();
    xhttp.open("GET", "/restart_test/" + jobId, true);
    xhttp.send();
}


265 266 267 268 269 270 271 272 273 274
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');
}
275

276 277

function viewJobLogs(jobId) {
278 279 280
    const logIds = [0,1,2,3];
    const logNames = ['make stdout', 'make stderr', 'test stdout', 'test stderr'];

281 282 283 284 285 286 287 288 289 290 291
    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;

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
    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';
        };
    }
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

    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;
350 351 352 353 354 355
}


function requestStatus() {
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
356
        if (this.readyState == 4) {
357
            setTimeout(requestStatus, 1000);
358 359 360
            if (this.status == 200) {
                onStatusGet(JSON.parse(this.responseText));
            }
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        }
    };
    xhttp.open("GET", "/status", true);
    xhttp.send();
}


function makeCell(text, tagName='td') {
    const cell = document.createElement(tagName);
    cell.appendChild(document.createTextNode(text));
    return cell;
};


function makeRow(list, cellTagName='td') {
    const headerRow = document.createElement('tr');
    (list.map((n) => makeCell(n, cellTagName))).forEach((n) => headerRow.appendChild(n));
    return headerRow;
};

381

382 383 384 385 386 387 388
init();

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