Use the Highchart library to draw graphs.
[collection4.git] / src / action_show_instance.c
index 08d1bc0..c7de237 100644 (file)
 
 #define MAX_SHOW_GRAPHS 10
 
+#define SGD_FORMAT_JSON 0
+#define SGD_FORMAT_RRD  1
+
 struct show_graph_data_s
 {
   graph_config_t *cfg;
   graph_instance_t *inst;
   int graph_count;
+  int format;
 };
 typedef struct show_graph_data_s show_graph_data_t;
 
@@ -70,10 +74,10 @@ static void show_breadcrump_field (const char *str, /* {{{ */
     char *str_html = html_escape (str);
 
     if (field_name != NULL)
-      printf ("<a href=\"%s?action=list_graphs;q=%s:%s\">%s</a>",
+      printf ("<a href=\"%s?action=search;q=%s:%s\">%s</a>",
           script_name (), field_name, str_html, str_html);
     else
-      printf ("<a href=\"%s?action=list_graphs;q=%s\">%s</a>",
+      printf ("<a href=\"%s?action=search;q=%s\">%s</a>",
           script_name (), str_html, str_html);
 
     free (str_html);
@@ -132,8 +136,12 @@ static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ *
       "    <option value=\"-604800\">Week</option>\n"
       "    <option value=\"-2678400\">Month</option>\n"
       "    <option value=\"-31622400\">Year</option>\n"
-      "  </select>\n"
-      "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
+      "  </select><br />\n");
+  printf ("  <input id=\"format-json\" type=\"radio\" name=\"format\" value=\"JSON\" checked=\"checked\" />"
+      "<label for=\"format-json\">&nbsp;JavaScript</label><br />\n"
+      "  <input id=\"format-rrd\" type=\"radio\" name=\"format\" value=\"RRD\" />"
+      "<label for=\"format-rrd\">&nbsp;RRDtool</label>\n<br />");
+  printf ("  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
 
   printf ("</form>\n");
 
@@ -142,111 +150,203 @@ static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ *
   return (0);
 } /* }}} int show_time_selector */
 
-static int show_instance_list_cb (graph_instance_t *inst, /* {{{ */
-    void *user_data)
+static int left_menu (void *user_data) /* {{{ */
 {
   show_graph_data_t *data = user_data;
-  char descr[128];
   char params[1024];
+  graph_instance_t *inst;
+  graph_ident_t *ident;
+  const char *host;
 
-  memset (descr, 0, sizeof (descr));
-  inst_describe (data->cfg, inst, descr, sizeof (descr));
-  html_escape_buffer (descr, sizeof (descr));
+  memset (params, 0, sizeof (params));
+  graph_get_params (data->cfg, params, sizeof (params));
+  html_escape_buffer (params, sizeof (params));
 
-  if (inst == data->inst)
+  inst = inst_get_selected (data->cfg);
+  ident = inst_get_selector (inst);
+  host = ident_get_host (ident);
+  if (IS_ANY (host))
+    host = NULL;
+
+  printf ("\n<ul class=\"menu left\">\n"
+      "  <li><a href=\"%s?action=show_graph;%s\">All instances</a></li>\n"
+      "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n",
+      script_name (), params,
+      script_name ());
+  if (host != NULL)
   {
-    printf ("    <li class=\"instance\"><strong>%s</strong></li>\n", descr);
-    return (0);
+    char host_html[1024];
+    char host_uri[1024];
+
+    html_escape_copy (host_html, host, sizeof (host_html));
+    uri_escape_copy (host_uri, host, sizeof (host_uri));
+
+    printf ("  <li><a href=\"%s?action=search;q=host:%s\">Host &quot;%s&quot;</a></li>\n",
+        script_name (), host_uri, host_html);
   }
+  printf ("</ul>\n");
 
-  memset (params, 0, sizeof (params));
-  inst_get_params (data->cfg, inst, params, sizeof (params));
-  html_escape_buffer (params, sizeof (params));
+  host = NULL;
+  ident_destroy (ident);
+
+  return (0);
+} /* }}} int left_menu */
+
+static int show_instance_json (graph_config_t *cfg, /* {{{ */
+    graph_instance_t *inst,
+    long begin, long 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;
 
-  printf ("    <li class=\"instance\">"
-      "<a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
-      script_name (), params, descr);
+  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, begin);
+
+  yajl_gen_string (handler,
+      (unsigned char *) "end",
+      (unsigned int) strlen ("end"));
+  yajl_gen_integer (handler, 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_list_cb */
+} /* }}} int show_instance_json */
 
-static int show_instance_list (void *user_data) /* {{{ */
+static int show_instance_rrdtool (graph_config_t *cfg, /* {{{ */
+    graph_instance_t *inst,
+    long begin, long end, int index)
 {
-  show_graph_data_t *data = user_data;
   char title[128];
+  char descr[128];
   char params[1024];
+  char time_params[128];
 
   memset (title, 0, sizeof (title));
-  graph_get_title (data->cfg, title, sizeof (title));
+  graph_get_title (cfg, title, sizeof (title));
   html_escape_buffer (title, sizeof (title));
 
+  memset (descr, 0, sizeof (descr));
+  inst_describe (cfg, inst, descr, sizeof (descr));
+  html_escape_buffer (descr, sizeof (descr));
+
   memset (params, 0, sizeof (params));
-  graph_get_params (data->cfg, params, sizeof (params));
+  inst_get_params (cfg, inst, params, sizeof (params));
   html_escape_buffer (params, sizeof (params));
 
-  printf ("<ul class=\"graph_list\">\n"
-      "  <li class=\"graph\"><a href=\"%s?action=show_instance;%s\">%s</a>\n"
-      "  <ul class=\"instance_list\">\n",
-      script_name (), params, title);
+  snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
+      begin, end);
+  time_params[sizeof (time_params) - 1] = 0;
 
-  graph_inst_foreach (data->cfg, show_instance_list_cb, user_data);
+  if (index < MAX_SHOW_GRAPHS)
+    printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
+        "title=\"%s / %s\" /></div>\n",
+        script_name (), params, time_params, title, descr);
+  else
+    printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
+        "&quot;%s / %s&quot;</a>\n",
+        script_name (), params, title, descr);
 
-  printf ("  </ul>\n"
-      "</ul>\n");
+#if 0
+  printf ("<div><a href=\"%s?action=instance_data_json;%s%s\">"
+      "Get graph data as JSON</a></div>\n",
+      script_name (), params, time_params);
+#endif
 
   return (0);
-} /* }}} int show_instance_list */
+} /* }}} int show_instance_rrdtool */
 
 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
     void *user_data)
 {
   show_graph_data_t *data = user_data;
-  char title[128];
   char descr[128];
-  char params[1024];
 
   long begin;
   long end;
-  char time_params[128];
   int status;
 
-  memset (title, 0, sizeof (title));
-  graph_get_title (cfg, title, sizeof (title));
-  html_escape_buffer (title, sizeof (title));
-
   memset (descr, 0, sizeof (descr));
   inst_describe (cfg, inst, descr, sizeof (descr));
   html_escape_buffer (descr, sizeof (descr));
 
-  memset (params, 0, sizeof (params));
-  inst_get_params (cfg, inst, params, sizeof (params));
-  html_escape_buffer (params, sizeof (params));
-
-  time_params[0] = 0;
   begin = 0;
   end = 0;
 
   status = get_time_args (&begin, &end, /* now = */ NULL);
-  if (status == 0)
-  {
-    snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
-        begin, end);
-    time_params[sizeof (time_params) - 1] = 0;
-  }
+  if (status != 0)
+    return (status);
 
-  printf ("<h3>Instance &quot;%s&quot;</h3>\n", descr);
+  printf ("<h2>Instance &quot;%s&quot;</h2>\n", descr);
 
   show_breadcrump (cfg, inst);
 
-  if (data->graph_count < MAX_SHOW_GRAPHS)
-    printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
-        "title=\"%s / %s\" /></div>\n",
-        script_name (), params, time_params, title, descr);
+  if (data->format == SGD_FORMAT_RRD)
+    show_instance_rrdtool (cfg, inst, begin, end, data->graph_count);
   else
-    printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
-        "&quot;%s / %s&quot;</a>\n",
-        script_name (), params, title, descr);
+    show_instance_json (cfg, inst, begin, end, data->graph_count);
 
   data->graph_count++;
 
@@ -256,15 +356,25 @@ static int show_instance_cb (graph_config_t *cfg, /* {{{ */
 static int show_instance (void *user_data) /* {{{ */
 {
   show_graph_data_t *data = user_data;
+  /* char params[1024]; */
   int status;
 
-  fprintf (stderr, "show_instance: Calling inst_get_all_selected()\n");
   status = inst_get_all_selected (data->cfg,
       /* callback = */ show_instance_cb, /* user data = */ data);
   if (status != 0)
     fprintf (stderr, "show_instance: inst_get_all_selected failed "
         "with status %i\n", status);
 
+#if 0
+  memset (params, 0, sizeof (params));
+  graph_get_params (data->cfg, params, sizeof (params));
+  html_escape_buffer (params, sizeof (params));
+
+  printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_def_json;%s\">"
+      "Get graph definition as JSON</a></div>\n",
+      script_name (), params);
+#endif
+
   return (0);
 } /* }}} int show_instance */
 
@@ -275,6 +385,7 @@ int action_show_instance (void) /* {{{ */
 
   char tmp[128];
   char title[128];
+  const char *format;
 
   memset (&pg_data, 0, sizeof (pg_data));
   pg_data.cfg = gl_graph_get_selected ();
@@ -286,9 +397,15 @@ int action_show_instance (void) /* {{{ */
   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
   title[sizeof (title) - 1] = 0;
 
+  format = param ("format");
+  if ((format != NULL) && (strcasecmp ("RRD", format) == 0))
+    pg_data.format = SGD_FORMAT_RRD;
+  else
+    pg_data.format = SGD_FORMAT_JSON;
+
   pg_callbacks.top_right = html_print_search_box;
   pg_callbacks.middle_center = show_instance;
-  pg_callbacks.middle_left = show_instance_list;
+  pg_callbacks.middle_left = left_menu;
   pg_callbacks.middle_right = show_time_selector;
 
   html_print_page (title, &pg_callbacks, &pg_data);