share/collection.js: Keep visibility when redrawing graphs.
[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 #include "utils_search.h"
40
41 #include <fcgiapp.h>
42 #include <fcgi_stdio.h>
43
44 #define RESULT_LIMIT 50
45
46 struct callback_data_s
47 {
48   graph_config_t *cfg;
49   int graph_index;
50   int graph_limit;
51   _Bool graph_more;
52   int inst_index;
53   int inst_limit;
54   _Bool inst_more;
55   const char *search_term;
56 };
57 typedef struct callback_data_s callback_data_t;
58
59 static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
60 {
61   printf ("\n<ul class=\"menu left\">\n"
62       "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
63       "  <li><a href=\"%s?action=list_hosts\">All hosts</a></li>\n"
64       "</ul>\n",
65       script_name (), script_name ());
66
67   return (0);
68 } /* }}} int left_menu */
69
70 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
71     graph_instance_t *inst,
72     void *user_data)
73 {
74   callback_data_t *data = user_data;
75   char params[1024];
76   char desc[1024];
77
78   if (data->cfg != cfg)
79   {
80     data->graph_index++;
81     if (data->graph_index >= data->graph_limit)
82     {
83       data->graph_more = 1;
84       return (1);
85     }
86
87     if (data->cfg != NULL)
88       printf ("  </ul></li>\n");
89
90     memset (desc, 0, sizeof (desc));
91     graph_get_title (cfg, desc, sizeof (desc));
92     html_escape_buffer (desc, sizeof (desc));
93
94     printf ("  <li class=\"graph\">%s\n"
95         "  <ul class=\"instance_list\">\n", desc);
96
97     data->cfg = cfg;
98     data->inst_index = -1;
99     data->inst_more = 0;
100   }
101
102   data->inst_index++;
103   if (data->inst_index >= data->inst_limit)
104   {
105     if (!data->inst_more)
106     {
107       char *search_term_html = html_escape (data->search_term);
108       char param_search_term[1024];
109
110       memset (params, 0, sizeof (params));
111       graph_get_params (cfg, params, sizeof (params));
112       html_escape_buffer (params, sizeof (params));
113
114       param_search_term[0] = 0;
115       if (search_term_html != NULL)
116       {
117         snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
118             search_term_html);
119         param_search_term[sizeof (param_search_term) - 1] = 0;
120       }
121
122       free (search_term_html);
123
124       printf ("    <li class=\"instance more\"><a href=\"%s"
125           "?action=show_graph;%s%s\">More &#x2026;</a></li>\n",
126           script_name (), params, param_search_term);
127
128       data->inst_more = 1;
129     }
130     return (0);
131   }
132
133   memset (params, 0, sizeof (params));
134   inst_get_params (cfg, inst, params, sizeof (params));
135   html_escape_buffer (params, sizeof (params));
136
137   memset (desc, 0, sizeof (desc));
138   inst_describe (cfg, inst, desc, sizeof (desc));
139   html_escape_buffer (desc, sizeof (desc));
140
141   printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
142       script_name (), params, desc);
143
144   return (0);
145 } /* }}} int print_graph_inst_html */
146
147 struct page_data_s
148 {
149   char *search_term;
150   search_info_t *search_info;
151 };
152 typedef struct page_data_s page_data_t;
153
154 static int print_search_result (void *user_data) /* {{{ */
155 {
156   page_data_t *pg_data = user_data;
157   callback_data_t cb_data = { /* cfg = */ NULL,
158     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
159     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0,
160     /* search_term = */ pg_data->search_term };
161   char *search_term_html;
162
163   assert (pg_data->search_term != NULL);
164
165   search_term_html = html_escape (pg_data->search_term);
166   printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
167       search_term_html);
168   free (search_term_html);
169
170   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
171
172   gl_search (pg_data->search_info, print_graph_inst_html,
173       /* user_data = */ &cb_data);
174
175
176   if (cb_data.cfg != NULL)
177     printf ("      </ul></li>\n");
178
179   if (cb_data.graph_more)
180   {
181     printf ("    <li class=\"graph more\">More ...</li>\n");
182   }
183
184   printf ("    </ul>\n");
185
186   return (0);
187 } /* }}} int print_search_result */
188
189 static int print_search_form (void *user_data) /* {{{ */
190 {
191   page_data_t *pg_data = user_data;
192   char search_term_html[1024];
193
194   if (pg_data->search_term != NULL)
195   {
196     html_escape_copy (search_term_html, pg_data->search_term,
197         sizeof (search_term_html));
198   }
199   else
200   {
201     search_term_html[0] = 0;
202   }
203
204   printf ("<form action=\"%s\" method=\"get\">\n"
205       "  <input type=\"hidden\" name=\"action\" value=\"search\" />\n"
206       "  <fieldset>\n"
207       "    <legend>Advanced search</legend>\n"
208 #if 0
209       "      <span class=\"nbr\">\n"
210       "        <input type=\"checkbox\" name=\"sfh\" value=\"1\" checked=\"checked\" id=\"search_for_host\" />\n"
211       "        <label for=\"search_for_host\">Host</label>\n"
212       "      </span>\n"
213       "      <span class=\"nbr\">\n"
214       "        <input type=\"checkbox\" name=\"sfp\" value=\"1\" checked=\"checked\" id=\"search_for_plugin\" />\n"
215       "        <label for=\"search_for_plugin\">Plugin</label>\n"
216       "      </span>\n"
217       "      <span class=\"nbr\">\n"
218       "        <input type=\"checkbox\" name=\"sfpi\" value=\"1\" checked=\"checked\" id=\"search_for_plugin_instance\" />\n"
219       "        <label for=\"search_for_plugin_instance\">Plugin instance</label>\n"
220       "      </span>\n"
221       "      <span class=\"nbr\">\n"
222       "        <input type=\"checkbox\" name=\"sft\" value=\"1\" checked=\"checked\" id=\"search_for_type\" />\n"
223       "        <label for=\"search_for_type\">Type</label>\n"
224       "      </span>\n"
225       "      <span class=\"nbr\">\n"
226       "        <input type=\"checkbox\" name=\"sfti\" value=\"1\" checked=\"checked\" id=\"search_for_type_instance\" />\n"
227       "        <label for=\"search_for_type_instance\">Type instance</label>\n"
228       "      </span>\n"
229 #endif
230       "    <div>Search for <input type=\"text\" name=\"q\" value=\"%s\" size=\"50\" /></div>\n"
231       "    <input type=\"submit\" name=\"button\" value=\"Advanced search\" />"
232       "  </fieldset>\n"
233       "</form>\n",
234       script_name (), search_term_html);
235
236   return (0);
237 } /* }}} int print_search_form */
238
239 static int search_html (page_data_t *pg_data) /* {{{ */
240 {
241   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
242   char title[512];
243
244   if (pg_data->search_term != NULL)
245     snprintf (title, sizeof (title), "Graphs matching \"%s\"",
246         pg_data->search_term);
247   else
248     strncpy (title, "Search", sizeof (title));
249   title[sizeof (title) - 1] = 0;
250
251   pg_callbacks.top_right = html_print_search_box;
252   pg_callbacks.middle_left = left_menu;
253   if (pg_data->search_term != NULL)
254     pg_callbacks.middle_center = print_search_result;
255   else
256     pg_callbacks.middle_center = print_search_form;
257
258   html_print_page (title, &pg_callbacks, pg_data);
259
260   return (0);
261 } /* }}} int search_html */
262
263 int action_search (void) /* {{{ */
264 {
265   page_data_t pg_data;
266   int status;
267
268   pg_data.search_term = strtolower_copy (param ("q"));
269   if ((pg_data.search_term != NULL) && (pg_data.search_term[0] == 0))
270   {
271     free (pg_data.search_term);
272     pg_data.search_term = NULL;
273     pg_data.search_info = NULL;
274   }
275   else
276   {
277     pg_data.search_info = search_parse (pg_data.search_term);
278   }
279
280   status = search_html (&pg_data);
281
282   free (pg_data.search_term);
283   search_destroy (pg_data.search_info);
284
285   return (status);
286 } /* }}} int action_search */
287
288 /* vim: set sw=2 sts=2 et fdm=marker : */