All files: Add LGPL 2.1 license 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 };
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       printf ("    <li class=\"instance more\">More ...</li>\n");
92       data->inst_more = 1;
93     }
94     return (0);
95   }
96
97   memset (params, 0, sizeof (params));
98   inst_get_params (cfg, inst, params, sizeof (params));
99   html_escape_buffer (params, sizeof (params));
100
101   memset (desc, 0, sizeof (desc));
102   inst_describe (cfg, inst, desc, sizeof (desc));
103   html_escape_buffer (desc, sizeof (desc));
104
105   printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
106       script_name (), params, desc);
107
108   return (0);
109 } /* }}} int print_graph_inst_html */
110
111 struct page_data_s
112 {
113   const char *search_term;
114 };
115 typedef struct page_data_s page_data_t;
116
117 static int print_search_result (void *user_data) /* {{{ */
118 {
119   page_data_t *pg_data = user_data;
120   callback_data_t cb_data = { /* cfg = */ NULL,
121     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
122     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0 };
123
124   if (pg_data->search_term != NULL)
125   {
126     char *search_term_html = html_escape (pg_data->search_term);
127     printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
128         search_term_html);
129     free (search_term_html);
130   }
131
132   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
133   if (pg_data->search_term == NULL)
134     gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
135   else
136   {
137     char *term_lc = strtolower_copy (pg_data->search_term);
138
139     if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
140       gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
141           print_graph_inst_html, /* user_data = */ &cb_data);
142     else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
143       gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
144           print_graph_inst_html, /* user_data = */ &cb_data);
145     else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
146       gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
147           print_graph_inst_html, /* user_data = */ &cb_data);
148     else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
149       gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
150           print_graph_inst_html, /* user_data = */ &cb_data);
151     else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
152       gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
153           print_graph_inst_html, /* user_data = */ &cb_data);
154     else
155       gl_search (term_lc,
156           print_graph_inst_html, /* user_data = */ &cb_data);
157
158     free (term_lc);
159   }
160
161   if (cb_data.cfg != NULL)
162     printf ("      </ul></li>\n");
163
164   if (cb_data.graph_more)
165   {
166     printf ("    <li class=\"graph more\">More ...</li>\n");
167   }
168
169   printf ("    </ul>\n");
170
171   return (0);
172 } /* }}} int print_search_result */
173
174 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
175 {
176   char *host_html;
177
178   /* Make compiler happy */
179   user_data = NULL;
180
181   if (host == NULL)
182     return (EINVAL);
183   
184   host_html = html_escape (host);
185   if (host_html == NULL)
186     return (ENOMEM);
187
188   printf ("  <li class=\"host\"><a href=\"%s?action=list_graphs;q=host:%s\">"
189       "%s</a></li>\n",
190       script_name (), host_html, host_html);
191
192   return (0);
193 } /* }}} int print_host_list_callback */
194
195 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
196 {
197   printf ("<div><h3>List of hosts</h3>\n"
198       "<ul id=\"host-list\">\n");
199   gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
200   printf ("</ul></div>\n");
201
202   return (0);
203 } /* }}} int print_host_list */
204
205 static int list_graphs_html (const char *term) /* {{{ */
206 {
207   page_data_t pg_data;
208   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
209   char title[512];
210
211   if (term != NULL)
212     snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
213   else
214     strncpy (title, "c4: List of all graphs", sizeof (title));
215   title[sizeof (title) - 1] = 0;
216
217   memset (&pg_data, 0, sizeof (pg_data));
218   pg_data.search_term = term;
219
220   pg_callbacks.top_right = html_print_search_box;
221   pg_callbacks.middle_left = print_host_list;
222   pg_callbacks.middle_center = print_search_result;
223
224   html_print_page (title, &pg_callbacks, &pg_data);
225
226   return (0);
227 } /* }}} int list_graphs_html */
228
229 int action_list_graphs (void) /* {{{ */
230 {
231   char *search;
232   int status;
233
234   gl_update ();
235
236   search = strtolower_copy (param ("q"));
237   status = list_graphs_html (search);
238   free (search);
239
240   return (status);
241 } /* }}} int action_list_graphs */
242
243 /* vim: set sw=2 sts=2 et fdm=marker : */