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