1 /**
2 * collection4 - action_graph_data_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_graph_data_json.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_instance.h"
36 #include "graph_list.h"
37 #include "utils_cgi.h"
39 #include <fcgiapp.h>
40 #include <fcgi_stdio.h>
42 /* Expire data after one day. */
43 #define EXPIRES_SECS 86400
45 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
46 const char *str, unsigned int len)
47 {
48 fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
49 } /* }}} void write_callback */
51 int action_graph_data_json (void) /* {{{ */
52 {
53 graph_config_t *cfg;
54 graph_instance_t *inst;
56 time_t tt_begin = 0;
57 time_t tt_end = 0;
58 time_t tt_now = 0;
60 dp_time_t dp_begin = { 0, 0 };
61 dp_time_t dp_end = { 0, 0 };
63 yajl_gen_config handler_config;
64 yajl_gen handler;
66 time_t expires;
67 char time_buffer[128];
68 int status;
70 cfg = gl_graph_get_selected ();
71 if (cfg == NULL)
72 return (ENOMEM);
74 inst = inst_get_selected (cfg);
75 if (inst == NULL)
76 return (EINVAL);
78 /* Get selected time(s) */
79 tt_begin = tt_end = tt_now = 0;
80 status = get_time_args (&tt_begin, &tt_end, &tt_now);
81 if (status != 0)
82 return (status);
84 dp_begin.tv_sec = tt_begin;
85 dp_begin.tv_nsec = 0;
86 dp_end.tv_sec = tt_end;
87 dp_end.tv_nsec = 0;
89 memset (&handler_config, 0, sizeof (handler_config));
90 handler_config.beautify = 0;
91 handler_config.indentString = " ";
93 handler = yajl_gen_alloc2 (write_callback,
94 &handler_config,
95 /* alloc functions = */ NULL,
96 /* context = */ NULL);
97 if (handler == NULL)
98 {
99 graph_destroy (cfg);
100 return (-1);
101 }
103 printf ("Content-Type: application/json\n");
105 /* By default, permit caching until 1/1000th after the last data. If that
106 * data is in the past, assume the entire data is in the past and allow
107 * caching for one day. */
108 expires = tt_end + ((tt_end - tt_begin) / 1000);
109 if (expires < tt_now)
110 expires = tt_now + 86400;
112 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
113 if (status == 0)
114 printf ("Expires: %s\n"
115 "Cache-Control: public\n",
116 time_buffer);
117 printf ("\n");
119 status = inst_data_to_json (inst,
120 dp_begin, dp_end, handler);
122 graph_destroy (cfg);
123 yajl_gen_free (handler);
125 return (status);
126 } /* }}} int action_graph_data_json */
128 /* vim: set sw=2 sts=2 et fdm=marker : */