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