<commit_before>/*
 *
 *   Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you under the Apache License,
 *   Version 2.0 (the "License"); you may not use this file except
 *   in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on anselectedRevision
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

var selectedRevisionLogMap = {};
var selectedRevisionReplicaList = [];
var selectedReplica;
var editor;
var isLogsAvailable = false;
$(document).ready(function () {
    editor = CodeMirror.fromTextArea(document.getElementById("build-logs"), {
        styleActiveLine: true,
        lineNumbers: true,
        readOnly: true,
        searchonly: true,
        lineWrapping: true,
        theme:'icecoder'
    });
    initData(selectedRevision, true);
    setInterval(function(){ initData(selectedRevision, false); }, 3000);
});

function regerateReplicasList(selectedRevisionReplicaList) {
    $('#replicas').empty();
    for (var i = 0; i < selectedRevisionReplicaList.length; i++) {
        var $option = $('<option value="' + selectedRevisionReplicaList[i] + '">' + selectedRevisionReplicaList[i] + '</option>');
        if (i == 0) {
            selectedReplica = selectedRevisionReplicaList[i];
            $option.attr('selected', 'selected');
        }
        $('#replicas').append($option);
    }
}

function setLogArea(logVal, isFirstRequest){
    $('#build-logs').empty();
    editor.setValue("");
    if(!isFirstRequest) {
        var currentLog = $('#build-logs').val();
        logVal = currentLog + logVal;
    }
    $('#build-logs').val(logVal);
    editor.setValue(logVal);
    var scroller = editor.getScrollInfo();
    editor.scrollTo(0, scroller.height);
    $('.log-search').focus();
}

function initData(selectedRevision, isFirstRequest){
    if(isFirstRequest){
        $('#replicas').empty();
        setLogArea("Loading...", true);
    }
    jagg.post("../blocks/runtimeLogs/ajax/runtimeLogs.jag", {
        action:"getSnapshotLogs",
        applicationKey:applicationKey,
        selectedRevision:selectedRevision,
        isFirstRequest:isFirstRequest
    },function (result) {
        initelements();
        result = result.replace(/\t+/g, "    ");
        selectedRevisionLogMap = jQuery.parseJSON(result);
        if(!jQuery.isEmptyObject(selectedRevisionLogMap)){
            $("#log-download").removeClass("btn-action btn disabled").addClass("btn-action");
            selectedRevisionReplicaList = Object.keys(selectedRevisionLogMap);
            if(isFirstRequest){
                regerateReplicasList(selectedRevisionReplicaList);
            }
            setLogArea(selectedRevisionLogMap[selectedRevisionReplicaList[0]], isFirstRequest);
        } else {
            //Check for application revision status and display correct message
            jagg.post("../blocks/runtimeLogs/ajax/runtimeLogs.jag", {
                action: "getApplicationRevisionStatus",
                applicationKey: applicationKey,
                selectedRevision: selectedRevision
            }, function(result) {
                result = result.trim();
                var revisionStatus = result;
                if (revisionStatus == "stopped") {
                    jagg.message({
                        content: "Application is stopped. Please go to back to the application and click the" +
                        " Start button.",
                        type: 'information',
                        id: 'view_log',
                        timeout: '5000'
                    });
                } else {
                    jagg.message({
                        content: "Deployment in progress. Please wait",
                        type: 'information',
                        id: 'view_log',
                        timeout: '5000'
                    });
                }
            }, function(jqXHR, textStatus, errorThrown) {
                jagg.message({
                    content: "Error occured while getting application revision status.",
                    type: 'error',
                    id: 'view_log'
                });
            });
        }
    },function (jqXHR, textStatus, errorThrown) {
        $('#revision').prop("disabled", false);
        jagg.message({content: "Error occurred while loading the logs.", type: 'error', id:'view_log'});
    });
}

function initelements(){
    //Url text loaded from the span element
    var urlText = $('.version-url a span').text();<commit_msg>Fixing APPCLOUD-173<commit_after>/*
 *
 *   Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you under the Apache License,
 *   Version 2.0 (the "License"); you may not use this file except
 *   in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on anselectedRevision
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

var selectedRevisionLogMap = {};
var selectedRevisionReplicaList = [];
var selectedReplica;
var editor;
var isLogsAvailable = false;
var timerId;
$(document).ready(function () {
    editor = CodeMirror.fromTextArea(document.getElementById("build-logs"), {
        styleActiveLine: true,
        lineNumbers: true,
        readOnly: true,
        searchonly: true,
        lineWrapping: true,
        theme:'icecoder'
    });
    initData(selectedRevision, true);
    timerId = setInterval(function(){ initData(selectedRevision, false); }, 3000);
});

function regerateReplicasList(selectedRevisionReplicaList) {
    $('#replicas').empty();
    for (var i = 0; i < selectedRevisionReplicaList.length; i++) {
        var $option = $('<option value="' + selectedRevisionReplicaList[i] + '">' + selectedRevisionReplicaList[i] + '</option>');
        if (i == 0) {
            selectedReplica = selectedRevisionReplicaList[i];
            $option.attr('selected', 'selected');
        }
        $('#replicas').append($option);
    }
}

function setLogArea(logVal, isFirstRequest){
    $('#build-logs').empty();
    editor.setValue("");
    if(!isFirstRequest) {
        var currentLog = $('#build-logs').val();
        logVal = currentLog + logVal;
    }
    $('#build-logs').val(logVal);
    editor.setValue(logVal);
    var scroller = editor.getScrollInfo();
    editor.scrollTo(0, scroller.height);
    $('.log-search').focus();
}

function initData(selectedRevision, isFirstRequest){
    if(isFirstRequest){
        $('#replicas').empty();
        setLogArea("Loading...", true);
    }
    jagg.post("../blocks/runtimeLogs/ajax/runtimeLogs.jag", {
        action:"getSnapshotLogs",
        applicationKey:applicationKey,
        selectedRevision:selectedRevision,
        isFirstRequest:isFirstRequest
    },function (result) {
        initelements();
        result = result.replace(/\t+/g, "    ");
        selectedRevisionLogMap = jQuery.parseJSON(result);
        if(!jQuery.isEmptyObject(selectedRevisionLogMap)){
            $("#log-download").removeClass("btn-action btn disabled").addClass("btn-action");
            selectedRevisionReplicaList = Object.keys(selectedRevisionLogMap);
            if(isFirstRequest){
                regerateReplicasList(selectedRevisionReplicaList);
            }
            setLogArea(selectedRevisionLogMap[selectedRevisionReplicaList[0]], isFirstRequest);
        } else {
            //Check for application revision status and display correct message
            jagg.post("../blocks/runtimeLogs/ajax/runtimeLogs.jag", {
                action: "getApplicationRevisionStatus",
                applicationKey: applicationKey,
                selectedRevision: selectedRevision
            }, function(result) {
                result = result.trim();
                var revisionStatus = result;
                if (revisionStatus == "stopped") {
                clearInterval(timerId);
                    jagg.message({
                        content: "Application is currently stopped, logs will be available after restarting.",
                        type: 'information',
                        id: 'view_log',
                        timeout: '20000'
                    });
                    setLogArea("Application is stopped, Since logs are currently not available.", true);
                } else {
                    jagg.message({
                        content: "Deployment in progress. Please wait",
                        type: 'information',
                        id: 'view_log',
                        timeout: '8000'
                    });
                }
            }, function(jqXHR, textStatus, errorThrown) {
                jagg.message({
                    content: "Error occurred while getting application revision status.",
                    type: 'error',
                    id: 'view_log'
                });
            });
        }
    },function (jqXHR, textStatus, errorThrown) {
        $('#revision').prop("disabled", false);
        jagg.message({content: "Error occurred while loading the logs.", type: 'error', id:'view_log'});
    });
}

function initelements(){
    //Url text loaded from the span element
    var urlText = $('.version-url a span').text();
{'fraction_non_alphanumeric': 0.12114158485983201, 'fraction_numerical': 0.005869851229632629, 'mean_word_length': 2.2368162463151, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2148674', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2543, 'n_words': 625}