src/graph.[ch]: Rename "graph_inst_search" to "graph_search_inst_string".
[collection4.git] / src / action_search.c
1 /**
2  * collection4 - action_search.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <assert.h>
31
32 #include "action_search.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
39
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
42
43 #define RESULT_LIMIT 50
44
45 struct callback_data_s
46 {
47   graph_config_t *cfg;
48   int graph_index;
49   int graph_limit;
50   _Bool graph_more;
51   int inst_index;
52   int inst_limit;
53   _Bool inst_more;
54   const char *search_term;
55 };
56 typedef struct callback_data_s callback_data_t;
57
58 static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
59 {
60   printf ("\n<ul class=\"menu left\">\n"
61       "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
62       "  <li><a href=\"%s?action=list_hosts\">All hosts</a></li>\n"
63       "</ul>\n",
64       script_name (), script_name ());
65
66   return (0);
67 } /* }}} int left_menu */
68
69 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
70     graph_instance_t *inst,
71     void *user_data)
72 {
73   callback_data_t *data = user_data;
74   char params[1024];
75   char desc[1024];
76
77   if (data->cfg != cfg)
78   {
79     data->graph_index++;
80     if (data->graph_index >= data->graph_limit)
81     {
82       data->graph_more = 1;
83       return (1);
84     }
85
86     if (data->cfg != NULL)
87       printf ("  </ul></li>\n");
88
89     memset (desc, 0, sizeof (desc));
90     graph_get_title (cfg, desc, sizeof (desc));
91     html_escape_buffer (desc, sizeof (desc));
92
93     printf ("  <li class=\"graph\">%s\n"
94         "  <ul class=\"instance_list\">\n", desc);
95
96     data->cfg = cfg;
97     data->inst_index = -1;
98     data->inst_more = 0;
99   }
100
101   data->inst_index++;
102   if (data->inst_index >= data->inst_limit)
103   {
104     if (!data->inst_more)
105     {
106       char *search_term_html = html_escape (data->search_term);
107       char param_search_term[1024];
108
109       memset (params, 0, sizeof (params));
110       graph_get_params (cfg, params, sizeof (params));
111       html_escape_buffer (params, sizeof (params));
112
113       param_search_term[0] = 0;
114       if (search_term_html != NULL)
115       {
116         snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
117             search_term_html);
118         param_search_term[sizeof (param_search_term) - 1] = 0;
119       }
120
121       free (search_term_html);
122
123       printf ("    <li class=\"instance more\"><a href=\"%s"
124           "?action=show_graph;%s%s\">More &#x2026;</a></li>\n",
125           script_name (), params, param_search_term);
126
127       data->inst_more = 1;
128     }
129     return (0);
130   }
131
132   memset (params, 0, sizeof (params));
133   inst_get_params (cfg, inst, params, sizeof (params));
134   html_escape_buffer (params, sizeof (params));
135
136   memset (desc, 0, sizeof (desc));
137   inst_describe (cfg, inst, desc, sizeof (desc));
138   html_escape_buffer (desc, sizeof (desc));
139
140   printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
141       script_name (), params, desc);
142
143   return (0);
144 } /* }}} int print_graph_inst_html */
145
146 struct page_data_s
147 {
148   const char *search_term;
149 };
150 typedef struct page_data_s page_data_t;
151
152 static int print_search_result (void *user_data) /* {{{ */
153 {
154   page_data_t *pg_data = user_data;
155   callback_data_t cb_data = { /* cfg = */ NULL,
156     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
157     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0,
158     /* search_term = */ pg_data->search_term };
159   char *term_lc;
160   char *search_term_html;
161
162   assert (pg_data->search_term != NULL);
163
164   search_term_html = html_escape (pg_data->search_term);
165   printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
166       search_term_html);
167   free (search_term_html);
168
169   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
170
171   term_lc = strtolower_copy (pg_data->search_term);
172
173   if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
174     gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
175         print_graph_inst_html, /* user_data = */ &cb_data);
176   else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
177     gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
178         print_graph_inst_html, /* user_data = */ &cb_data);
179   else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
180     gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
181         print_graph_inst_html, /* user_data = */ &cb_data);
182   else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
183     gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
184         print_graph_inst_html, /* user_data = */ &cb_data);
185   else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
186     gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
187         print_graph_inst_html, /* user_data = */ &cb_data);
188   else
189     gl_search_string (term_lc,
190         print_graph_inst_html, /* user_data = */ &cb_data);
191
192   free (term_lc);
193
194   if (cb_data.cfg != NULL)
195     printf ("      </ul></li>\n");
196
197   if (cb_data.graph_more)
198   {
199     printf ("    <li class=\"graph more\">More ...</li>\n");
200   }
201
202   printf ("    </ul>\n");
203
204   return (0);
205 } /* }}} int print_search_result */
206
207 static int print_search_form (void *user_data) /* {{{ */
208 {
209   page_data_t *pg_data = user_data;
210   char search_term_html[1024];
211
212   if (pg_data->search_term != NULL)
213   {
214     html_escape_copy (search_term_html, pg_data->search_term,
215         sizeof (search_term_html));
216   }
217   else
218   {
219     search_term_html[0] = 0;
220   }
221
222   printf ("<form action=\"%s\" method=\"get\">\n"
223       "  <input type=\"hidden\" name=\"action\" value=\"search\" />\n"
224       "  <fieldset>\n"
225       "    <legend>Advanced search</legend>\n"
226 #if 0
227       "      <span class=\"nbr\">\n"
228       "        <input type=\"checkbox\" name=\"sfh\" value=\"1\" checked=\"checked\" id=\"search_for_host\" />\n"
229       "        <label for=\"search_for_host\">Host</label>\n"
230       "      </span>\n"
231       "      <span class=\"nbr\">\n"
232       "        <input type=\"checkbox\" name=\"sfp\" value=\"1\" checked=\"checked\" id=\"search_for_plugin\" />\n"
233       "        <label for=\"search_for_plugin\">Plugin</label>\n"
234       "      </span>\n"
235       "      <span class=\"nbr\">\n"
236       "        <input type=\"checkbox\" name=\"sfpi\" value=\"1\" checked=\"checked\" id=\"search_for_plugin_instance\" />\n"
237       "        <label for=\"search_for_plugin_instance\">Plugin instance</label>\n"
238       "      </span>\n"
239       "      <span class=\"nbr\">\n"
240       "        <input type=\"checkbox\" name=\"sft\" value=\"1\" checked=\"checked\" id=\"search_for_type\" />\n"
241       "        <label for=\"search_for_type\">Type</label>\n"
242       "      </span>\n"
243       "      <span class=\"nbr\">\n"
244       "        <input type=\"checkbox\" name=\"sfti\" value=\"1\" checked=\"checked\" id=\"search_for_type_instance\" />\n"
245       "        <label for=\"search_for_type_instance\">Type instance</label>\n"
246       "      </span>\n"
247 #endif
248       "    <div>Search for <input type=\"text\" name=\"q\" value=\"%s\" size=\"50\" /></div>\n"
249       "    <input type=\"submit\" name=\"button\" value=\"Advanced search\" />"
250       "  </fieldset>\n"
251       "</form>\n",
252       script_name (), search_term_html);
253
254   return (0);
255 } /* }}} int print_search_form */
256
257 static int search_html (const char *term) /* {{{ */
258 {
259   page_data_t pg_data;
260   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
261   char title[512];
262
263   if (term != NULL)
264     snprintf (title, sizeof (title), "Graphs matching \"%s\"",
265         term);
266   else
267     strncpy (title, "Search", sizeof (title));
268   title[sizeof (title) - 1] = 0;
269
270   memset (&pg_data, 0, sizeof (pg_data));
271   pg_data.search_term = term;
272
273   pg_callbacks.top_right = html_print_search_box;
274   pg_callbacks.middle_left = left_menu;
275   if (term != NULL)
276     pg_callbacks.middle_center = print_search_result;
277   else
278     pg_callbacks.middle_center = print_search_form;
279
280   html_print_page (title, &pg_callbacks, &pg_data);
281
282   return (0);
283 } /* }}} int search_html */
284
285 int action_search (void) /* {{{ */
286 {
287   char *search;
288   int status;
289
290   gl_update ();
291
292   search = strtolower_copy (param ("q"));
293   if ((search != NULL) && (search[0] == 0))
294   {
295     free (search);
296     search = NULL;
297   }
298   status = search_html (search);
299   free (search);
300
301   return (status);
302 } /* }}} int action_search */
303
304 /* vim: set sw=2 sts=2 et fdm=marker : */