"list graphs" action: Support searching for plugin- and type-instances.
[collection4.git] / src / action_show_graph.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <assert.h>
8
9 #include "action_show_graph.h"
10 #include "common.h"
11 #include "graph.h"
12 #include "graph_ident.h"
13 #include "graph_instance.h"
14 #include "graph_list.h"
15 #include "utils_cgi.h"
16
17 #include <fcgiapp.h>
18 #include <fcgi_stdio.h>
19
20 #define OUTPUT_ERROR(...) do {             \
21   printf ("Content-Type: text/plain\n\n"); \
22   printf (__VA_ARGS__);                    \
23   return (0);                              \
24 } while (0)
25
26 struct show_graph_data_s
27 {
28   graph_config_t *cfg;
29   graph_instance_t *inst;
30 };
31 typedef struct show_graph_data_s show_graph_data_t;
32
33 static void show_breadcrump_field (const char *str, /* {{{ */
34     const char *field_name)
35 {
36   if ((str == NULL) || (str[0] == 0))
37     printf ("<em>none</em>");
38   else if (IS_ANY (str))
39     printf ("<em>any</em>");
40   else if (IS_ALL (str))
41     printf ("<em>all</em>");
42   else
43   {
44     char *str_html = html_escape (str);
45
46     if (field_name != NULL)
47       printf ("<a href=\"%s?action=list_graphs;q=%s:%s\">%s</a>",
48           script_name (), field_name, str_html, str_html);
49     else
50       printf ("<a href=\"%s?action=list_graphs;q=%s\">%s</a>",
51           script_name (), str_html, str_html);
52
53     free (str_html);
54   }
55 } /* }}} void show_breadcrump_field */
56
57 static int show_breadcrump (show_graph_data_t *data) /* {{{ */
58 {
59   graph_ident_t *ident;
60   char *prefix;
61
62   if (data->inst != NULL)
63   {
64     prefix = "Instance";
65     ident = inst_get_selector (data->inst);
66   }
67   else
68   {
69     prefix = "Graph";
70     ident = graph_get_selector (data->cfg);
71   }
72
73   printf ("<div class=\"breadcrump\">%s: &quot;", prefix);
74   show_breadcrump_field (ident_get_host (ident), "host");
75   printf ("&nbsp;/ ");
76   show_breadcrump_field (ident_get_plugin (ident), "plugin");
77   printf ("&nbsp;&ndash; ");
78   show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
79   printf ("&nbsp;/ ");
80   show_breadcrump_field (ident_get_type (ident), "type");
81   printf ("&nbsp;&ndash; ");
82   show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
83   printf ("&quot;</div>\n");
84
85   return (0);
86 } /* }}} int show_breadcrump */
87
88 static int show_instance_list_cb (graph_instance_t *inst, /* {{{ */
89     void *user_data)
90 {
91   show_graph_data_t *data = user_data;
92   char descr[128];
93   char params[1024];
94
95   memset (descr, 0, sizeof (descr));
96   inst_describe (data->cfg, inst, descr, sizeof (descr));
97   html_escape_buffer (descr, sizeof (descr));
98
99   if (inst == data->inst)
100   {
101     printf ("    <li class=\"instance\"><strong>%s</strong></li>\n", descr);
102     return (0);
103   }
104
105   memset (params, 0, sizeof (params));
106   inst_get_params (data->cfg, inst, params, sizeof (params));
107   html_escape_buffer (params, sizeof (params));
108
109   printf ("    <li class=\"instance\">"
110       "<a href=\"%s?action=show_graph;%s\">%s</a></li>\n",
111       script_name (), params, descr);
112
113   return (0);
114 } /* }}} int show_instance_list_cb */
115
116 static int show_instance_list (void *user_data) /* {{{ */
117 {
118   show_graph_data_t *data = user_data;
119   char title[128];
120   char params[1024];
121
122   memset (title, 0, sizeof (title));
123   graph_get_title (data->cfg, title, sizeof (title));
124   html_escape_buffer (title, sizeof (title));
125
126   memset (params, 0, sizeof (params));
127   graph_get_params (data->cfg, params, sizeof (params));
128   html_escape_buffer (params, sizeof (params));
129
130   printf ("<ul class=\"graph_list\">\n"
131       "  <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">%s</a>\n"
132       "  <ul class=\"instance_list\">\n",
133       script_name (), params, title);
134
135   graph_inst_foreach (data->cfg, show_instance_list_cb, user_data);
136
137   printf ("  </ul>\n"
138       "</ul>\n");
139
140   return (0);
141 } /* }}} int show_instance_list */
142
143 static int show_instance (void *user_data) /* {{{ */
144 {
145   show_graph_data_t *data = user_data;
146   char title[128];
147   char descr[128];
148   char params[1024];
149
150   show_breadcrump (data);
151
152   memset (title, 0, sizeof (title));
153   graph_get_title (data->cfg, title, sizeof (title));
154   html_escape_buffer (title, sizeof (title));
155
156   memset (descr, 0, sizeof (descr));
157   inst_describe (data->cfg, data->inst, descr, sizeof (descr));
158   html_escape_buffer (descr, sizeof (descr));
159
160   memset (params, 0, sizeof (params));
161   inst_get_params (data->cfg, data->inst, params, sizeof (params));
162   html_escape_buffer (params, sizeof (params));
163
164   printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s\" "
165       "title=\"%s / %s\" /></div>\n",
166       script_name (), params, title, descr);
167
168   return (0);
169 } /* }}} int show_instance */
170
171 static int show_graph (void *user_data)
172 {
173   show_graph_data_t *data = user_data;
174
175   show_breadcrump (data);
176   return (show_instance_list (user_data));
177 } /* }}} int show_graph */
178
179 int action_show_graph (void) /* {{{ */
180 {
181   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
182   show_graph_data_t pg_data;
183
184   char title[128];
185
186   pg_data.cfg = gl_graph_get_selected ();
187   if (pg_data.cfg == NULL)
188     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
189
190   memset (title, 0, sizeof (title));
191   graph_get_title (pg_data.cfg, title, sizeof (title));
192
193   pg_data.inst = inst_get_selected (pg_data.cfg);
194   if (pg_data.inst != NULL)
195   {
196     char descr[128];
197     char html_title[128];
198
199     memset (descr, 0, sizeof (descr));
200     inst_describe (pg_data.cfg, pg_data.inst, descr, sizeof (descr));
201
202     snprintf (html_title, sizeof (html_title), "Graph \"%s / %s\"",
203         title, descr);
204     html_title[sizeof (html_title) - 1] = 0;
205
206     pg_callbacks.top_right = html_print_search_box;
207     pg_callbacks.middle_center = show_instance;
208     pg_callbacks.middle_left = show_instance_list;
209
210     html_print_page (html_title, &pg_callbacks, &pg_data);
211   }
212   else /* if (pg_data.inst == NULL) */
213   {
214     pg_callbacks.top_right = html_print_search_box;
215     pg_callbacks.middle_center = show_graph;
216
217     html_print_page (title, &pg_callbacks, &pg_data);
218   }
219
220   return (0);
221 } /* }}} int action_graph */
222
223 /* vim: set sw=2 sts=2 et fdm=marker : */