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 **/
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
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"
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
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 */
44 static int print_one_graph (graph_config_t *cfg, /* {{{ */
45 void *user_data)
46 {
47 char title[1024];
48 size_t num_instances;
49 graph_ident_t *selector;
51 yajl_gen handler = user_data;
53 num_instances = graph_num_instances (cfg);
54 if (num_instances < 1)
55 return (0);
57 selector = graph_get_selector (cfg);
58 if (selector == NULL)
59 {
60 /* TODO: Print error. */
61 return (0);
62 }
64 yajl_gen_map_open (handler);
66 memset (title, 0, sizeof (title));
67 graph_get_title (cfg, title, sizeof (title));
69 yajl_gen_string (handler,
70 (unsigned char *) "title",
71 (unsigned int) strlen ("title"));
72 yajl_gen_string (handler,
73 (unsigned char *) title,
74 (unsigned int) strlen (title));
76 yajl_gen_string (handler,
77 (unsigned char *) "selector",
78 (unsigned int) strlen ("selector"));
79 ident_to_json (selector, handler);
81 yajl_gen_string (handler,
82 (unsigned char *) "num_instances",
83 (unsigned int) strlen ("num_instances"));
84 yajl_gen_integer (handler, (long int) num_instances);
86 yajl_gen_map_close (handler);
88 ident_destroy (selector);
90 return (0);
91 } /* }}} int print_one_graph */
93 static int print_all_graphs (yajl_gen handler) /* {{{ */
94 {
95 const char *dynamic;
96 _Bool include_dynamic = 0;
98 dynamic = param ("dynamic");
99 if ((dynamic != NULL)
100 && (strcmp ("true", dynamic) == 0))
101 include_dynamic = 1;
103 yajl_gen_array_open (handler);
105 gl_graph_get_all (include_dynamic, print_one_graph,
106 /* user_data = */ handler);
108 yajl_gen_array_close (handler);
110 return (0);
111 } /* }}} int print_all_graphs */
113 int action_list_graphs_json (void) /* {{{ */
114 {
115 graph_config_t *cfg;
117 yajl_gen_config handler_config;
118 yajl_gen handler;
120 time_t now;
121 char time_buffer[128];
122 int status;
124 memset (&handler_config, 0, sizeof (handler_config));
125 handler_config.beautify = 1;
126 handler_config.indentString = " ";
128 handler = yajl_gen_alloc2 (write_callback,
129 &handler_config,
130 /* alloc functions = */ NULL,
131 /* context = */ NULL);
132 if (handler == NULL)
133 return (-1);
135 printf ("Content-Type: application/json\n");
137 now = time (NULL);
138 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
139 if (status == 0)
140 printf ("Expires: %s\n"
141 "Cache-Control: public\n",
142 time_buffer);
143 printf ("\n");
145 print_all_graphs (handler);
147 yajl_gen_free (handler);
149 return (status);
150 } /* }}} int action_list_graphs_json */
152 /* vim: set sw=2 sts=2 et fdm=marker : */