120e419b6a78af0cd8fe91593181b6bbbe3fd6c9
[collection4.git] / src / action_list_graphs_json.c
1 /**
2  * collection4 - action_list_graphs_json.c
3  * Copyright (C) 2010,2011  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_json.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_list.h"
33 #include "utils_cgi.h"
34
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
37
38 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
39     const char *str, unsigned int len)
40 {
41   fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
42 } /* }}} void write_callback */
43
44 static int print_one_graph (graph_config_t *cfg, /* {{{ */
45     void *user_data)
46 {
47   char params[1024];
48   char title[1024];
49   size_t num_instances;
50
51   yajl_gen handler = user_data;
52
53   num_instances = graph_num_instances (cfg);
54   if (num_instances < 1)
55     return (0);
56
57   yajl_gen_map_open (handler);
58
59   memset (title, 0, sizeof (title));
60   graph_get_title (cfg, title, sizeof (title));
61
62   memset (params, 0, sizeof (params));
63   graph_get_params (cfg, params, sizeof (params));
64
65   yajl_gen_string (handler,
66       (unsigned char *) "title",
67       (unsigned int) strlen ("title"));
68   yajl_gen_string (handler,
69       (unsigned char *) title,
70       (unsigned int) strlen (title));
71
72   yajl_gen_string (handler,
73       (unsigned char *) "params",
74       (unsigned int) strlen ("params"));
75   yajl_gen_string (handler,
76       (unsigned char *) params,
77       (unsigned int) strlen (params));
78
79   yajl_gen_string (handler,
80       (unsigned char *) "num_instances",
81       (unsigned int) strlen ("num_instances"));
82   yajl_gen_integer (handler, (long int) num_instances);
83
84   yajl_gen_map_close (handler);
85
86   return (0);
87 } /* }}} int print_one_graph */
88
89 static int print_all_graphs (yajl_gen handler) /* {{{ */
90 {
91   const char *dynamic;
92   _Bool include_dynamic = 0;
93
94   dynamic = param ("dynamic");
95   if ((dynamic != NULL)
96       && (strcmp ("true", dynamic) == 0))
97     include_dynamic = 1;
98
99   yajl_gen_array_open (handler);
100
101   gl_graph_get_all (include_dynamic, print_one_graph,
102       /* user_data = */ handler);
103
104   yajl_gen_array_close (handler);
105
106   return (0);
107 } /* }}} int print_all_graphs */
108
109 int action_list_graphs_json (void) /* {{{ */
110 {
111   graph_config_t *cfg;
112
113   yajl_gen_config handler_config;
114   yajl_gen handler;
115
116   time_t now;
117   char time_buffer[128];
118   int status;
119
120   memset (&handler_config, 0, sizeof (handler_config));
121   handler_config.beautify = 1;
122   handler_config.indentString = "  ";
123
124   handler = yajl_gen_alloc2 (write_callback,
125       &handler_config,
126       /* alloc functions = */ NULL,
127       /* context = */ NULL);
128   if (handler == NULL)
129     return (-1);
130
131   printf ("Content-Type: application/json\n");
132
133   now = time (NULL);
134   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
135   if (status == 0)
136     printf ("Expires: %s\n"
137         "Cache-Control: public\n",
138         time_buffer);
139   printf ("\n");
140
141   print_all_graphs (handler);
142
143   yajl_gen_free (handler);
144
145   return (status);
146 } /* }}} int action_list_graphs_json */
147
148 /* vim: set sw=2 sts=2 et fdm=marker : */