viewlog.js 10.5 KB
Newer Older
Marco Mariani's avatar
Marco Mariani committed
1 2 3 4 5 6 7 8
/*jslint undef: true */
/*global $, document, window, processState, getCookie, setCookie, setSpeed, $SCRIPT_ROOT */
/*global openedlogpage: true */
/* vim: set et sts=4: */

$(document).ready(function () {
    "use strict";

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    // Current_log is not used for auto displaying mode, only for manual reload of log file!!!
    var current_log = 'instance.log',
        sending,
        state,
        selectedFile = "",
        logfilelist = "instance_root/.log_list";

    var getCurrentLogFile = function () {
      if ( $("#manual").is(":checked") ) {
        return "";
      }
      if (current_log === "software.log") {
        return "software";
      }
      else if (current_log === "instance.log") {
        return "instance";
      }
      else {
        return "";
      }
    };

Marco Mariani's avatar
Marco Mariani committed
31 32 33 34 35 36 37 38 39 40 41
    function setupBox() {
        var state = $("#logconfigbox").css("display");
        if (state === "none") {
            $("#logconfigbox").slideDown("normal");
            $("#logheader").removeClass("hide");
            $("#logheader").addClass("show");
        } else {
            $("#logconfigbox").slideUp("normal");
            $("#logheader").removeClass("show");
            $("#logheader").addClass("hide");
        }
42
    }
Marco Mariani's avatar
Marco Mariani committed
43 44 45 46 47

    function updatelogBox() {
        if (processState === "Stopped" || processState === "Checking" || $("#manual").is(":checked")) {
            $("#salpgridLog").hide();
            $("#manualLog").show();
48 49 50 51
            $("#slapstate").hide();
            $("#openloglist").show();
            loadLog(current_log);
            $("#salpgridLog").empty();
Marco Mariani's avatar
Marco Mariani committed
52 53 54
        } else {
            $("#salpgridLog").show();
            $("#manualLog").hide();
55 56
            $("#slapstate").show();
            $("#openloglist").hide();
Marco Mariani's avatar
Marco Mariani committed
57
        }
58
    }
Marco Mariani's avatar
Marco Mariani committed
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 168 169 170
    function loadLog(filepath, $elt) {
      if (sending){
        return;
      }
      sending = true;
      var info = $("#logheader").html();
      $("#logheader").html("LOADING FILE... <img src='"+$SCRIPT_ROOT+"/static/images/loading.gif' />");
      var jqxhr = $.ajax({
          type: "POST",
          url: $SCRIPT_ROOT + '/getFileLog',
          data: {filename: filepath, truncate: 1500}
          })
        .done(function(data) {
          if (data.code === 0) {
            $("#error").Popup(data.result, {type: 'alert', duration: 5000});
          } else {
            $("#manualLog").empty();
            $("#manualLog").html(data.result);
            $("#logheader").empty();
            info = "Log from file " + filepath;
            $("#manualLog")
                .scrollTop($("#manualLog")[0].scrollHeight - $("#manualLog").height());
            current_log = filepath;
            if ($elt) {
              $("#openloglist ul li").each(function () {
                if ($(this).hasClass("checked")){
                  $(this).removeClass("checked");
                  return;
                }
              });
              $elt.addClass('checked');
            }
          }
        })
        .fail(function(jqXHR, exception) {
          if (jqXHR.status == 404) {
              $("#error").Popup("Requested page not found. [404]", {type: 'error'});
          } else if (jqXHR.status == 500) {
              $("#error").Popup("Internal Error. Cannot respond to your request, please check your parameters", {type: 'error'});
          } else {
              $("#error").Popup("An Error occured: \n" + jqXHR.responseText, {type: 'error'});
          }
        })
        .always(function() {
          sending = false;
          $("#logheader").html(info);
        });
    }

    function init() {
      openedlogpage = getCurrentLogFile();
      state = getCookie("autoUpdate");
      if (state) {
        $("#" + state).attr('checked', true);
        //updatelogBox();
        if (state === "manual") {
            openedlogpage = "";
            setSpeed(0);
        } else {
            setSpeed((state === "live") ? 100 : 2500);
        }
      } else {
          $("#slow").attr('checked', true);
      }
      $("#inlineViewer").colorbox({inline:true, width: "600px",
                      title: "Please select your log file",
                      onComplete:function () {
                        selectedFile = "";
                      }
      });
      //Load Custom file list if exist!
      $.ajax({
          type: "POST",
          url: $SCRIPT_ROOT + '/getFileContent',
          data: {file: logfilelist}
          })
        .done(function(data) {
          if (data.code === 0) {
            // nothing
          } else {
            var list = data.result.split('#'), i, filename;
            for (i = 0; i < list.length; i += 1) {
              if ( list[i] === "" ) {
                continue;
              }
              filename = list[i].replace(/^.*(\\|\/|\:)/, '');
              $("#openloglist ul").append("<li rel='" + list[i] + "'>" +
                    "<span class='bt_close' title='Remove this element!'" +
                    "style='display:none'>×</span>" + filename + "</li>");
            }
          }
        })
        .fail(function(jqXHR, exception) {
          // nothing
        })
        .always(function() {
          $("#openloglist ul li").click(function () {
            var logfile = $(this).attr('rel');
            if (current_log === logfile){
              return false;
            }
            loadLog(logfile, $(this));
            return false;
          });
        });
    }

    function initTree(tree, path, key) {
      if (!key){
        key = '0';
      }
Alain Takoudjou's avatar
Alain Takoudjou committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      $.ajax({
        type: "POST",
        url: $SCRIPT_ROOT + '/getPath',
        data: {file: "instance_root"}
        })
      .done(function(data) {
        if (data.code === 1) {
          $(tree).fancytree({
            activate: function(event, data) {
              var node = data.node;
            },
            click: function(event, data) {
              if (!data.node.isFolder()){
                selectedFile = data.node.data.path;
              }
            },
            source: {
              url: $SCRIPT_ROOT + "/fileBrowser",
              data:{opt: 20, dir: path, key: key, listfiles: 'yes'},
              cache: false
            },
            lazyload: function(event, data) {
              var node = data.node;
              data.result = {
                url: $SCRIPT_ROOT + "/fileBrowser",
                data: {opt: 20, dir: node.data.path , key: node.key, listfiles: 'yes'}
              }
            },
          });
        }
        else {
Alain Takoudjou's avatar
Alain Takoudjou committed
202
          logfilelist = "";
Alain Takoudjou's avatar
Alain Takoudjou committed
203
        }
204 205 206 207
      });
    }

    init();
Marco Mariani's avatar
Marco Mariani committed
208
    updatelogBox();
209
    initTree('#fileTree', "instance_root");
Marco Mariani's avatar
Marco Mariani committed
210 211 212 213 214 215

    $("#logheader").click(function () {
        setupBox();
    });

    $("#manual").change(function () {
216
      if ( $(this).is(':checked') ) {
Marco Mariani's avatar
Marco Mariani committed
217
        setCookie("autoUpdate", "manual");
218 219 220 221
        updatelogBox();
        openedlogpage = "";
      }
        /*
Marco Mariani's avatar
Marco Mariani committed
222 223 224 225
        if ($("input#type").val() === "instance") {
            window.location.href = $SCRIPT_ROOT + "/viewInstanceLog";
        } else {
            window.location.href = $SCRIPT_ROOT + "/viewSoftwareLog";
226
        }*/
Marco Mariani's avatar
Marco Mariani committed
227 228 229
    });

    $("#live").change(function () {
230
      if ( $(this).is(':checked') ) {
Marco Mariani's avatar
Marco Mariani committed
231 232 233
        updatelogBox();
        setSpeed(100);
        setCookie("autoUpdate", "live");
234
      }
Marco Mariani's avatar
Marco Mariani committed
235 236 237
    });

    $("#slow").change(function () {
238
      if ( $(this).is(':checked') ) {
Marco Mariani's avatar
Marco Mariani committed
239 240 241
        updatelogBox();
        setSpeed(2500);
        setCookie("autoUpdate", "slow");
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
      }
    });

    $("#refreshlog").click(function () {
      loadLog(current_log);
      return false;
    });

    $("#addfile").click(function () {
      var filename;
      $.colorbox.close();
      if (selectedFile !== "") {
        $.ajax({
          type: "POST",
          url: $SCRIPT_ROOT + '/checkFileType',
          data: {path: selectedFile}
          })
        .done(function(data) {
          if (data.code === 0) {
            $("#error").Popup(data.result, {type: 'alert', duration: 5000});
          } else {
            filename = selectedFile.replace(/^.*(\\|\/|\:)/, '');
            $("#openloglist ul").append("<li rel='" + selectedFile + "'>" +
                    "<span class='bt_close' title='Remove this element!'" +
                    ">×</span>" + filename + "</li>");
          }
        })
        .fail(function(jqXHR, exception) {
          $("#error").Popup("An Error occured: \n" + jqXHR.responseText, {type: 'error'});
        });
      }
      else{
        $("#error").Popup("No log file selected!", {type: 'alert', duration: 3000});
      }
Alain Takoudjou's avatar
Alain Takoudjou committed
276
      return false;
277 278 279
    });

    $("#logEdit").click(function () {
Alain Takoudjou's avatar
Alain Takoudjou committed
280 281 282 283 284
      if ( logfilelist === "" ) {
        $("#error").Popup("You don't have any services yet! Please run your services to choose custom log files",
                    {type: 'alert', duration: 5000});
        return false;
      }
285 286 287 288 289 290 291 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 330 331
      if ( $(this).text() === "Save list") {
        $(this).text("Edit list");
        $("#addbox").hide();
        $("#openloglist ul li").click(function () {
          var logfile = $(this).attr('rel');
          if (current_log === logfile){
            return false;
          }
          loadLog(logfile, $(this));
          return false;
        });
        var savelist = "";
        $("#openloglist ul li").each(function () {
          $(this).children( "span" ).hide();
          $(this).children( "span" ).unbind('click');
          if ( $(this).children('span').length > 0 ) {
            savelist += $(this).attr('rel') + "#"
          }
        });
        //Send result to be saved!!
        $.ajax({
          type: "POST",
          url: $SCRIPT_ROOT + '/saveFileContent',
          data: {file: logfilelist, content: savelist}
          })
        .done(function(data) {
          if (data.code === 0) {
            $("#error").Popup(data.result, {type: 'error', duration: 5000});
          } else {
            $("#error").Popup("Done! Your log file list has been saved.", {type: 'confirm', duration: 5000});
          }
        })
        .fail(function(jqXHR, exception) {
          $("#error").Popup("An Error occured: \n" + jqXHR.responseText, {type: 'error'});
        });
      }
      else {
        $(this).text("Save list");
        $("#openloglist ul li").unbind('click');
        $("#addbox").show();
        $("#openloglist ul li").each(function () {
          $(this).children( "span" ).show();
          $(this).children( "span" ).click(function () {
            $(this).parent().remove();
          });
        });
      }
Alain Takoudjou's avatar
Alain Takoudjou committed
332
      return false;
333 334 335 336 337 338 339 340 341
    });

    $("#slapswitch").click( function () {
      if ( $(this).attr('rel') === 'opend' ) {
        window.location.href = $SCRIPT_ROOT + "/inspectInstance";
      }
      else {
        $("#softrun").click();
      }
Alain Takoudjou's avatar
Alain Takoudjou committed
342
      return false;
Marco Mariani's avatar
Marco Mariani committed
343 344 345
    });

});