modoboa / modoboa-stats

Graphical statistics for Modoboa
MIT License
9 stars 13 forks source link

export to JSON directly in graphics.py #72

Closed renarios closed 2 years ago

renarios commented 2 years ago

In file graphics.py you export to XML and after that you convert to JSON.

I see the comment:

"""Export data to XML using rrdtool and convert it to JSON."""

and you issue the code to export:

       cmd = "{} xport --start {} --end {} ".format(
            self.rrdtool_binary, str(start), str(end))
        cmd += " ".join(cmdargs)
        code, output = exec_cmd(smart_bytes(cmd))
        if code:
            return []

after that you convert to JSON

        for row in tree.xpath('/xport/data/row'):
            for vindex, value in enumerate(row.findall('v')):
                if value.text == 'NaN':
                    result[vindex]['data'].append({'x': timestamp, 'y': 0})
                else:
                    result[vindex]['data'].append(
                        {'x': timestamp, 'y': float(value.text)}
                    )
            timestamp += step
        return result`

According to the man page you can export in JSON-format directly by using the option '--json': rrdtool xport --json

That's saves code and resources.

The command to directly export to JSON would be:

       cmd = "{} xport --json --start {} --end {} ".format(
            self.rrdtool_binary, str(start), str(end))
        cmd += " ".join(cmdargs)
        code, output = exec_cmd(smart_bytes(cmd))
        if code:
            return []
tonioo commented 2 years ago

@renarios What version of Modoboa are you using? This module is now deprecated since it has been merged to the core application. I should archive this repository...

renarios commented 2 years ago

I use it as part of the core application version 20.0. Excuse me for placing the issue in the wrong repository.