fix off by 1 error
[rrdtool.git] / bindings / python / rrdtoolmodule.c
index 6cc22ad..aea40f1 100644 (file)
@@ -37,7 +37,9 @@
 # define UNUSED(x) x
 #endif
 
-static const char *__version__ = "$Revision: 1.14 $";
+
+#include "../../rrd_config.h"
+static const char *__version__ = PACKAGE_VERSION;
 
 #include "Python.h"
 #include "../../src/rrd_tool.h"
@@ -466,6 +468,7 @@ static PyObject *PyDict_FromInfo(
         }
         if (val) {
             PyDict_SetItemString(r, data->key, val);
+            Py_DECREF(val);
         }
         data = data->next;
     }
@@ -490,10 +493,13 @@ static PyObject *PyRRD_info(
     if ((data = rrd_info(argc, argv)) == NULL) {
         PyErr_SetString(ErrorObject, rrd_get_error());
         rrd_clear_error();
-        return NULL;
+        r = NULL;
+    } else {
+        r = PyDict_FromInfo(data);
+        rrd_info_free(data);
     }
-    r = PyDict_FromInfo(data);
-    rrd_info_free(data);
+
+    destroy_args(&argv);
     return r;
 }
 
@@ -515,10 +521,13 @@ static PyObject *PyRRD_graphv(
     if ((data = rrd_graph_v(argc, argv)) == NULL) {
         PyErr_SetString(ErrorObject, rrd_get_error());
         rrd_clear_error();
-        return NULL;
+        r = NULL;
+    } else {
+        r = PyDict_FromInfo(data);
+        rrd_info_free(data);
     }
-    r = PyDict_FromInfo(data);
-    rrd_info_free(data);
+
+    destroy_args(&argv);
     return r;
 }
 
@@ -540,18 +549,21 @@ static PyObject *PyRRD_updatev(
     if ((data = rrd_update_v(argc, argv)) == NULL) {
         PyErr_SetString(ErrorObject, rrd_get_error());
         rrd_clear_error();
-        return NULL;
+        r = NULL;
+    } else {
+        r = PyDict_FromInfo(data);
+        rrd_info_free(data);
     }
-    r = PyDict_FromInfo(data);
-    rrd_info_free(data);
+
+    destroy_args(&argv);
     return r;
 }
 
-static char PyRRD_flush__doc__[] =
+static char PyRRD_flushcached__doc__[] =
   "flush(args..): flush RRD files from memory\n"
   "   flush [--daemon address] file [file ...]";
 
-static PyObject *PyRRD_flush(
+static PyObject *PyRRD_flushcached(
     PyObject UNUSED(*self),
     PyObject * args)
 {
@@ -559,10 +571,10 @@ static PyObject *PyRRD_flush(
     int       argc;
     char    **argv;
 
-    if (create_args("flush", args, &argc, &argv) < 0)
+    if (create_args("flushcached", args, &argc, &argv) < 0)
         return NULL;
 
-    if (rrd_cmd_flush(argc, argv) != 0) {
+    if (rrd_flushcached(argc, argv) != 0) {
         PyErr_SetString(ErrorObject, rrd_get_error());
         rrd_clear_error();
         r = NULL;
@@ -575,6 +587,84 @@ static PyObject *PyRRD_flush(
     return r;
 }
 
+static char PyRRD_xport__doc__[] =
+    "xport(args..): dictionary representation of data stored in RRDs\n"
+    "    [-s|--start seconds] [-e|--end seconds] [-m|--maxrows rows]"
+    "[--step value] [--daemon address] [DEF:vname=rrd:ds-name:CF]"
+    "[CDEF:vname=rpn-expression] [XPORT:vname[:legend]]";
+
+
+static PyObject *PyRRD_xport(
+    PyObject UNUSED(*self),
+    PyObject * args)
+{
+    PyObject *r;
+    int       argc, xsize;
+    char    **argv, **legend_v;
+    time_t    start, end;
+    unsigned long step, col_cnt;
+    rrd_value_t *data, *datai;
+
+    if (create_args("xport", args, &argc, &argv) < 0)
+        return NULL;
+
+    if (rrd_xport(argc, argv, &xsize, &start, &end,
+                  &step, &col_cnt, &legend_v, &data) == -1) {
+        PyErr_SetString(ErrorObject, rrd_get_error());
+        rrd_clear_error();
+        r = NULL;
+    } else {
+        PyObject *meta_dict, *data_list, *legend_list, *t;
+        unsigned long i, j;
+        rrd_value_t dv;
+
+        unsigned long row_cnt = ((end - start) / step) + 1;
+
+        r = PyDict_New();
+        meta_dict = PyDict_New();
+        legend_list = PyList_New(col_cnt);
+        data_list = PyList_New(row_cnt);
+        PyDict_SetItem(r, PyString_FromString("meta"), meta_dict);
+        PyDict_SetItem(r, PyString_FromString("data"), data_list);
+
+        datai = data;
+
+        PyDict_SetItem(meta_dict, PyString_FromString("start"), PyInt_FromLong((long) start));
+        PyDict_SetItem(meta_dict, PyString_FromString("end"), PyInt_FromLong((long) end));
+        PyDict_SetItem(meta_dict, PyString_FromString("step"), PyInt_FromLong((long) step));
+        PyDict_SetItem(meta_dict, PyString_FromString("rows"), PyInt_FromLong((long) row_cnt));
+        PyDict_SetItem(meta_dict, PyString_FromString("columns"), PyInt_FromLong((long) col_cnt));
+        PyDict_SetItem(meta_dict, PyString_FromString("legend"), legend_list);
+
+        for (i = 0; i < col_cnt; i++) {
+            PyList_SET_ITEM(legend_list, i, PyString_FromString(legend_v[i]));
+        }
+
+        for (i = 0; i < row_cnt; i++) {
+            t = PyTuple_New(col_cnt);
+            PyList_SET_ITEM(data_list, i, t);
+
+            for (j = 0; j < col_cnt; j++) {
+                dv = *(datai++);
+                if (isnan(dv)) {
+                    PyTuple_SET_ITEM(t, j, Py_None);
+                    Py_INCREF(Py_None);
+                } else {
+                    PyTuple_SET_ITEM(t, j, PyFloat_FromDouble((double) dv));
+                }
+            }
+        }
+
+        for (i = 0; i < col_cnt; i++) {
+            rrd_freemem(legend_v[i]);
+        }
+        rrd_freemem(legend_v);
+        rrd_freemem(data);
+    }
+    destroy_args(&argv);
+    return r;
+}
+
 /* List of methods defined in the module */
 #define meth(name, func, doc) {name, (PyCFunction)func, METH_VARARGS, doc}
 
@@ -590,7 +680,8 @@ static PyMethodDef _rrdtool_methods[] = {
     meth("info", PyRRD_info, PyRRD_info__doc__),
     meth("graphv", PyRRD_graphv, PyRRD_graphv__doc__),
     meth("updatev", PyRRD_updatev, PyRRD_updatev__doc__),
-    meth("flush", PyRRD_flush, PyRRD_flush__doc__),
+    meth("flushcached", PyRRD_flushcached, PyRRD_flushcached__doc__),
+    meth("xport", PyRRD_xport, PyRRD_xport__doc__),
     {NULL, NULL, 0, NULL}
 };