dsci-runner.git | html/ | builds.html


    %s %s
    <div class="container">
        <div>
          <p class="title">DSCI Jobs</p>
           <hr>
          <p id="builds" id="data"></p>
        </div>
    </div>
</body>
</html>

<script>

var ip = location.host;
if (location.protocol == 'https:') {
    var proto = "wss"
} else {
    var proto = "ws"
}
    
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
    
function connect() {

    var ip = location.host;
    if (location.protocol == 'https:') {
      var proto = "wss"
    } else {
      var proto = "ws"
    }

    var ws = new WebSocket(`${proto}://${ip}/livebuilds`);

    ws.onopen = function() {
        console.log('ws - socket is open');
        // subscribe to some channels
        ws.send("Меня зовут Джон");
    };

    ws.onmessage = function(e) {
        //console.log('ws - data - ', e.data);
        const builds = JSON.parse(e.data)
        var p_cont = document.createElement('table');
        p_cont.setAttribute("id","builds_table");
        var table = document.createElement('table');
        table.setAttribute("class","table is-striped");
        var th = document.createElement('tr');
        var thd1 = document.createElement('td');
        thd1.appendChild(document.createTextNode('build'));
        var thd2 = document.createElement('td');
        thd2.appendChild(document.createTextNode('description'));
        var thd3 = document.createElement('td');
        thd3.appendChild(document.createTextNode('state'));
        th.appendChild(thd1);
        th.appendChild(thd2);
        th.appendChild(thd3);
        table.appendChild(th);          
        for (const build of builds) {
            var tr = document.createElement('tr');
            var td1 = document.createElement('td');
            var a = document.createElement('a');
            a.setAttribute("href","/report/ui2/" + build.Project + "/" + build.ID);
            //a.setAttribute("target","_blank");
            a.appendChild(document.createTextNode(build.Project + '@' + build.ID));
            td1.appendChild(a);
            var td2 = document.createElement('td');
            td2.appendChild(document.createTextNode(build.Description));

            var state = build.State;
            var build_st_str = "";
            if (state == 0 ){
                build_st_str = "unknown"
            }
            if (state == -1 ){
                build_st_str = "fail"
            }
            if (state == -11 ){
                build_st_str = "terminated"
            }
            if (state == 1 ){
                build_st_str = "ok"
            }
            if (state == 0 ){
                build_st_str = "run"
            }
            if (state == -2 ){
                build_st_str = "unknown"
            }
            //console.log(build);
            var td3 = document.createElement('td');
            td3.appendChild(document.createTextNode(build_st_str)); 
            tr.appendChild(td1);
            tr.appendChild(td2);
            tr.appendChild(td3);
            table.appendChild(tr);          
        }
        p_cont.appendChild(table) 
        var builds_obj = document.getElementById('builds');
        //builds_ws.replaceChildren(p_cont);
        builds_obj.innerHTML = p_cont.outerHTML
    }

    ws.onclose = function(e) {

        console.log('ws - socket is closed, reconnecting', e.reason);

        sleep(10000).then(() => {
            connect();
        });
    }    

    ws.onerror = function(err) {
        console.error('ws - socket encountered error: ', err.message, 'Closing socket');
        ws.close();
    };
}

connect();

</script>