// Lightstreamer Monitor Console Demo
// Table Management for the Statistics Section

//////////////// Statistics Table Management

  monitoringGroup = ["monitor_statistics"];
  monitoringSchema = [
    "MEMORY.TOTAL", "MEMORY.FREE",
    "THREADS", "POOL.SIZE", "POOL.ACTIVE", "POOL.WAITING", "POOL.QUEUE",
    "CLIENTS.CONNECTIONS", "CLIENTS.MAX_CONNECTIONS", "CLIENTS.SESSIONS",
    "ITEMS.TOTAL", "ITEMS.EVENTS_SEC", "ITEMS.FILTERED_EVENTS_SEC",
    "BANDWIDTH.TOTAL_BYTES", "BANDWIDTH.CURRENT", "BANDWIDTH.ALLOCATED"];
    // also available: "BANDWIDTH.CLIENT_AVERAGE", "CLIENTS.LAST_REFUSED_CONN",
    // "PUMP_WAIT.SLEEP", "PUMP_WAIT.NOTIFY", "PUMP_WAIT.EVAL"

  // create an OverwriteTable
  var monitoringTable = new OverwriteTable(monitoringGroup, monitoringSchema, "MERGE");

  monitoringTable.setDataAdapter("MONITOR");
  monitoringTable.setSnapshotRequired(true);

  // define visual effects and formatting
  monitoringTable.onChangingValues = function (itemPos, visualUpdateInfo, itemName) {
    if (visualUpdateInfo != null) {
      visualUpdateInfo.setHotTime(1000);
      visualUpdateInfo.setRowStyle("lshot", "lscold");
      for (var i = 0; i < monitoringSchema.length; i++) {
        var val = visualUpdateInfo.getFormattedValue(monitoringSchema[i]);
        if (val != null) {
          visualUpdateInfo.setFormattedValue(monitoringSchema[i], monitorFormat(val));
        }
      }
    }
  };

  // bind the table to the corresponding HTML elements
  pushPage.addTable(monitoringTable, "stats");


//////////////// Formatting Functions

  function monitorFormat(value) {
    var strVal = new String(value);
    if (strVal.length > 3) {
     var intCharsNum = strVal.indexOf(".");
      if (intCharsNum == -1) {
        intCharsNum = strVal.length;
      }
      if (intCharsNum <= 3) {
        return strVal;
      }
      var res = "";
      while (intCharsNum > 3) {
        res = "," + strVal.substr(intCharsNum-3,3) + res;
        intCharsNum -= 3;
      }
      res = strVal.substr(0,intCharsNum) + res;
      if (strVal.indexOf(".") > -1) {
        res += strVal.substr(strVal.indexOf("."));
      }
      return res;
    } else {
      return strVal;
    }
  }
