a182a626baf620e9ed456c95a10cd1b70bb05689
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;
82 char title[1024];
83 graph_ident_t *ident;
84 char *ident_json;
86 memset (&data, 0, sizeof (data));
87 data.first = 1;
88 data.cfg = gl_graph_get_selected ();
89 if (data.cfg == NULL)
90 return (ENOMEM);
92 ident = graph_get_selector (data.cfg);
93 if (ident == NULL)
94 return (ENOMEM);
96 ident_json = ident_to_json (ident);
97 if (ident_json == NULL)
98 {
99 ident_destroy (ident);
100 return (ENOMEM);
101 }
103 printf ("Content-Type: text/plain\n\n");
105 memset (title, 0, sizeof (title));
106 graph_get_title (data.cfg, title, sizeof (title));
107 json_escape_buffer (title, sizeof (title));
109 printf ("{\n");
110 printf (" \"title\": \"%s\",\n", title);
111 printf (" \"selector\": %s,\n", ident_json);
112 printf (" \"instances\":\n");
113 printf (" [\n");
114 graph_inst_foreach (data.cfg, show_instance_cb, &data);
115 printf ("\n ]\n}\n");
117 free (ident_json);
118 ident_destroy (ident);
119 return (0);
120 } /* }}} int action_show_graph_json */
122 /* vim: set sw=2 sts=2 et fdm=marker : */