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