share/collection.js: Add legend label formatter.
[collection4.git] / share / collection.js
index 2cb6f30..05ca9ba 100644 (file)
@@ -1,12 +1,75 @@
+/**
+ * collection4 - collection.js
+ * Copyright (C) 2010  Florian octo Forster
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Florian octo Forster <ff at octo.it>
+ **/
+
 var c4 =
 {
   instances: []
 };
 
-function graph_get_params (graph)
+function value_to_string (value) /* {{{ */
+{
+  var abs_value;
+  var v2s = function (value)
+  {
+    var tmp = Math.round (100.0 * value) / 100.0;
+    return ("" + tmp);
+  }
+
+  if (value == null)
+    return ('NaN');
+  else if (value == 0)
+    return ('0');
+
+  abs_value = Math.abs (value);
+
+  if ((abs_value < 10000) && (abs_value >= 0.1))
+    return (v2s (value));
+  else if (abs_value > 1)
+  {
+    if (abs_value < 10000000)
+      return (v2s (value / 1000) + "k");
+    else if (abs_value < 10000000000)
+      return (v2s (value / 1000000) + "M");
+    else if (abs_value < 10000000000000)
+      return (v2s (value / 1000000000) + "G");
+    else
+      return (v2s (value / 1000000000000) + "T");
+  }
+  else
+  {
+    if (abs_value >= 0.001)
+      return (v2s (value * 1000) + "m");
+    else if (abs_value >= 0.000001)
+      return (v2s (value * 1000000) + "u");
+    else
+      return (v2s (value * 1000000000) + "n");
+  }
+} /* }}} function value_to_string */
+
+function instance_get_params (inst) /* {{{ */
 {
-  var graph_selector = graph.graph_selector;
-  var inst_selector = graph.instance_selector;
+  var graph_selector = inst.graph_selector;
+  var inst_selector = inst.instance_selector;
   var selector = {};
 
   if (graph_selector.host == inst_selector.host)
@@ -60,9 +123,9 @@ function graph_get_params (graph)
   }
 
   return (selector);
-} /* graph_get_params */
+} /* }}} instance_get_params */
 
-function ident_clone (ident)
+function ident_clone (ident) /* {{{ */
 {
   var ret = {};
 
@@ -73,13 +136,13 @@ function ident_clone (ident)
   ret.type_instance = ident.type_instance;
 
   return (ret);
-} /* ident_clone */
+} /* }}} ident_clone */
 
-function json_graph_get_def (graph)
+function inst_get_defs (inst) /* {{{ */
 {
-  if (!graph.def)
+  if (!inst.def)
   {
-    var params = ident_clone (graph.graph_selector);
+    var params = instance_get_params (inst);
     params.action = "graph_def_json";
 
     $.ajax({
@@ -92,82 +155,237 @@ function json_graph_get_def (graph)
         if (!data)
           return;
 
-        graph.def = data;
+        inst.def = data;
       }});
   }
 
-  if (graph.def)
-    return (graph.def);
+  if (inst.def)
+    return (inst.def);
   return;
-} /* json_graph_get_def */
+} /* }}} inst_get_defs */
+
+function ident_matches (selector, ident) /* {{{ */
+{
+  var part_matches = function (s,p)
+  {
+    if (s == null)
+      return (false);
+
+    if ((s == "/any/") || (s == "/all/"))
+      return (true);
+
+    if (p == null)
+      return (false);
+
+    if (s == p)
+      return (true);
+
+    return (false);
+  };
+
+  if (!part_matches (selector.host, ident.host))
+    return (false);
+
+  if (!part_matches (selector.plugin, ident.plugin))
+    return (false);
+
+  if (!part_matches (selector.plugin_instance, ident.plugin_instance))
+    return (false);
+
+  if (!part_matches (selector.type, ident.type))
+    return (false);
+
+  if (!part_matches (selector.type_instance, ident.type_instance))
+    return (false);
+
+  return (true);
+} /* }}} function ident_matches */
+
+function ident_describe (ident, selector) /* {{{ */
+{
+  var ret = "";
+  var check_field = function (field)
+  {
+    if (ident[field].toLowerCase () != selector[field].toLowerCase ())
+    {
+      if (ret != "")
+        ret += "/";
+      ret += ident[field];
+    }
+  };
+
+  check_field ("host");
+  check_field ("plugin");
+  check_field ("plugin_instance");
+  check_field ("type");
+  check_field ("type_instance");
+
+  if (ret == "")
+    return (null);
+  return (ret);
+} /* }}} function ident_describe */
+
+function def_draw_one (def, data, chart_opts) /* {{{ */
+{
+  var chart_series = new Object ();
+
+  chart_series.type = 'line';
+  chart_series.pointInterval = data.interval * 1000;
+  chart_series.pointStart = data.first_value_time * 1000;
+  chart_series.data = data.data;
+  chart_series.lineWidth = 1;
+  chart_series.shadow = false;
+  chart_series.marker = { enabled: false };
+
+  if (def.legend)
+    chart_series.name = def.legend;
+  else if (def.ds_name)
+    chart_series.name = def.ds_name;
+
+  if (def.area)
+    chart_series.type = 'area';
+
+  if (def.stack)
+    chart_series.stacking = 'normal';
 
-function json_graph_update (index)
+  if ((def.color) && (def.color != 'random'))
+    chart_series.color = def.color;
+
+  chart_opts.series.push (chart_series);
+} /* }}} function def_draw_one */
+
+function def_draw (def, data_list, chart_opts) /* {{{ */
+{
+  var i;
+
+  for (i = 0; i < data_list.length; i++)
+  {
+    if ((def.ds_name) && (def.ds_name != data_list[i].data_source))
+      continue;
+    if (!ident_matches (def.select, data_list[i].file))
+      continue;
+
+    def_draw_one (def, data_list[i], chart_opts);
+  }
+} /* }}} function def_draw */
+
+function instance_draw (inst, def, data_list) /* {{{ */
 {
-  var graph;
+  var x_data = [];
+  var y_data = [];
+  var i;
+
+  var chart_opts = new Object ();
+
+  if (!inst || !def || !data_list)
+    return;
+
+  chart_opts.chart =
+  {
+    renderTo: inst.container,
+    zoomType: 'x'
+  };
+  chart_opts.xAxis =
+  {
+    type: 'datetime',
+    maxZoom: 300, // five minutes
+    title: { text: null }
+  };
+  chart_opts.yAxis =
+  {
+    labels:
+    {
+      formatter: function () { return (value_to_string (this.value)); }
+    },
+    endOnTick: false
+  };
+  chart_opts.legend =
+  {
+    labelFormatter: function ()
+    {
+      var series = this;
+      var min = Number.MAX_VALUE;
+      var max = Number.NEGATIVE_INFINITY;
+      var num = 0;
+      var sum = 0;
+      var avg;
+      var i;
+
+      for (i = 0; i < this.data.length; i++)
+      {
+        var v;
+
+        v = this.data[i].y;
+        if (v == null)
+          continue;
+
+        if (min > v)
+          min = v;
+        if (max < v)
+          max = v;
+
+        sum += v;
+        num++;
+      }
+
+      if (num == 0)
+      {
+        min = null;
+        max = null;
+        avg = null;
+      }
+      else
+      {
+        avg = sum / num;
+      }
+
+      return (this.name + " (" + value_to_string (min) + " min, "
+          + value_to_string (avg) + " avg, "
+          + value_to_string (max) + " max)");
+    }
+  };
+  chart_opts.series = new Array ();
+
+  if (def.title)
+    chart_opts.title = { text: def.title };
+
+  for (i = def.defs.length - 1; i >= 0; i--)
+    def_draw (def.defs[i], data_list, chart_opts);
+
+//  if ((def.defs.length == 0) && (data_list.length == 1))
+//    def_draw_one (null, data_list[0], chart_opts);
+
+  inst.chart = new Highcharts.Chart (chart_opts);
+} /* }}} function instance_draw */
+
+function json_graph_update (index) /* {{{ */
+{
+  var inst;
   var def;
   var params;
 
-  graph = c4.instances[index];
-  if (!graph)
+  inst = c4.instances[index];
+  if (!inst)
     return;
 
-  def = json_graph_get_def (graph);
+  def = inst_get_defs (inst);
   if (!def)
     return;
 
-  if (!graph.raphael)
-  {
-    graph.raphael = Raphael ("c4-graph" + index);
-  }
+  if (!inst.container)
+    inst.container = "c4-graph" + index;
 
-  params = graph_get_params (graph);
-  params.action = "graph_data_json";
-  params.begin = graph.begin;
-  params.end = graph.end;
+  params = instance_get_params (inst);
+  params.action = "instance_data_json";
+  params.begin = inst.begin;
+  params.end = inst.end;
 
   $.getJSON ("collection.fcgi", params,
       function (data)
       {
-        var x_data = [];
-        var y_data = [];
-        var i;
-
-        if (!data)
-          return;
-
-        for (i = 0; i < data.length; i++)
-        {
-          var ds = data[i];
-
-          var j;
-          var x = [];
-          var y = [];
-
-          for (j = 0; j < ds.data.length; j++)
-          {
-            var dp = ds.data[j];
-            var t = dp[0];
-            var v = dp[1];
-
-            if (v == null)
-              continue;
-
-            x.push (t);
-            y.push (v);
-          }
-
-          x_data.push (x);
-          y_data.push (y);
-        }
-
-        graph.raphael.clear ();
-        if (def.title)
-          graph.raphael.g.text (250, 15, def.title);
-        if (def.vertical_label)
-          graph.raphael.g.text (5, 100, def.vertical_label).rotate (270);
-        graph.raphael.g.linechart(50, 25, 500, 150, x_data, y_data, {axis: "0 0 1 1"});
+        instance_draw (inst, def, data);
       }); /* getJSON */
-} /* json_graph_update */
+} /* }}} json_graph_update */
 
 function format_instance(inst)
 {