show_instance action: Export graph and instance data in a more structured fashion.
[collection4.git] / share / collection.js
index 4aacdd1..2cb6f30 100644 (file)
@@ -1,3 +1,174 @@
+var c4 =
+{
+  instances: []
+};
+
+function graph_get_params (graph)
+{
+  var graph_selector = graph.graph_selector;
+  var inst_selector = graph.instance_selector;
+  var selector = {};
+
+  if (graph_selector.host == inst_selector.host)
+  {
+    selector.host = graph_selector.host;
+  }
+  else
+  {
+    selector.graph_host = graph_selector.host;
+    selector.inst_host = inst_selector.host;
+  }
+
+  if (graph_selector.plugin == inst_selector.plugin)
+  {
+    selector.plugin = graph_selector.plugin;
+  }
+  else
+  {
+    selector.graph_plugin = graph_selector.plugin;
+    selector.inst_plugin = inst_selector.plugin;
+  }
+
+  if (graph_selector.plugin_instance == inst_selector.plugin_instance)
+  {
+    selector.plugin_instance = graph_selector.plugin_instance;
+  }
+  else
+  {
+    selector.graph_plugin_instance = graph_selector.plugin_instance;
+    selector.inst_plugin_instance = inst_selector.plugin_instance;
+  }
+
+  if (graph_selector.type == inst_selector.type)
+  {
+    selector.type = graph_selector.type;
+  }
+  else
+  {
+    selector.graph_type = graph_selector.type;
+    selector.inst_type = inst_selector.type;
+  }
+
+  if (graph_selector.type_instance == inst_selector.type_instance)
+  {
+    selector.type_instance = graph_selector.type_instance;
+  }
+  else
+  {
+    selector.graph_type_instance = graph_selector.type_instance;
+    selector.inst_type_instance = inst_selector.type_instance;
+  }
+
+  return (selector);
+} /* graph_get_params */
+
+function ident_clone (ident)
+{
+  var ret = {};
+
+  ret.host = ident.host;
+  ret.plugin = ident.plugin;
+  ret.plugin_instance = ident.plugin_instance;
+  ret.type = ident.type;
+  ret.type_instance = ident.type_instance;
+
+  return (ret);
+} /* ident_clone */
+
+function json_graph_get_def (graph)
+{
+  if (!graph.def)
+  {
+    var params = ident_clone (graph.graph_selector);
+    params.action = "graph_def_json";
+
+    $.ajax({
+      url: "collection.fcgi",
+      async: false,
+      dataType: 'json',
+      data: params,
+      success: function (data)
+      {
+        if (!data)
+          return;
+
+        graph.def = data;
+      }});
+  }
+
+  if (graph.def)
+    return (graph.def);
+  return;
+} /* json_graph_get_def */
+
+function json_graph_update (index)
+{
+  var graph;
+  var def;
+  var params;
+
+  graph = c4.instances[index];
+  if (!graph)
+    return;
+
+  def = json_graph_get_def (graph);
+  if (!def)
+    return;
+
+  if (!graph.raphael)
+  {
+    graph.raphael = Raphael ("c4-graph" + index);
+  }
+
+  params = graph_get_params (graph);
+  params.action = "graph_data_json";
+  params.begin = graph.begin;
+  params.end = graph.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"});
+      }); /* getJSON */
+} /* json_graph_update */
+
 function format_instance(inst)
 {
   return ("<li class=\"instance\"><a href=\"" + location.pathname
@@ -51,7 +222,7 @@ function update_search_suggestions ()
   );
 } /* update_search_suggestions */
 
-function zoom_redraw (jq_obj)
+function zoom_redraw (jq_obj) /* {{{ */
 {
   var url = jq_obj.data ("base_url");
 
@@ -65,9 +236,9 @@ function zoom_redraw (jq_obj)
 
   jq_obj.attr ("src", url);
   return (true);
-}
+} /* }}} function zoom_redraw */
 
-function zoom_reset (graph_id, diff)
+function zoom_reset (graph_id, diff) /* {{{ */
 {
   var jq_obj;
   var end;
@@ -84,34 +255,34 @@ function zoom_reset (graph_id, diff)
   jq_obj.data ('end', end.toFixed (0));
 
   return (zoom_redraw (jq_obj));
-}
+} /* }}} function zoom_reset */
 
-function zoom_hour (graph_id)
+function zoom_hour (graph_id) /* {{{ */
 {
   zoom_reset (graph_id, 3600);
-}
+} /* }}} function zoom_hour */
 
-function zoom_day (graph_id)
+function zoom_day (graph_id) /* {{{ */
 {
   zoom_reset (graph_id, 86400);
-}
+} /* }}} function zoom_day */
 
-function zoom_week (graph_id)
+function zoom_week (graph_id) /* {{{ */
 {
   zoom_reset (graph_id, 7 * 86400);
-}
+} /* }}} function zoom_week */
 
-function zoom_month (graph_id)
+function zoom_month (graph_id) /* {{{ */
 {
   zoom_reset (graph_id, 31 * 86400);
-}
+} /* }}} function zoom_month */
 
-function zoom_year (graph_id)
+function zoom_year (graph_id) /* {{{ */
 {
   zoom_reset (graph_id, 366 * 86400);
-}
+} /* }}} function zoom_year */
 
-function zoom_relative (graph_id, factor_begin, factor_end)
+function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
 {
   var jq_obj;
   var end;
@@ -138,7 +309,7 @@ function zoom_relative (graph_id, factor_begin, factor_end)
   jq_obj.data ('end', end + (diff * factor_end));
 
   return (zoom_redraw (jq_obj));
-}
+} /* }}} function zoom_relative */
 
 function zoom_reference (graph_id) /* {{{ */
 {
@@ -246,6 +417,12 @@ $(document).ready(function() {
         + "</div>"
         );
     });
+
+    var i;
+    for (i = 0; i < c4.instances.length; i++)
+    {
+      json_graph_update (i);
+    }
 });
 
 /* vim: set sw=2 sts=2 et fdm=marker : */