Merge remote-tracking branch 'github/master'
[collection4.git] / src / action_instance_data_json.c
1 /**
2  * collection4 - action_instance_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  **/
23
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>
31
32 #include "action_instance_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"
38
39 #include <fcgiapp.h>
40 #include <fcgi_stdio.h>
41
42 /* Expire data after one day. */
43 #define EXPIRES_SECS 86400
44
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 */
50
51 static int param_get_resolution (dp_time_t *resolution) /* {{{ */
52 {
53   const char *tmp;
54   char *endptr;
55   double value;
56
57   tmp = param ("resolution");
58   if (tmp == NULL)
59     return (ENOENT);
60
61   errno = 0;
62   endptr = NULL;
63   value = strtod (tmp, &endptr);
64   if (errno != 0)
65     return (errno);
66   else if ((value <= 0.0) || (endptr == tmp))
67     return (EINVAL);
68
69   resolution->tv_sec = (time_t) value;
70   resolution->tv_nsec = (long) ((value - ((double) resolution->tv_sec)) * 1000000000.0);
71   return (0);
72 } /* }}} int param_get_resolution */
73
74 int action_instance_data_json (void) /* {{{ */
75 {
76   graph_config_t *cfg;
77   graph_instance_t *inst;
78
79   time_t tt_begin = 0;
80   time_t tt_end = 0;
81   time_t tt_now = 0;
82
83   dp_time_t dp_begin = { 0, 0 };
84   dp_time_t dp_end = { 0, 0 };
85   dp_time_t dp_resolution = { 0, 0 };
86
87   yajl_gen_config handler_config;
88   yajl_gen handler;
89
90   time_t expires;
91   char time_buffer[128];
92   int status;
93
94   cfg = gl_graph_get_selected ();
95   if (cfg == NULL)
96     return (ENOMEM);
97
98   inst = inst_get_selected (cfg);
99   if (inst == NULL)
100     return (EINVAL);
101
102   /* Get selected time(s) */
103   tt_begin = tt_end = tt_now = 0;
104   status = get_time_args (&tt_begin, &tt_end, &tt_now);
105   if (status != 0)
106     return (status);
107
108   dp_begin.tv_sec = tt_begin;
109   dp_begin.tv_nsec = 0;
110   dp_end.tv_sec = tt_end;
111   dp_end.tv_nsec = 0;
112
113   dp_resolution.tv_sec = (tt_end - tt_begin) / 324;
114   param_get_resolution (&dp_resolution);
115
116   memset (&handler_config, 0, sizeof (handler_config));
117   handler_config.beautify = 0;
118   handler_config.indentString = "  ";
119
120   handler = yajl_gen_alloc2 (write_callback,
121       &handler_config,
122       /* alloc functions = */ NULL,
123       /* context = */ NULL);
124   if (handler == NULL)
125     return (-1);
126
127   printf ("Content-Type: application/json\n");
128
129   /* By default, permit caching until 1/1000th after the last data. If that
130    * data is in the past, assume the entire data is in the past and allow
131    * caching for one day. */
132   expires = tt_end + ((tt_end - tt_begin) / 1000);
133   if (expires < tt_now)
134     expires = tt_now + 86400;
135
136   status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
137   if (status == 0)
138     printf ("Expires: %s\n"
139         "Cache-Control: public\n",
140         time_buffer);
141   printf ("\n");
142
143   status = inst_data_to_json (inst,
144       dp_begin, dp_end, dp_resolution, handler);
145
146   yajl_gen_free (handler);
147
148   return (status);
149 } /* }}} int action_instance_data_json */
150
151 /* vim: set sw=2 sts=2 et fdm=marker : */