"list graph" action: Don't show any instances if no search term is given.
[collection4.git] / src / action_list_graphs.c
1 /**
2  * collection4 - action_list_graphs.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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "action_list_graphs.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
36
37 #include <fcgiapp.h>
38 #include <fcgi_stdio.h>
39
40 #define RESULT_LIMIT 50
41
42 struct callback_data_s
43 {
44   graph_config_t *cfg;
45   int graph_index;
46   int graph_limit;
47   _Bool graph_more;
48   int inst_index;
49   int inst_limit;
50   _Bool inst_more;
51 };
52 typedef struct callback_data_s callback_data_t;
53
54 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
55     graph_instance_t *inst,
56     void *user_data)
57 {
58   callback_data_t *data = user_data;
59   char params[1024];
60   char desc[1024];
61
62   if (data->cfg != cfg)
63   {
64     data->graph_index++;
65     if (data->graph_index >= data->graph_limit)
66     {
67       data->graph_more = 1;
68       return (1);
69     }
70
71     if (data->cfg != NULL)
72       printf ("  </ul></li>\n");
73
74     memset (desc, 0, sizeof (desc));
75     graph_get_title (cfg, desc, sizeof (desc));
76     html_escape_buffer (desc, sizeof (desc));
77
78     printf ("  <li class=\"graph\">%s\n"
79         "  <ul class=\"instance_list\">\n", desc);
80
81     data->cfg = cfg;
82     data->inst_index = -1;
83     data->inst_more = 0;
84   }
85
86   data->inst_index++;
87   if (data->inst_index >= data->inst_limit)
88   {
89     if (!data->inst_more)
90     {
91       memset (params, 0, sizeof (params));
92       graph_get_params (cfg, params, sizeof (params));
93       html_escape_buffer (params, sizeof (params));
94
95       printf ("    <li class=\"instance more\"><a href=\"%s"
96           "?action=show_graph;%s\">More &#x2026;</a></li>\n",
97           script_name (), params);
98
99       data->inst_more = 1;
100     }
101     return (0);
102   }
103
104   memset (params, 0, sizeof (params));
105   inst_get_params (cfg, inst, params, sizeof (params));
106   html_escape_buffer (params, sizeof (params));
107
108   memset (desc, 0, sizeof (desc));
109   inst_describe (cfg, inst, desc, sizeof (desc));
110   html_escape_buffer (desc, sizeof (desc));
111
112   printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
113       script_name (), params, desc);
114
115   return (0);
116 } /* }}} int print_graph_inst_html */
117
118 static int print_graph_html (graph_config_t *cfg, /* {{{ */
119     __attribute__((unused)) void *user_data)
120 {
121   char params[1024];
122   char title[1024];
123
124   memset (title, 0, sizeof (title));
125   graph_get_title (cfg, title, sizeof (title));
126   html_escape_buffer (title, sizeof (title));
127
128   memset (params, 0, sizeof (params));
129   graph_get_params (cfg, params, sizeof (params));
130   html_escape_buffer (params, sizeof (params));
131
132   printf ("      <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
133       "%s</a></li>\n",
134       script_name (), params, title);
135
136   return (0);
137 } /* }}} int print_graph_html */
138
139 struct page_data_s
140 {
141   const char *search_term;
142 };
143 typedef struct page_data_s page_data_t;
144
145 static int print_search_result (void *user_data) /* {{{ */
146 {
147   page_data_t *pg_data = user_data;
148   callback_data_t cb_data = { /* cfg = */ NULL,
149     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
150     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0 };
151
152   if (pg_data->search_term != NULL)
153   {
154     char *search_term_html = html_escape (pg_data->search_term);
155     printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
156         search_term_html);
157     free (search_term_html);
158   }
159
160   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
161   if (pg_data->search_term == NULL)
162   {
163     gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
164   }
165   else
166   {
167     char *term_lc = strtolower_copy (pg_data->search_term);
168
169     if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
170       gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
171           print_graph_inst_html, /* user_data = */ &cb_data);
172     else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
173       gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
174           print_graph_inst_html, /* user_data = */ &cb_data);
175     else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
176       gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
177           print_graph_inst_html, /* user_data = */ &cb_data);
178     else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
179       gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
180           print_graph_inst_html, /* user_data = */ &cb_data);
181     else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
182       gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
183           print_graph_inst_html, /* user_data = */ &cb_data);
184     else
185       gl_search (term_lc,
186           print_graph_inst_html, /* user_data = */ &cb_data);
187
188     free (term_lc);
189   }
190
191   if (cb_data.cfg != NULL)
192     printf ("      </ul></li>\n");
193
194   if (cb_data.graph_more)
195   {
196     printf ("    <li class=\"graph more\">More ...</li>\n");
197   }
198
199   printf ("    </ul>\n");
200
201   return (0);
202 } /* }}} int print_search_result */
203
204 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
205 {
206   char *host_html;
207
208   /* Make compiler happy */
209   user_data = NULL;
210
211   if (host == NULL)
212     return (EINVAL);
213   
214   host_html = html_escape (host);
215   if (host_html == NULL)
216     return (ENOMEM);
217
218   printf ("  <li class=\"host\"><a href=\"%s?action=list_graphs;q=host:%s\">"
219       "%s</a></li>\n",
220       script_name (), host_html, host_html);
221
222   return (0);
223 } /* }}} int print_host_list_callback */
224
225 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
226 {
227   printf ("<div><h3>List of hosts</h3>\n"
228       "<ul id=\"host-list\">\n");
229   gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
230   printf ("</ul></div>\n");
231
232   return (0);
233 } /* }}} int print_host_list */
234
235 static int list_graphs_html (const char *term) /* {{{ */
236 {
237   page_data_t pg_data;
238   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
239   char title[512];
240
241   if (term != NULL)
242     snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
243   else
244     strncpy (title, "c4: List of all graphs", sizeof (title));
245   title[sizeof (title) - 1] = 0;
246
247   memset (&pg_data, 0, sizeof (pg_data));
248   pg_data.search_term = term;
249
250   pg_callbacks.top_right = html_print_search_box;
251   pg_callbacks.middle_left = print_host_list;
252   pg_callbacks.middle_center = print_search_result;
253
254   html_print_page (title, &pg_callbacks, &pg_data);
255
256   return (0);
257 } /* }}} int list_graphs_html */
258
259 int action_list_graphs (void) /* {{{ */
260 {
261   char *search;
262   int status;
263
264   gl_update ();
265
266   search = strtolower_copy (param ("q"));
267   status = list_graphs_html (search);
268   free (search);
269
270   return (status);
271 } /* }}} int action_list_graphs */
272
273 /* vim: set sw=2 sts=2 et fdm=marker : */