Code

src/utils_cgi.[ch]: Move "html_print_search_box" to here.
[collection4.git] / src / action_list_graphs.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
6 #include "action_list_graphs.h"
7 #include "common.h"
8 #include "graph.h"
9 #include "graph_ident.h"
10 #include "graph_list.h"
11 #include "utils_cgi.h"
13 #include <fcgiapp.h>
14 #include <fcgi_stdio.h>
16 #define RESULT_LIMIT 50
18 struct callback_data_s
19 {
20   graph_config_t *cfg;
21   int limit;
22   _Bool first;
23 };
24 typedef struct callback_data_s callback_data_t;
26 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
27 {
28   char desc[1024];
30   if (cfg == NULL)
31     return (EINVAL);
33   graph_get_title (cfg, desc, sizeof (desc));
35   printf ("{\"title\":\"%s\",\"instances\":[", desc);
37   return (0);
38 } /* }}} int json_begin_graph */
40 static int json_end_graph (void) /* {{{ */
41 {
42   printf ("]}");
44   return (0);
45 } /* }}} int json_end_graph */
47 static int json_print_instance (graph_config_t *cfg, /* {{{ */
48     graph_instance_t *inst)
49 {
50   char params[1024];
51   char desc[1024];
53   if ((cfg == NULL) || (inst == NULL))
54     return (EINVAL);
56   memset (desc, 0, sizeof (desc));
57   inst_describe (cfg, inst, desc, sizeof (desc));
59   memset (params, 0, sizeof (params));
60   inst_get_params (cfg, inst, params, sizeof (params));
62   printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
63       desc, params);
65   return (0);
66 } /* }}} int json_print_instance */
68 static int print_graph_inst_json (graph_config_t *cfg, /* {{{ */
69     graph_instance_t *inst,
70     void *user_data)
71 {
72   callback_data_t *data = user_data;
74   if (data->cfg != cfg)
75   {
76     if (!data->first)
77     {
78       json_end_graph ();
79       printf (",\n");
80     }
81     json_begin_graph (cfg);
83     data->cfg = cfg;
84     data->first = 0;
85   }
86   else /* if (not first instance) */
87   {
88     printf (",\n");
89   }
91   json_print_instance (cfg, inst);
93   if (data->limit > 0)
94     data->limit--;
96   if (data->limit == 0)
97     return (1);
99   return (0);
100 } /* }}} int print_graph_inst_json */
102 static int list_graphs_json (const char *term) /* {{{ */
104   callback_data_t data;
106   time_t now;
107   char time_buffer[128];
108   int status;
110   printf ("Content-Type: application/json\n");
112   now = time (NULL);
113   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
114   if (status == 0)
115     printf ("Expires: %s\n"
116         "Cache-Control: public\n",
117         time_buffer);
118   printf ("\n");
120   data.cfg = NULL;
121   data.limit = RESULT_LIMIT;
122   data.first = 1;
124   printf ("[\n");
125   if (term == NULL)
126     gl_instance_get_all (print_graph_inst_json, /* user_data = */ &data);
127   else
128     gl_search (term, print_graph_inst_json, /* user_data = */ &data);
130   if (!data.first)
131     json_end_graph ();
133   printf ("\n]");
135   return (0);
136 } /* }}} int list_graphs_json */
138 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
139     graph_instance_t *inst,
140     void *user_data)
142   callback_data_t *data = user_data;
143   char params[1024];
144   char desc[1024];
146   if (data->cfg != cfg)
147   {
148     if (data->cfg != NULL)
149       printf ("  </ul></li>\n");
151     memset (desc, 0, sizeof (desc));
152     graph_get_title (cfg, desc, sizeof (desc));
153     html_escape_buffer (desc, sizeof (desc));
155     printf ("  <li class=\"graph\">%s\n"
156         "  <ul class=\"instance_list\">\n", desc);
158     data->cfg = cfg;
159   }
161   memset (params, 0, sizeof (params));
162   inst_get_params (cfg, inst, params, sizeof (params));
163   html_escape_buffer (params, sizeof (params));
165   memset (desc, 0, sizeof (desc));
166   inst_describe (cfg, inst, desc, sizeof (desc));
167   html_escape_buffer (desc, sizeof (desc));
169   printf ("    <li class=\"instance\"><a href=\"%s?action=graph;%s\">%s</a></li>\n",
170       script_name (), params, desc);
172   if (data->limit > 0)
173     data->limit--;
175   /* Abort scan if limit is reached. */
176   if (data->limit == 0)
177     return (1);
179   return (0);
180 } /* }}} int print_graph_inst_html */
182 struct page_data_s
184   const char *search_term;
185 };
186 typedef struct page_data_s page_data_t;
188 static int print_search_result (void *user_data) /* {{{ */
190   page_data_t *pg_data = user_data;
191   callback_data_t cb_data = { NULL, /* limit = */ RESULT_LIMIT, /* first = */ 1 };
193   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
194   if (pg_data->search_term == NULL)
195     gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
196   else
197   {
198     char *term_lc = strtolower_copy (pg_data->search_term);
199     gl_search (term_lc, print_graph_inst_html, /* user_data = */ &cb_data);
200     free (term_lc);
201   }
203   if (cb_data.cfg != NULL)
204     printf ("      </ul></li>\n");
206   printf ("    </ul>\n");
208   return (0);
209 } /* }}} int print_search_result */
211 struct print_host_list_data_s
213   str_array_t *array;
214   char *last_host;
215 };
216 typedef struct print_host_list_data_s print_host_list_data_t;
218 static int print_host_list_callback (graph_config_t *cfg, /* {{{ */
219     graph_instance_t *inst, void *user_data)
221   print_host_list_data_t *data = user_data;
222   graph_ident_t *ident;
223   const char *host;
225   /* make compiler happy */
226   cfg = NULL;
228   ident = inst_get_selector (inst);
229   if (ident == NULL)
230     return (-1);
232   host = ident_get_host (ident);
233   if (host == NULL)
234   {
235     ident_destroy (ident);
236     return (-1);
237   }
239   if (IS_ALL (host))
240     return (0);
242   if ((data->last_host != NULL)
243       && (strcmp (data->last_host, host) == 0))
244   {
245     ident_destroy (ident);
246     return (0);
247   }
249   free (data->last_host);
250   data->last_host = strdup (host);
252   array_append (data->array, host);
254   ident_destroy (ident);
255   return (0);
256 } /* }}} int print_host_list_callback */
258 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
260   print_host_list_data_t data;
261   int hosts_argc;
262   char **hosts_argv;
263   int i;
265   data.array = array_create ();
266   data.last_host = NULL;
268   gl_instance_get_all (print_host_list_callback, &data);
270   free (data.last_host);
271   data.last_host = NULL;
273   array_sort (data.array);
275   hosts_argc = array_argc (data.array);
276   hosts_argv = array_argv (data.array);
278   if (hosts_argc < 1)
279   {
280     array_destroy (data.array);
281     return (0);
282   }
284   printf ("<ul id=\"host-list\">\n");
285   for (i = 0; i < hosts_argc; i++)
286   {
287     char *host = hosts_argv[i];
288     char *host_html;
290     if ((data.last_host != NULL) && (strcmp (data.last_host, host) == 0))
291       continue;
292     data.last_host = host;
294     host_html = html_escape (host);
296     printf ("  <li><a href=\"%s?action=list_graphs&search=%s\">%s</a></li>\n",
297         script_name (), host_html, host_html);
299     free (host_html);
300   }
301   printf ("</ul>\n");
303   array_destroy (data.array);
305   return (0);
306 } /* }}} int print_host_list */
308 static int list_graphs_html (const char *term) /* {{{ */
310   page_data_t pg_data;
311   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
312   char title[512];
314   if (term != NULL)
315     snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
316   else
317     strncpy (title, "c4: List of all graphs", sizeof (title));
318   title[sizeof (title) - 1] = 0;
320   memset (&pg_data, 0, sizeof (pg_data));
321   pg_data.search_term = term;
323   pg_callbacks.top_right = html_print_search_box;
324   pg_callbacks.middle_left = print_host_list;
325   pg_callbacks.middle_center = print_search_result;
327   html_print_page (title, &pg_callbacks, &pg_data);
329   return (0);
330 } /* }}} int list_graphs_html */
332 int action_list_graphs (void) /* {{{ */
334   const char *format;
335   char *search;
336   int status;
338   gl_update ();
340   search = strtolower_copy (param ("search"));
342   format = param ("format");
343   if (format == NULL)
344     format = "html";
346   if (strcmp ("json", format) == 0)
347     status = list_graphs_json (search);
348   else
349     status = list_graphs_html (search);
351   free (search);
353   return (status);
354 } /* }}} int action_list_graphs */
356 /* vim: set sw=2 sts=2 et fdm=marker : */