Code

show_instance action: Export graph and instance data in a more structured fashion.
authorFlorian Forster <octo@verplant.org>
Fri, 10 Sep 2010 11:25:08 +0000 (13:25 +0200)
committerFlorian Forster <octo@verplant.org>
Fri, 10 Sep 2010 11:25:08 +0000 (13:25 +0200)
share/collection.js
src/action_show_instance.c
src/graph_instance.h

index 86ba87d36a1d11ea1fc7613334448751d86a31e6..2cb6f306e28c841e2e75d8bb8b1a6bb05ff69f78 100644 (file)
@@ -1,16 +1,92 @@
 var c4 =
 {
-  graphs: []
+  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?action=graph_def_json;" + graph.params,
+      url: "collection.fcgi",
       async: false,
       dataType: 'json',
+      data: params,
       success: function (data)
       {
         if (!data)
@@ -25,12 +101,13 @@ function json_graph_get_def (graph)
   return;
 } /* json_graph_get_def */
 
-function json_graph_update(index)
+function json_graph_update (index)
 {
   var graph;
   var def;
+  var params;
 
-  graph = c4.graphs[index];
+  graph = c4.instances[index];
   if (!graph)
     return;
 
@@ -43,7 +120,12 @@ function json_graph_update(index)
     graph.raphael = Raphael ("c4-graph" + index);
   }
 
-  $.getJSON ("collection.fcgi?action=graph_data_json;" + graph.params + ";begin=-3600;end=0",
+  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 = [];
@@ -337,7 +419,7 @@ $(document).ready(function() {
     });
 
     var i;
-    for (i = 0; i < c4.graphs.length; i++)
+    for (i = 0; i < c4.instances.length; i++)
     {
       json_graph_update (i);
     }
index 0d6b5e2a7be98a16479033a6d9e298c97dba7ccd..92bbf1cb10991ca2d4db6f36d4d6241f00292ca3 100644 (file)
@@ -184,6 +184,88 @@ static int left_menu (void *user_data) /* {{{ */
   return (0);
 } /* }}} int left_menu */
 
+static int show_instance_json (graph_config_t *cfg, /* {{{ */
+    graph_instance_t *inst,
+    time_t begin, time_t end, int index)
+{
+  yajl_gen_config handler_config;
+  yajl_gen handler;
+  const unsigned char *json_buffer;
+  unsigned int json_buffer_length;
+
+  graph_ident_t *graph_selector;
+  graph_ident_t *inst_selector;
+
+  graph_selector = graph_get_selector (cfg);
+  if (graph_selector == NULL)
+    return (ENOMEM);
+
+  inst_selector = inst_get_selector (inst);
+  if (inst_selector == NULL)
+  {
+    ident_destroy (inst_selector);
+    return (ENOMEM);
+  }
+
+  memset (&handler_config, 0, sizeof (handler_config));
+  handler_config.beautify = 1;
+  handler_config.indentString = "  ";
+
+  handler = yajl_gen_alloc2 (/* callback = */ NULL,
+      &handler_config,
+      /* alloc functions = */ NULL,
+      /* context = */ NULL);
+  if (handler == NULL)
+  {
+    ident_destroy (inst_selector);
+    ident_destroy (graph_selector);
+    return (-1);
+  }
+
+  yajl_gen_map_open (handler);
+
+  yajl_gen_string (handler,
+      (unsigned char *) "graph_selector",
+      (unsigned int) strlen ("graph_selector"));
+  ident_to_json (graph_selector, handler);
+  ident_destroy (graph_selector);
+
+  yajl_gen_string (handler,
+      (unsigned char *) "instance_selector",
+      (unsigned int) strlen ("instance_selector"));
+  ident_to_json (inst_selector, handler);
+  ident_destroy (inst_selector);
+
+  yajl_gen_string (handler,
+      (unsigned char *) "begin",
+      (unsigned int) strlen ("begin"));
+  yajl_gen_integer (handler, (long int) begin);
+
+  yajl_gen_string (handler,
+      (unsigned char *) "end",
+      (unsigned int) strlen ("end"));
+  yajl_gen_integer (handler, (long int) end);
+
+  yajl_gen_map_close (handler);
+
+  json_buffer = NULL;
+  json_buffer_length = 0;
+  yajl_gen_get_buf (handler, &json_buffer, &json_buffer_length);
+
+  if (json_buffer == NULL)
+  {
+    yajl_gen_free (handler);
+    return (EINVAL);
+  }
+
+  printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", index);
+  printf ("<script type=\"text/javascript\">c4.instances[%i] = %s;</script>\n",
+      index, (const char *) json_buffer);
+
+  yajl_gen_free (handler);
+  return (0);
+} /* }}} int show_instance_json */
+
 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
     void *user_data)
@@ -235,9 +317,7 @@ static int show_instance_cb (graph_config_t *cfg, /* {{{ */
         "&quot;%s / %s&quot;</a>\n",
         script_name (), params, title, descr);
 
-  printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", data->graph_count);
-  printf ("<script type=\"text/javascript\">c4.graphs[%i] = { \"params\": \"%s\", \"begin\": %li, \"end\": %li };</script>\n",
-      data->graph_count, params, (long) begin, (long) end);
+  show_instance_json (cfg, inst, begin, end, data->graph_count);
 
   printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_data_json;%s%s\">"
       "Get graph data as JSON</a></div>\n",
index 0279deb3da1781d49c860bb094f138b8db4b0782..ebf6bc63c75f88611fb54b6ab08fc5c687313771 100644 (file)
@@ -55,6 +55,7 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst,
 int inst_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst,
     rrd_args_t *args);
 
+/* Returns a copy of the selector which must be freed by the caller. */
 graph_ident_t *inst_get_selector (graph_instance_t *inst);
 
 int inst_compare (const graph_instance_t *i0, const graph_instance_t *i1);