2 * collection4 - action_search.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
29 #include "action_search.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
38 #include <fcgi_stdio.h>
40 #define RESULT_LIMIT 50
42 struct callback_data_s
51 const char *search_term;
53 typedef struct callback_data_s callback_data_t;
55 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
56 graph_instance_t *inst,
59 callback_data_t *data = user_data;
66 if (data->graph_index >= data->graph_limit)
72 if (data->cfg != NULL)
73 printf (" </ul></li>\n");
75 memset (desc, 0, sizeof (desc));
76 graph_get_title (cfg, desc, sizeof (desc));
77 html_escape_buffer (desc, sizeof (desc));
79 printf (" <li class=\"graph\">%s\n"
80 " <ul class=\"instance_list\">\n", desc);
83 data->inst_index = -1;
88 if (data->inst_index >= data->inst_limit)
92 char *search_term_html = html_escape (data->search_term);
93 char param_search_term[1024];
95 memset (params, 0, sizeof (params));
96 graph_get_params (cfg, params, sizeof (params));
97 html_escape_buffer (params, sizeof (params));
99 param_search_term[0] = 0;
100 if (search_term_html != NULL)
102 snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
104 param_search_term[sizeof (param_search_term) - 1] = 0;
107 free (search_term_html);
109 printf (" <li class=\"instance more\"><a href=\"%s"
110 "?action=show_graph;%s%s\">More …</a></li>\n",
111 script_name (), params, param_search_term);
118 memset (params, 0, sizeof (params));
119 inst_get_params (cfg, inst, params, sizeof (params));
120 html_escape_buffer (params, sizeof (params));
122 memset (desc, 0, sizeof (desc));
123 inst_describe (cfg, inst, desc, sizeof (desc));
124 html_escape_buffer (desc, sizeof (desc));
126 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
127 script_name (), params, desc);
130 } /* }}} int print_graph_inst_html */
132 static int print_graph_html (graph_config_t *cfg, /* {{{ */
133 __attribute__((unused)) void *user_data)
138 if (graph_num_instances (cfg) < 1)
141 memset (title, 0, sizeof (title));
142 graph_get_title (cfg, title, sizeof (title));
143 html_escape_buffer (title, sizeof (title));
145 memset (params, 0, sizeof (params));
146 graph_get_params (cfg, params, sizeof (params));
147 html_escape_buffer (params, sizeof (params));
149 printf (" <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
151 script_name (), params, title);
154 } /* }}} int print_graph_html */
158 const char *search_term;
160 typedef struct page_data_s page_data_t;
162 static int print_search_result (void *user_data) /* {{{ */
164 page_data_t *pg_data = user_data;
165 callback_data_t cb_data = { /* cfg = */ NULL,
166 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
167 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0,
168 /* search_term = */ pg_data->search_term };
170 if (pg_data->search_term != NULL)
172 char *search_term_html = html_escape (pg_data->search_term);
173 printf (" <h2>Search results for "%s"</h2>\n",
175 free (search_term_html);
178 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
179 if (pg_data->search_term == NULL)
181 gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
185 char *term_lc = strtolower_copy (pg_data->search_term);
187 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
188 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
189 print_graph_inst_html, /* user_data = */ &cb_data);
190 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
191 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
192 print_graph_inst_html, /* user_data = */ &cb_data);
193 else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
194 gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
195 print_graph_inst_html, /* user_data = */ &cb_data);
196 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
197 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
198 print_graph_inst_html, /* user_data = */ &cb_data);
199 else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
200 gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
201 print_graph_inst_html, /* user_data = */ &cb_data);
204 print_graph_inst_html, /* user_data = */ &cb_data);
209 if (cb_data.cfg != NULL)
210 printf (" </ul></li>\n");
212 if (cb_data.graph_more)
214 printf (" <li class=\"graph more\">More ...</li>\n");
220 } /* }}} int print_search_result */
222 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
226 /* Make compiler happy */
232 host_html = html_escape (host);
233 if (host_html == NULL)
236 printf (" <li class=\"host\"><a href=\"%s?action=search;q=host:%s\">"
238 script_name (), host_html, host_html);
241 } /* }}} int print_host_list_callback */
243 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
246 printf ("<div><h3>List of hosts</h3>\n"
247 "<ul id=\"host-list\">\n");
248 gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
249 printf ("</ul></div>\n");
252 } /* }}} int print_host_list */
254 static int search_html (const char *term) /* {{{ */
257 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
261 snprintf (title, sizeof (title), "Graphs matching \"%s\"",
264 strncpy (title, "List of all graphs", sizeof (title));
265 title[sizeof (title) - 1] = 0;
267 memset (&pg_data, 0, sizeof (pg_data));
268 pg_data.search_term = term;
270 pg_callbacks.top_right = html_print_search_box;
271 pg_callbacks.middle_left = print_host_list;
272 pg_callbacks.middle_center = print_search_result;
274 html_print_page (title, &pg_callbacks, &pg_data);
277 } /* }}} int search_html */
279 int action_search (void) /* {{{ */
286 search = strtolower_copy (param ("q"));
287 status = search_html (search);
291 } /* }}} int action_search */
293 /* vim: set sw=2 sts=2 et fdm=marker : */