2 * Substantial penalty for early withdrawal.
9 typedef struct string_arr_t {
22 #define RRD_CHECK_ERROR \
23 if (rrd_test_error()) \
24 rb_raise(rb_eRRDError, rrd_get_error()); \
27 string_arr string_arr_new(
34 Check_Type(rb_strings, T_ARRAY);
35 a.len = RARRAY(rb_strings)->len + 1;
37 a.strings = malloc(a.len * sizeof(char *));
38 a.strings[0] = "dummy"; /* first element is a dummy element */
40 for (i = 0; i < a.len - 1; i++) {
41 VALUE v = rb_ary_entry(rb_strings, i);
45 a.strings[i + 1] = strdup(STR2CSTR(v));
48 snprintf(buf, 63, "%d", FIX2INT(v));
49 a.strings[i + 1] = strdup(buf);
52 rb_raise(rb_eTypeError, "invalid argument - %s, expected T_STRING or T_FIXNUM on index %d", rb_class2name(CLASS_OF(v)), i);
60 void string_arr_delete(
65 /* skip dummy first entry */
66 for (i = 1; i < a.len; i++) {
87 a = string_arr_new(args);
89 func(a.len, a.strings);
92 RRD_CHECK_ERROR return Qnil;
99 return rrd_call(rrd_create, args);
106 return rrd_call(rrd_dump, args);
114 unsigned long i, j, k, step, ds_cnt;
115 rrd_value_t *raw_data;
117 VALUE data, names, result;
120 a = string_arr_new(args);
122 rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
124 string_arr_delete(a);
126 RRD_CHECK_ERROR names = rb_ary_new();
128 for (i = 0; i < ds_cnt; i++) {
129 rb_ary_push(names, rb_str_new2(raw_names[i]));
136 for (i = start; i <= end; i += step) {
137 VALUE line = rb_ary_new2(ds_cnt);
139 for (j = 0; j < ds_cnt; j++) {
140 rb_ary_store(line, j, rb_float_new(raw_data[k]));
143 rb_ary_push(data, line);
147 result = rb_ary_new2(4);
148 rb_ary_store(result, 0, INT2FIX(start));
149 rb_ary_store(result, 1, INT2FIX(end));
150 rb_ary_store(result, 2, names);
151 rb_ary_store(result, 2, data);
161 VALUE result, print_results;
165 a = string_arr_new(args);
167 rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
168 string_arr_delete(a);
170 RRD_CHECK_ERROR result = rb_ary_new2(3);
172 print_results = rb_ary_new();
174 for (p = calcpr; p && *p; p++) {
175 rb_ary_push(print_results, rb_str_new2(*p));
179 rb_ary_store(result, 0, print_results);
180 rb_ary_store(result, 1, INT2FIX(xsize));
181 rb_ary_store(result, 2, INT2FIX(ysize));
186 VALUE rb_rrd_info(VALUE self, VALUE args)
192 a = string_arr_new(args);
193 data = rrd_info(a.len, a.strings);
194 string_arr_delete(a);
198 result = rb_hash_new();
200 VALUE key = rb_str_new2(data->key);
201 switch (data->type) {
203 if (isnan(data->u_val)) {
204 rb_hash_aset(result, key, Qnil);
207 rb_hash_aset(result, key, rb_float_new(data->u_val));
211 rb_hash_aset(result, key, INT2FIX(data->u_cnt));
214 rb_hash_aset(result, key, rb_str_new2(data->u_str));
233 a = string_arr_new(args);
235 last = rrd_last(a.len, a.strings);
236 string_arr_delete(a);
239 return rb_funcall(rb_cTime, rb_intern("at"), 1, INT2FIX(last));
246 return rrd_call(rrd_resize, args);
249 VALUE rb_rrd_restore(
253 return rrd_call(rrd_restore, args);
260 return rrd_call(rrd_tune, args);
267 return rrd_call(rrd_update, args);
273 mRRD = rb_define_module("RRD");
274 rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
276 rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
277 rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
278 rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
279 rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
280 rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
281 rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
282 rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
283 rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
284 rb_define_module_function(mRRD, "update", rb_rrd_update, -2);