"show graph" action: Print a menu on the left side.
[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 };
55 typedef struct show_graph_data_s show_graph_data_t;
56
57 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
58 {
59   param_list_t *pl;
60
61   pl = param_create (/* query string = */ NULL);
62   param_set (pl, "begin", NULL);
63   param_set (pl, "end", NULL);
64   param_set (pl, "button", NULL);
65
66   printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
67
68   param_print_hidden (pl);
69
70   printf ("  <select name=\"begin\">\n"
71       "    <option value=\"-3600\">Hour</option>\n"
72       "    <option value=\"-86400\">Day</option>\n"
73       "    <option value=\"-604800\">Week</option>\n"
74       "    <option value=\"-2678400\">Month</option>\n"
75       "    <option value=\"-31622400\">Year</option>\n"
76       "  </select>\n"
77       "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
78
79   printf ("</form>\n");
80
81   param_destroy (pl);
82
83   return (0);
84 } /* }}} int show_time_selector */
85
86 static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
87 {
88   printf ("\n<ul class=\"menu left\">\n"
89       "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
90       "</ul>\n",
91       script_name ());
92
93   return (0);
94 } /* }}} int left_menu */
95
96 static int show_instance_cb (graph_instance_t *inst, /* {{{ */
97     void *user_data)
98 {
99   show_graph_data_t *data = user_data;
100   char descr[128];
101   char params[1024];
102
103   memset (descr, 0, sizeof (descr));
104   inst_describe (data->cfg, inst, descr, sizeof (descr));
105   html_escape_buffer (descr, sizeof (descr));
106
107   memset (params, 0, sizeof (params));
108   inst_get_params (data->cfg, inst, params, sizeof (params));
109   html_escape_buffer (params, sizeof (params));
110
111   printf ("  <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">"
112       "%s</a></li>\n",
113       script_name (), params, descr);
114
115   return (0);
116 } /* }}} int show_instance_cb */
117
118 static int show_graph (void *user_data) /* {{{ */
119 {
120   show_graph_data_t *data = user_data;
121
122   printf ("<h2>Available instances</h2>\n");
123   printf ("<ul class=\"instance_list\">\n");
124   graph_inst_foreach (data->cfg, show_instance_cb, data);
125   printf ("</ul>\n");
126
127   return (0);
128 } /* }}} int show_graph */
129
130 int action_show_graph (void) /* {{{ */
131 {
132   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
133   show_graph_data_t pg_data;
134
135   char tmp[128];
136   char title[128];
137
138   memset (&pg_data, 0, sizeof (pg_data));
139   pg_data.cfg = gl_graph_get_selected ();
140   if (pg_data.cfg == NULL)
141     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
142
143   memset (tmp, 0, sizeof (tmp));
144   graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
145   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
146   title[sizeof (title) - 1] = 0;
147
148   pg_callbacks.top_right = html_print_search_box;
149   pg_callbacks.middle_left = left_menu;
150   pg_callbacks.middle_center = show_graph;
151   pg_callbacks.middle_right = show_time_selector;
152
153   html_print_page (title, &pg_callbacks, &pg_data);
154
155   return (0);
156 } /* }}} int action_show_graph */
157
158 /* vim: set sw=2 sts=2 et fdm=marker : */