1 /**
2 * collection4 - action_show_graph_json.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 **/
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>
32 #include "action_show_graph_json.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"
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
43 struct show_graph_data_s
44 {
45 graph_config_t *cfg;
46 _Bool first;
47 };
48 typedef struct show_graph_data_s show_graph_data_t;
50 static int show_instance_cb (graph_instance_t *inst, /* {{{ */
51 void *user_data)
52 {
53 show_graph_data_t *data = user_data;
54 graph_ident_t *ident;
55 char *ident_json;
57 ident = inst_get_selector (inst);
58 if (ident == NULL)
59 return (ENOMEM);
61 ident_json = ident_to_json (ident);
62 if (ident_json == NULL)
63 {
64 ident_destroy (ident);
65 return (ENOMEM);
66 }
68 if (!data->first)
69 printf (",\n");
70 data->first = 0;
72 printf (" %s", ident_json);
74 free (ident_json);
75 ident_destroy (ident);
76 return (0);
77 } /* }}} int show_instance_cb */
79 int action_show_graph_json (void) /* {{{ */
80 {
81 show_graph_data_t data;
83 time_t now;
84 char time_buffer[128];
85 int status;
87 char title[1024];
88 graph_ident_t *ident;
89 char *ident_json;
91 memset (&data, 0, sizeof (data));
92 data.first = 1;
93 data.cfg = gl_graph_get_selected ();
94 if (data.cfg == NULL)
95 return (ENOMEM);
97 ident = graph_get_selector (data.cfg);
98 if (ident == NULL)
99 return (ENOMEM);
101 ident_json = ident_to_json (ident);
102 if (ident_json == NULL)
103 {
104 ident_destroy (ident);
105 return (ENOMEM);
106 }
108 printf ("Content-Type: text/plain\n");
110 now = time (NULL);
111 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
112 if (status == 0)
113 printf ("Expires: %s\n"
114 "Cache-Control: public\n",
115 time_buffer);
116 printf ("\n");
118 memset (title, 0, sizeof (title));
119 graph_get_title (data.cfg, title, sizeof (title));
120 json_escape_buffer (title, sizeof (title));
122 printf ("{\n");
123 printf (" \"title\": \"%s\",\n", title);
124 printf (" \"selector\": %s,\n", ident_json);
125 printf (" \"instances\":\n");
126 printf (" [\n");
127 graph_inst_foreach (data.cfg, show_instance_cb, &data);
128 printf ("\n ]\n}\n");
130 free (ident_json);
131 ident_destroy (ident);
132 return (0);
133 } /* }}} int action_show_graph_json */
135 /* vim: set sw=2 sts=2 et fdm=marker : */