share/collection.conf: Fix the DSName of "ping".
[collection4.git] / src / action_search.c
index 7782d77..27b6a0e 100644 (file)
  *   Florian octo Forster <ff at octo.it>
  **/
 
+#include "config.h"
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <assert.h>
 
 #include "action_search.h"
 #include "common.h"
@@ -33,6 +36,7 @@
 #include "graph_instance.h"
 #include "graph_list.h"
 #include "utils_cgi.h"
+#include "utils_search.h"
 
 #include <fcgiapp.h>
 #include <fcgi_stdio.h>
@@ -52,6 +56,17 @@ struct callback_data_s
 };
 typedef struct callback_data_s callback_data_t;
 
+static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
+{
+  printf ("\n<ul class=\"menu left\">\n"
+      "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
+      "  <li><a href=\"%s?action=list_hosts\">All hosts</a></li>\n"
+      "</ul>\n",
+      script_name (), script_name ());
+
+  return (0);
+} /* }}} int left_menu */
+
 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
     void *user_data)
@@ -129,33 +144,10 @@ static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
   return (0);
 } /* }}} int print_graph_inst_html */
 
-static int print_graph_html (graph_config_t *cfg, /* {{{ */
-    __attribute__((unused)) void *user_data)
-{
-  char params[1024];
-  char title[1024];
-
-  if (graph_num_instances (cfg) < 1)
-    return (0);
-
-  memset (title, 0, sizeof (title));
-  graph_get_title (cfg, title, sizeof (title));
-  html_escape_buffer (title, sizeof (title));
-
-  memset (params, 0, sizeof (params));
-  graph_get_params (cfg, params, sizeof (params));
-  html_escape_buffer (params, sizeof (params));
-
-  printf ("      <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
-      "%s</a></li>\n",
-      script_name (), params, title);
-
-  return (0);
-} /* }}} int print_graph_html */
-
 struct page_data_s
 {
-  const char *search_term;
+  char *search_term;
+  search_info_t *search_info;
 };
 typedef struct page_data_s page_data_t;
 
@@ -166,45 +158,20 @@ static int print_search_result (void *user_data) /* {{{ */
     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0,
     /* search_term = */ pg_data->search_term };
+  char *search_term_html;
 
-  if (pg_data->search_term != NULL)
-  {
-    char *search_term_html = html_escape (pg_data->search_term);
-    printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
-        search_term_html);
-    free (search_term_html);
-  }
+  assert (pg_data->search_term != NULL);
+
+  search_term_html = html_escape (pg_data->search_term);
+  printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
+      search_term_html);
+  free (search_term_html);
 
   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
-  if (pg_data->search_term == NULL)
-  {
-    gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
-  }
-  else
-  {
-    char *term_lc = strtolower_copy (pg_data->search_term);
-
-    if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
-      gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
-          print_graph_inst_html, /* user_data = */ &cb_data);
-    else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
-      gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
-          print_graph_inst_html, /* user_data = */ &cb_data);
-    else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
-      gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
-          print_graph_inst_html, /* user_data = */ &cb_data);
-    else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
-      gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
-          print_graph_inst_html, /* user_data = */ &cb_data);
-    else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
-      gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
-          print_graph_inst_html, /* user_data = */ &cb_data);
-    else
-      gl_search (term_lc,
-          print_graph_inst_html, /* user_data = */ &cb_data);
-
-    free (term_lc);
-  }
+
+  gl_search (pg_data->search_info, print_graph_inst_html,
+      /* user_data = */ &cb_data);
+
 
   if (cb_data.cfg != NULL)
     printf ("      </ul></li>\n");
@@ -219,73 +186,101 @@ static int print_search_result (void *user_data) /* {{{ */
   return (0);
 } /* }}} int print_search_result */
 
-static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
+static int print_search_form (void *user_data) /* {{{ */
 {
-  char *host_html;
-
-  /* Make compiler happy */
-  user_data = NULL;
-
-  if (host == NULL)
-    return (EINVAL);
-  
-  host_html = html_escape (host);
-  if (host_html == NULL)
-    return (ENOMEM);
-
-  printf ("  <li class=\"host\"><a href=\"%s?action=search;q=host:%s\">"
-      "%s</a></li>\n",
-      script_name (), host_html, host_html);
+  page_data_t *pg_data = user_data;
+  char search_term_html[1024];
 
-  return (0);
-} /* }}} int print_host_list_callback */
+  if (pg_data->search_term != NULL)
+  {
+    html_escape_copy (search_term_html, pg_data->search_term,
+        sizeof (search_term_html));
+  }
+  else
+  {
+    search_term_html[0] = 0;
+  }
 
-static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
-{
-       return (0);
-  printf ("<div><h3>List of hosts</h3>\n"
-      "<ul id=\"host-list\">\n");
-  gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
-  printf ("</ul></div>\n");
+  printf ("<form action=\"%s\" method=\"get\">\n"
+      "  <input type=\"hidden\" name=\"action\" value=\"search\" />\n"
+      "  <fieldset>\n"
+      "    <legend>Advanced search</legend>\n"
+#if 0
+      "      <span class=\"nbr\">\n"
+      "        <input type=\"checkbox\" name=\"sfh\" value=\"1\" checked=\"checked\" id=\"search_for_host\" />\n"
+      "        <label for=\"search_for_host\">Host</label>\n"
+      "      </span>\n"
+      "      <span class=\"nbr\">\n"
+      "        <input type=\"checkbox\" name=\"sfp\" value=\"1\" checked=\"checked\" id=\"search_for_plugin\" />\n"
+      "        <label for=\"search_for_plugin\">Plugin</label>\n"
+      "      </span>\n"
+      "      <span class=\"nbr\">\n"
+      "        <input type=\"checkbox\" name=\"sfpi\" value=\"1\" checked=\"checked\" id=\"search_for_plugin_instance\" />\n"
+      "        <label for=\"search_for_plugin_instance\">Plugin instance</label>\n"
+      "      </span>\n"
+      "      <span class=\"nbr\">\n"
+      "        <input type=\"checkbox\" name=\"sft\" value=\"1\" checked=\"checked\" id=\"search_for_type\" />\n"
+      "        <label for=\"search_for_type\">Type</label>\n"
+      "      </span>\n"
+      "      <span class=\"nbr\">\n"
+      "        <input type=\"checkbox\" name=\"sfti\" value=\"1\" checked=\"checked\" id=\"search_for_type_instance\" />\n"
+      "        <label for=\"search_for_type_instance\">Type instance</label>\n"
+      "      </span>\n"
+#endif
+      "    <div>Search for <input type=\"text\" name=\"q\" value=\"%s\" size=\"50\" /></div>\n"
+      "    <input type=\"submit\" name=\"button\" value=\"Advanced search\" />"
+      "  </fieldset>\n"
+      "</form>\n",
+      script_name (), search_term_html);
 
   return (0);
-} /* }}} int print_host_list */
+} /* }}} int print_search_form */
 
-static int search_html (const char *term) /* {{{ */
+static int search_html (page_data_t *pg_data) /* {{{ */
 {
-  page_data_t pg_data;
   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
   char title[512];
 
-  if (term != NULL)
+  if (pg_data->search_term != NULL)
     snprintf (title, sizeof (title), "Graphs matching \"%s\"",
-        term);
+        pg_data->search_term);
   else
-    strncpy (title, "List of all graphs", sizeof (title));
+    strncpy (title, "Search", sizeof (title));
   title[sizeof (title) - 1] = 0;
 
-  memset (&pg_data, 0, sizeof (pg_data));
-  pg_data.search_term = term;
-
   pg_callbacks.top_right = html_print_search_box;
-  pg_callbacks.middle_left = print_host_list;
-  pg_callbacks.middle_center = print_search_result;
+  pg_callbacks.middle_left = left_menu;
+  if (pg_data->search_term != NULL)
+    pg_callbacks.middle_center = print_search_result;
+  else
+    pg_callbacks.middle_center = print_search_form;
 
-  html_print_page (title, &pg_callbacks, &pg_data);
+  html_print_page (title, &pg_callbacks, pg_data);
 
   return (0);
 } /* }}} int search_html */
 
 int action_search (void) /* {{{ */
 {
-  char *search;
+  page_data_t pg_data;
   int status;
 
-  gl_update ();
+  pg_data.search_term = strtolower_copy (param ("q"));
+  if ((pg_data.search_term != NULL) && (pg_data.search_term[0] == 0))
+  {
+    free (pg_data.search_term);
+    pg_data.search_term = NULL;
+    pg_data.search_info = NULL;
+  }
+  else
+  {
+    pg_data.search_info = search_parse (pg_data.search_term);
+  }
+
+  status = search_html (&pg_data);
 
-  search = strtolower_copy (param ("q"));
-  status = search_html (search);
-  free (search);
+  free (pg_data.search_term);
+  search_destroy (pg_data.search_info);
 
   return (status);
 } /* }}} int action_search */