share/collection.js: Keep visibility when redrawing graphs.
[collection4.git] / src / action_show_graph.c
1 /**
2  * collection4 - action_show_graph.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 #include <stdint.h>
29 #include <inttypes.h>
30 #include <assert.h>
31
32 #include "action_show_graph.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
39
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
42
43 #define OUTPUT_ERROR(...) do {             \
44   printf ("Content-Type: text/plain\n\n"); \
45   printf (__VA_ARGS__);                    \
46   return (0);                              \
47 } while (0)
48
49 #define MAX_SHOW_GRAPHS 10
50
51 struct show_graph_data_s
52 {
53   graph_config_t *cfg;
54   const char *search_term;
55 };
56 typedef struct show_graph_data_s show_graph_data_t;
57
58 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
59 {
60   param_list_t *pl;
61
62   pl = param_create (/* query string = */ NULL);
63   param_set (pl, "begin", NULL);
64   param_set (pl, "end", NULL);
65   param_set (pl, "button", NULL);
66
67   printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
68
69   param_print_hidden (pl);
70
71   printf ("  <select name=\"begin\">\n"
72       "    <option value=\"-3600\">Hour</option>\n"
73       "    <option value=\"-86400\">Day</option>\n"
74       "    <option value=\"-604800\">Week</option>\n"
75       "    <option value=\"-2678400\">Month</option>\n"
76       "    <option value=\"-31622400\">Year</option>\n"
77       "  </select>\n"
78       "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
79
80   printf ("</form>\n");
81
82   param_destroy (pl);
83
84   return (0);
85 } /* }}} int show_time_selector */
86
87 static int left_menu (void *user_data) /* {{{ */
88 {
89   show_graph_data_t *data = user_data;
90
91   printf ("\n<ul class=\"menu left\">\n");
92
93   if ((data->search_term != NULL) && (data->search_term[0] != 0))
94   {
95     char params[1024];
96
97     memset (params, 0, sizeof (params));
98     graph_get_params (data->cfg, params, sizeof (params));
99     html_escape_buffer (params, sizeof (params));
100
101     printf ("  <li><a href=\"%s?action=show_graph;%s\">"
102         "All instances</a></li>\n",
103         script_name (), params);
104   }
105
106   printf ("  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
107       "</ul>\n",
108       script_name ());
109
110   return (0);
111 } /* }}} int left_menu */
112
113 static int show_instance_cb (graph_instance_t *inst, /* {{{ */
114     void *user_data)
115 {
116   show_graph_data_t *data = user_data;
117   char descr[128];
118   char params[1024];
119
120   if ((data->search_term != NULL) && (data->search_term[0] != 0))
121   {
122     _Bool matches = 0;
123     char *term_lc = strtolower_copy (data->search_term);
124
125     if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
126     {
127       if (inst_matches_field (inst, GIF_HOST, term_lc + strlen ("host:")))
128         matches = 1;
129     }
130     else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
131     {
132       if (inst_matches_field (inst, GIF_PLUGIN, term_lc + strlen ("plugin:")))
133         matches = 1;
134     }
135     else if (strncmp ("plugin_instance:", term_lc,
136           strlen ("plugin_instance:")) == 0)
137     {
138       if (inst_matches_field (inst, GIF_PLUGIN_INSTANCE,
139             term_lc + strlen ("plugin_instance:")))
140         matches = 1;
141     }
142     else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
143     {
144       if (inst_matches_field (inst, GIF_TYPE, term_lc + strlen ("type:")))
145         matches = 1;
146     }
147     else if (strncmp ("type_instance:", term_lc,
148           strlen ("type_instance:")) == 0)
149     {
150       if (inst_matches_field (inst, GIF_TYPE_INSTANCE,
151             term_lc + strlen ("type_instance:")))
152         matches = 1;
153     }
154     else if (inst_matches_string (data->cfg, inst, term_lc))
155     {
156       matches = 1;
157     }
158
159     free (term_lc);
160
161     if (!matches)
162       return (0);
163   } /* if (data->search_term) */
164
165   memset (descr, 0, sizeof (descr));
166   inst_describe (data->cfg, inst, descr, sizeof (descr));
167   html_escape_buffer (descr, sizeof (descr));
168
169   memset (params, 0, sizeof (params));
170   inst_get_params (data->cfg, inst, params, sizeof (params));
171   html_escape_buffer (params, sizeof (params));
172
173   printf ("  <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">"
174       "%s</a></li>\n",
175       script_name (), params, descr);
176
177   return (0);
178 } /* }}} int show_instance_cb */
179
180 static int show_graph (void *user_data) /* {{{ */
181 {
182   show_graph_data_t *data = user_data;
183
184   if ((data->search_term == NULL) || (data->search_term[0] == 0))
185     printf ("<h2>All instances</h2>\n");
186   else
187   {
188     char *search_term_html = html_escape (data->search_term);
189     printf ("<h2>Instances matching &quot;%s&quot;</h2>\n",
190         search_term_html);
191     free (search_term_html);
192   }
193
194   printf ("<ul class=\"instance_list\">\n");
195   graph_inst_foreach (data->cfg, show_instance_cb, data);
196   printf ("</ul>\n");
197
198   return (0);
199 } /* }}} int show_graph */
200
201 int action_show_graph (void) /* {{{ */
202 {
203   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
204   show_graph_data_t pg_data;
205
206   char tmp[128];
207   char title[128];
208
209   memset (&pg_data, 0, sizeof (pg_data));
210   pg_data.cfg = gl_graph_get_selected ();
211   if (pg_data.cfg == NULL)
212     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
213
214   pg_data.search_term = param ("q");
215
216   memset (tmp, 0, sizeof (tmp));
217   graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
218   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
219   title[sizeof (title) - 1] = 0;
220
221   pg_callbacks.top_right = html_print_search_box;
222   pg_callbacks.middle_left = left_menu;
223   pg_callbacks.middle_center = show_graph;
224   pg_callbacks.middle_right = show_time_selector;
225
226   html_print_page (title, &pg_callbacks, &pg_data);
227
228   return (0);
229 } /* }}} int action_show_graph */
230
231 /* vim: set sw=2 sts=2 et fdm=marker : */