From: oetiker Date: Sat, 28 Jul 2012 13:55:30 +0000 (+0000) Subject: support for the xport command -- Jimmy Ngo X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=58e33c38668afe88b2e45c9aca017b46a92537ca;p=rrdtool.git support for the xport command -- Jimmy Ngo git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@2295 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/bindings/python/rrdtoolmodule.c b/bindings/python/rrdtoolmodule.c index a2647b5..fdf4121 100644 --- a/bindings/python/rrdtoolmodule.c +++ b/bindings/python/rrdtoolmodule.c @@ -587,6 +587,84 @@ static PyObject *PyRRD_flushcached( 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; + + 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} @@ -603,6 +681,7 @@ static PyMethodDef _rrdtool_methods[] = { meth("graphv", PyRRD_graphv, PyRRD_graphv__doc__), meth("updatev", PyRRD_updatev, PyRRD_updatev__doc__), meth("flushcached", PyRRD_flushcached, PyRRD_flushcached__doc__), + meth("xport", PyRRD_xport, PyRRD_xport__doc__), {NULL, NULL, 0, NULL} }; diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 83f35b6..1b02414 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -36,7 +36,7 @@ library_dir = os.environ.get('BUILDLIBDIR', os.path.join(RRDBASE, '.libs')) include_dir = os.environ.get('INCDIR', RRDBASE) setup(name = "py-rrdtool", - version = "0.2.1", + version = "0.2.2", description = "Python Interface to RRDTool", author = "Hye-Shik Chang", author_email = "perky@fallin.lv",