This 2-patch series exposes 'flush' methods to the various language
[rrdtool.git] / bindings / ruby / main.c
1 /* $Id$
2  * Substantial penalty for early withdrawal.
3  */
4
5 #include <unistd.h>
6 #include <ruby.h>
7 #include <math.h>
8 #include "../../src/rrd_tool.h"
9
10 typedef struct string_arr_t {
11     int       len;
12     char    **strings;
13 } string_arr;
14
15 VALUE     mRRD;
16 VALUE     rb_eRRDError;
17
18 typedef int (
19     *RRDFUNC) (
20     int argc,
21     char **argv);
22
23 typedef rrd_info_t *(
24     *RRDINFOFUNC) (
25     int argc,
26     char **argv);
27
28 #define RRD_CHECK_ERROR  \
29     if (rrd_test_error()) \
30       rb_raise(rb_eRRDError, rrd_get_error()); \
31     rrd_clear_error();
32
33 string_arr string_arr_new(
34     VALUE rb_strings)
35 {
36     string_arr a;
37     char      buf[64];
38     int       i;
39
40     Check_Type(rb_strings, T_ARRAY);
41     a.len = RARRAY(rb_strings)->len + 1;
42
43     a.strings = malloc(a.len * sizeof(char *));
44     a.strings[0] = "dummy"; /* first element is a dummy element */
45
46     for (i = 0; i < a.len - 1; i++) {
47         VALUE     v = rb_ary_entry(rb_strings, i);
48
49         switch (TYPE(v)) {
50         case T_STRING:
51             a.strings[i + 1] = strdup(STR2CSTR(v));
52             break;
53         case T_FIXNUM:
54             snprintf(buf, 63, "%d", FIX2INT(v));
55             a.strings[i + 1] = strdup(buf);
56             break;
57         default:
58             rb_raise(rb_eTypeError,
59                      "invalid argument - %s, expected T_STRING or T_FIXNUM on index %d",
60                      rb_class2name(CLASS_OF(v)), i);
61             break;
62         }
63     }
64
65     return a;
66 }
67
68 void string_arr_delete(
69     string_arr a)
70 {
71     int       i;
72
73     /* skip dummy first entry */
74     for (i = 1; i < a.len; i++) {
75         free(a.strings[i]);
76     }
77
78     free(a.strings);
79 }
80
81 void reset_rrd_state(
82     )
83 {
84     optind = 0;
85     opterr = 0;
86     rrd_clear_error();
87 }
88
89 /* Simple Calls */
90
91 VALUE rrd_call(
92     RRDFUNC func,
93     VALUE args)
94 {
95     string_arr a;
96
97     a = string_arr_new(args);
98     reset_rrd_state();
99     func(a.len, a.strings);
100     string_arr_delete(a);
101
102     RRD_CHECK_ERROR return Qnil;
103 }
104
105 VALUE rb_rrd_create(
106     VALUE self,
107     VALUE args)
108 {
109     return rrd_call(rrd_create, args);
110 }
111
112 VALUE rb_rrd_dump(
113     VALUE self,
114     VALUE args)
115 {
116     return rrd_call(rrd_dump, args);
117 }
118
119 VALUE rb_rrd_resize(
120     VALUE self,
121     VALUE args)
122 {
123     return rrd_call(rrd_resize, args);
124 }
125
126 VALUE rb_rrd_restore(
127     VALUE self,
128     VALUE args)
129 {
130     return rrd_call(rrd_restore, args);
131 }
132
133 VALUE rb_rrd_tune(
134     VALUE self,
135     VALUE args)
136 {
137     return rrd_call(rrd_tune, args);
138 }
139
140 VALUE rb_rrd_update(
141     VALUE self,
142     VALUE args)
143 {
144     return rrd_call(rrd_update, args);
145 }
146
147 VALUE rb_rrd_flush(
148     VALUE self,
149     VALUE args)
150 {
151     return rrd_call(rrd_cmd_flush, args);
152 }
153
154
155 /* Calls Returning Data via the Info Interface */
156
157 VALUE rb_rrd_infocall(
158     RRDINFOFUNC func,
159     VALUE args)
160 {
161     string_arr a;
162     rrd_info_t *p, *data;
163     VALUE     result;
164
165     a = string_arr_new(args);
166     data = func(a.len, a.strings);
167     string_arr_delete(a);
168
169     RRD_CHECK_ERROR result = rb_hash_new();
170
171     p = data;
172     while (data) {
173         VALUE     key = rb_str_new2(data->key);
174
175         switch (data->type) {
176         case RD_I_VAL:
177             if (isnan(data->value.u_val)) {
178                 rb_hash_aset(result, key, Qnil);
179             } else {
180                 rb_hash_aset(result, key, rb_float_new(data->value.u_val));
181             }
182             break;
183         case RD_I_CNT:
184             rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
185             break;
186         case RD_I_STR:
187             rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
188             break;
189         case RD_I_INT:
190             rb_hash_aset(result, key, INT2FIX(data->value.u_int));
191             break;
192         case RD_I_BLO:
193             rb_hash_aset(result, key,
194                          rb_str_new((char *)data->value.u_blo.ptr,
195                                     data->value.u_blo.size));
196             break;
197         }
198         data = data->next;
199     }
200     rrd_info_free(p);
201     return result;
202 }
203
204 VALUE rb_rrd_info(
205     VALUE self,
206     VALUE args)
207 {
208     return rb_rrd_infocall(rrd_info, args);
209 }
210
211 VALUE rb_rrd_updatev(
212     VALUE self,
213     VALUE args)
214 {
215     return rb_rrd_infocall(rrd_update_v, args);
216 }
217
218 VALUE rb_rrd_graphv(
219     VALUE self,
220     VALUE args)
221 {
222     return rb_rrd_infocall(rrd_graph_v, args);
223 }
224
225
226 /* Other Calls */
227
228 VALUE rb_rrd_fetch(
229     VALUE self,
230     VALUE args)
231 {
232     string_arr a;
233     unsigned long i, j, k, step, ds_cnt;
234     rrd_value_t *raw_data;
235     char    **raw_names;
236     VALUE     data, names, result;
237     time_t    start, end;
238
239     a = string_arr_new(args);
240     reset_rrd_state();
241     rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
242               &raw_data);
243     string_arr_delete(a);
244
245     RRD_CHECK_ERROR names = rb_ary_new();
246
247     for (i = 0; i < ds_cnt; i++) {
248         rb_ary_push(names, rb_str_new2(raw_names[i]));
249         rrd_freemem(raw_names[i]);
250     }
251     rrd_freemem(raw_names);
252
253     k = 0;
254     data = rb_ary_new();
255     for (i = start; i <= end; i += step) {
256         VALUE     line = rb_ary_new2(ds_cnt);
257
258         for (j = 0; j < ds_cnt; j++) {
259             rb_ary_store(line, j, rb_float_new(raw_data[k]));
260             k++;
261         }
262         rb_ary_push(data, line);
263     }
264     rrd_freemem(raw_data);
265
266     result = rb_ary_new2(5);
267     rb_ary_store(result, 0, INT2NUM(start));
268     rb_ary_store(result, 1, INT2NUM(end));
269     rb_ary_store(result, 2, names);
270     rb_ary_store(result, 3, data);
271     rb_ary_store(result, 4, INT2FIX(step));
272     return result;
273 }
274
275 VALUE rb_rrd_graph(
276     VALUE self,
277     VALUE args)
278 {
279     string_arr a;
280     char    **calcpr, **p;
281     VALUE     result, print_results;
282     int       xsize, ysize;
283     double    ymin, ymax;
284
285     a = string_arr_new(args);
286     reset_rrd_state();
287     rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
288     string_arr_delete(a);
289
290     RRD_CHECK_ERROR result = rb_ary_new2(3);
291
292     print_results = rb_ary_new();
293     p = calcpr;
294     for (p = calcpr; p && *p; p++) {
295         rb_ary_push(print_results, rb_str_new2(*p));
296         rrd_freemem(*p);
297     }
298     rrd_freemem(calcpr);
299     rb_ary_store(result, 0, print_results);
300     rb_ary_store(result, 1, INT2FIX(xsize));
301     rb_ary_store(result, 2, INT2FIX(ysize));
302     return result;
303 }
304
305
306 VALUE rb_rrd_last(
307     VALUE self,
308     VALUE args)
309 {
310     string_arr a;
311     time_t    last;
312
313     a = string_arr_new(args);
314     reset_rrd_state();
315     last = rrd_last(a.len, a.strings);
316     string_arr_delete(a);
317
318     RRD_CHECK_ERROR
319         return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last));
320 }
321
322 void Init_RRD(
323     )
324 {
325     mRRD = rb_define_module("RRD");
326     rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
327
328     rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
329     rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
330     rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
331     rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
332     rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
333     rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
334     rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
335     rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
336     rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
337     rb_define_module_function(mRRD, "flush", rb_rrd_flush, -2);
338     rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
339     rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2);
340     rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2);
341 }