<commit_before>var currentServersSpec = null;
var currentHostname = null;
var serverItemsVersion = 0;

var STATE_LATENT = 0;
var STATE_DOWN = 1;
var STATE_NOT_SERVING = 2;
var STATE_SERVING = 3;

function makeServersList()
{
    var serverList = new Array();

    var specs = (systemConfig.serversSpec.length > 0) ? systemConfig.serversSpec.split(",") : new Array();
    var foundUs = false;
    for ( var i = 0; i < specs.length; ++i )
    {
        var parts = specs[i].split(":");

        var thisSpec = {};
        var index = (parts.length > 2) ? 1 : 0;
        thisSpec.serverId = parseInt(parts[index++]);
        thisSpec.hostname = parts[index];
        if ( thisSpec.hostname === systemConfig.hostname )
        {
            foundUs = true;
        }

        serverList.push(thisSpec);
    }
    if ( !foundUs )
    {
        thisSpec = {};
        thisSpec.serverId = -1;
        thisSpec.hostname = systemConfig.hostname;
        serverList.push(thisSpec);
    }

    serverList.sort(function(a, b){
        return a.serverId - b.serverId;
    });

    return serverList;
}

function buildServerItems()
{
    var serversList = makeServersList();
    if ( (currentServersSpec != systemConfig.serversSpec) || (currentHostname != systemConfig.hostname) )
    {
        ++serverItemsVersion;
        internalBuildServerItems(serversList);
    }
    updateServerState(serversList);
}

function internalBuildServerItems(serversList)
{
    currentServersSpec = systemConfig.serversSpec;
    currentHostname = systemConfig.hostname;

    var content = "";
    for ( var i = 0; i < serversList.length; ++i )
    {
        var spec = serversList[i];
        var domId = 'cp-' + i;

        var thisItem = $('#base-server-item-base').html();
        thisItem = thisItem.replace("$SERVER_ID$", spec.serverId);
        thisItem = thisItem.replace("$SERVER_HOSTNAME$", spec.hostname);
        thisItem = thisItem.replace(/\$BASE_ID\$/gi, domId);

        content += thisItem;
    }
    $('#base-server-item').html(content);

    for ( i = 0; i < serversList.length; ++i )
    {
        spec = serversList[i];
        domId = '#cp-' + i;

        makeLightSwitch(domId + '-instance-restarts-enabled', null, true);
        makeLightSwitch(domId + '-cleanup-enabled', null, true);
        makeLightSwitch(domId + '-backups-enabled', null, true);
        if ( systemState.backupActive )
        {
            $(domId + '-backups-enabled-control').show();
        }

        var serverIdContent = "Server Id: " + spec.serverId;
        var hostnameContent = "Hostname: " + spec.hostname;
        if ( spec.hostname === systemConfig.hostname )
        {
            hostnameContent += '<span class="cp-this-server">(This server)</span>';
            $(domId + '-log-button').hide();
        }

        $(domId + '-server-id').html(serverIdContent);
        $(domId + '-hostname').html(hostnameContent);

        $(domId + '-power-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-alert"
            }
        }).click(stopStartDialog(spec.hostname));

        $(domId + '-4ltr-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-circle-zoomin"
            }
        }).click(word4ltrDialog(spec.hostname));

        $(domId + '-log-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-info"
            }
        }).click(logDialog(spec.hostname));
    }

    $('#base-server-item').colorTip();
}

function stopStartDialog(hostname)
{
    return function() {
        okCancelDialog(hostname, "Are you sure you want to restart this server?", function(){
            makeRemoteCall(URL_CLUSTER_RESTART_BASE, hostname);
        });
    };
}

function logDialog(hostname)
{
    $('#log-text').text("Loading...");
    return function() {
        makeRemoteCall(URL_CLUSTER_LOG_BASE, hostname, function(text){
            $('#log-text').text(text);
            $('#log-dialog').dialog("option", "title", hostname);
            $('#log-dialog').dialog("open");
        });
    };
}

function word4ltrDialog(hostname)
{
    $('#word-4ltr-text').text("Loading...");
    return function() {
        $('#word-4ltr-button').click(function(){
            makeRemoteCall(URL_CLUSTER_4LTR_BASE + $('#word-4ltr').val() + "/", hostname, function(text){
                $('#word-4ltr-text').text(text);
                $('#word-4ltr-dialog').dialog("option", "title", hostname);
                $('#word-4ltr-dialog').dialog("open");
            })
        });
    };
}

function makeRemoteCall(baseUrl, hostname, callback)
{
    $.getJSON(baseUrl + hostname, function(data){
        if ( data.success )
        {
            if ( callback )
            {
                callback(eval(data.response));
            }
        }
        else
        {
            messageDialog("Error", "Could not complete message to " + hostname + ". Message: " + data.errorMessage);
        }
    });
}

function handleSwitch(hostname, type)
{
    return function(isChecked) {
        makeRemoteCall(URL_CLUSTER_SET_CONFIG_BASE + type + "/" + isChecked + "/", hostname);
    };
}

function updateOneServerState(index, data, hostname)
{
    var statusColor;
    var statusMessage = "";

    domId = '#cp-' + index;
    if ( data.success )
    {
        var isRunning = ((data.response.state == STATE_SERVING) || (data.response.state == STATE_NOT_SERVING));

        $(domId + '-power-button').button("option", "disabled", false);
        $(domId + '-4ltr-button').button("option", "disabled", !isRunning);
        $(domId + '-log-button').button("option", "disabled", !isRunning);

        $(domId + '-power-button').button("option", "label", data.response.switches.restarts ? "Restart..." : "Stop...");

        ableLightSwitch(domId + '-instance-restarts-enabled', handleSwitch(hostname, "restarts"));
        ableLightSwitch(domId + '-cleanup-enabled', handleSwitch(hostname, "cleanup"));
        ableLightSwitch(domId + '-backups-enabled', handleSwitch(hostname, "backups"));

        checkLightSwitch(domId + '-instance-restarts-enabled', data.response.switches.restarts);
        checkLightSwitch(domId + '-cleanup-enabled', data.response.switches.cleanup);
        checkLightSwitch(domId + '-backups-enabled', data.response.switches.backups);
<commit_msg>Log button should always be active if the Exhibitor instance can be reached<commit_after>var currentServersSpec = null;
var currentHostname = null;
var serverItemsVersion = 0;

var STATE_LATENT = 0;
var STATE_DOWN = 1;
var STATE_NOT_SERVING = 2;
var STATE_SERVING = 3;

function makeServersList()
{
    var serverList = new Array();

    var specs = (systemConfig.serversSpec.length > 0) ? systemConfig.serversSpec.split(",") : new Array();
    var foundUs = false;
    for ( var i = 0; i < specs.length; ++i )
    {
        var parts = specs[i].split(":");

        var thisSpec = {};
        var index = (parts.length > 2) ? 1 : 0;
        thisSpec.serverId = parseInt(parts[index++]);
        thisSpec.hostname = parts[index];
        if ( thisSpec.hostname === systemConfig.hostname )
        {
            foundUs = true;
        }

        serverList.push(thisSpec);
    }
    if ( !foundUs )
    {
        thisSpec = {};
        thisSpec.serverId = -1;
        thisSpec.hostname = systemConfig.hostname;
        serverList.push(thisSpec);
    }

    serverList.sort(function(a, b){
        return a.serverId - b.serverId;
    });

    return serverList;
}

function buildServerItems()
{
    var serversList = makeServersList();
    if ( (currentServersSpec != systemConfig.serversSpec) || (currentHostname != systemConfig.hostname) )
    {
        ++serverItemsVersion;
        internalBuildServerItems(serversList);
    }
    updateServerState(serversList);
}

function internalBuildServerItems(serversList)
{
    currentServersSpec = systemConfig.serversSpec;
    currentHostname = systemConfig.hostname;

    var content = "";
    for ( var i = 0; i < serversList.length; ++i )
    {
        var spec = serversList[i];
        var domId = 'cp-' + i;

        var thisItem = $('#base-server-item-base').html();
        thisItem = thisItem.replace("$SERVER_ID$", spec.serverId);
        thisItem = thisItem.replace("$SERVER_HOSTNAME$", spec.hostname);
        thisItem = thisItem.replace(/\$BASE_ID\$/gi, domId);

        content += thisItem;
    }
    $('#base-server-item').html(content);

    for ( i = 0; i < serversList.length; ++i )
    {
        spec = serversList[i];
        domId = '#cp-' + i;

        makeLightSwitch(domId + '-instance-restarts-enabled', null, true);
        makeLightSwitch(domId + '-cleanup-enabled', null, true);
        makeLightSwitch(domId + '-backups-enabled', null, true);
        if ( systemState.backupActive )
        {
            $(domId + '-backups-enabled-control').show();
        }

        var serverIdContent = "Server Id: " + spec.serverId;
        var hostnameContent = "Hostname: " + spec.hostname;
        if ( spec.hostname === systemConfig.hostname )
        {
            hostnameContent += '<span class="cp-this-server">(This server)</span>';
            $(domId + '-log-button').hide();
        }

        $(domId + '-server-id').html(serverIdContent);
        $(domId + '-hostname').html(hostnameContent);

        $(domId + '-power-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-alert"
            }
        }).click(stopStartDialog(spec.hostname));

        $(domId + '-4ltr-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-circle-zoomin"
            }
        }).click(word4ltrDialog(spec.hostname));

        $(domId + '-log-button').button({
            disabled: true,
            icons:{
                primary:"ui-icon-info"
            }
        }).click(logDialog(spec.hostname));
    }

    $('#base-server-item').colorTip();
}

function stopStartDialog(hostname)
{
    return function() {
        okCancelDialog(hostname, "Are you sure you want to restart this server?", function(){
            makeRemoteCall(URL_CLUSTER_RESTART_BASE, hostname);
        });
    };
}

function logDialog(hostname)
{
    $('#log-text').text("Loading...");
    return function() {
        makeRemoteCall(URL_CLUSTER_LOG_BASE, hostname, function(text){
            $('#log-text').text(text);
            $('#log-dialog').dialog("option", "title", hostname);
            $('#log-dialog').dialog("open");
        });
    };
}

function word4ltrDialog(hostname)
{
    $('#word-4ltr-text').text("Loading...");
    return function() {
        $('#word-4ltr-button').click(function(){
            makeRemoteCall(URL_CLUSTER_4LTR_BASE + $('#word-4ltr').val() + "/", hostname, function(text){
                $('#word-4ltr-text').text(text);
                $('#word-4ltr-dialog').dialog("option", "title", hostname);
                $('#word-4ltr-dialog').dialog("open");
            })
        });
    };
}

function makeRemoteCall(baseUrl, hostname, callback)
{
    $.getJSON(baseUrl + hostname, function(data){
        if ( data.success )
        {
            if ( callback )
            {
                callback(eval(data.response));
            }
        }
        else
        {
            messageDialog("Error", "Could not complete message to " + hostname + ". Message: " + data.errorMessage);
        }
    });
}

function handleSwitch(hostname, type)
{
    return function(isChecked) {
        makeRemoteCall(URL_CLUSTER_SET_CONFIG_BASE + type + "/" + isChecked + "/", hostname);
    };
}

function updateOneServerState(index, data, hostname)
{
    var statusColor;
    var statusMessage = "";

    domId = '#cp-' + index;
    if ( data.success )
    {
        var isRunning = ((data.response.state == STATE_SERVING) || (data.response.state == STATE_NOT_SERVING));

        $(domId + '-power-button').button("option", "disabled", false);
        $(domId + '-4ltr-button').button("option", "disabled", !isRunning);
        $(domId + '-log-button').button("option", "disabled", false);

        $(domId + '-power-button').button("option", "label", data.response.switches.restarts ? "Restart..." : "Stop...");

        ableLightSwitch(domId + '-instance-restarts-enabled', handleSwitch(hostname, "restarts"));
        ableLightSwitch(domId + '-cleanup-enabled', handleSwitch(hostname, "cleanup"));
        ableLightSwitch(domId + '-backups-enabled', handleSwitch(hostname, "backups"));

        checkLightSwitch(domId + '-instance-restarts-enabled', data.response.switches.restarts);
        checkLightSwitch(domId + '-cleanup-enabled', data.response.switches.cleanup);
        checkLightSwitch(domId + '-backups-enabled', data.response.switches.backups);

{'fraction_non_alphanumeric': 0.1583887368009386, 'fraction_numerical': 0.0037543996871333596, 'mean_word_length': 2.441722745625841, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755326', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3806, 'n_words': 682}