diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2021-02-13 14:28:22 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2021-02-13 15:08:04 +0100 |
commit | b0684cbe2eb5549e706e85141e5f0657b3d35dec (patch) | |
tree | 5a548dd15c7d878a6c8cfcbdedc75a9f12a8e8da | |
parent | 9f8eb57f540d1d68bc1ddac1a931a44d46369e59 (diff) |
Fix the way the graph resolution is determined
In some cases it could end up with a non-available resolution. This
simplifies the code to determine the resolution.
Fixes 1e4488a
-rw-r--r-- | muninplot/data.py | 9 | ||||
-rw-r--r-- | muninplot/wsgi.py | 28 | ||||
-rw-r--r-- | setup.cfg | 1 |
3 files changed, 19 insertions, 19 deletions
diff --git a/muninplot/data.py b/muninplot/data.py index 01f1ccd..68fa724 100644 --- a/muninplot/data.py +++ b/muninplot/data.py @@ -219,8 +219,8 @@ def get_values(group, host, graph, start, end, resolution=300, minmax=True): def get_resolutions(group, host, graph): - """Return a list of resolutions available for the graph.""" - # find the newest file + """Return a list of resolutions, begin, end available for the graph.""" + # find the newest file (we assume all fields have the same resolution) rrdfile = sorted( (os.stat(x).st_mtime, x) for x in ( os.path.join(MUNIN_DBDIR, group, y) @@ -236,8 +236,11 @@ def get_resolutions(group, host, graph): last_update = int(line.split(' = ')[1]) last_update = int(last_update / step) * step elif '.pdp_per_row = ' in line: + # number of steps per data point pdp_per_row = int(line.split(' = ')[1]) resolutions[pdp_per_row * step] = line.split('.')[0] elif '.rows = ' in line: rows[line.split('.')[0]] = int(line.split(' = ')[1]) - return last_update, [(r, rows[i]) for r, i in sorted(resolutions.items())] + return [ + (resolution, last_update - resolution * rows[item], last_update) + for resolution, item in sorted(resolutions.items())] diff --git a/muninplot/wsgi.py b/muninplot/wsgi.py index 6979a2a..f77ba90 100644 --- a/muninplot/wsgi.py +++ b/muninplot/wsgi.py @@ -108,7 +108,7 @@ def _parse_timestamp(timestamp): '%Y-%m-%d') for fmt in formats: try: - return time.mktime(time.strptime(timestamp, fmt)) + return int(time.mktime(time.strptime(timestamp, fmt))) except ValueError: pass raise ValueError('time data %r does not match any known format' % timestamp) @@ -118,29 +118,25 @@ def get_data(environ, start_response): """Return a data series for the graph as CSV.""" path = environ.get('PATH_INFO', '').lstrip('/') _, group, host, graph = path.split('/') - last_update, resolutions = get_resolutions(group, host, graph) + resolutions = get_resolutions(group, host, graph) parameters = cgi.parse_qs(environ.get('QUERY_STRING', '')) # get the time range to fetch the data for end = parameters.get('end') - end = _parse_timestamp(end[0]) if end else last_update + end = _parse_timestamp(end[0]) if end else resolutions[0][-1] start = parameters.get('start') start = _parse_timestamp(start[0]) if start else end - 24 * 60 * 60 * 7 - # calculate the minimum resolution that we want - resolution = min(( - parameters.get('resolution', (end - start) / 5000), - resolutions[-1][0])) - # find the resolution that contains the whole data set - for res, rows in resolutions: - if res >= resolution: - s = max((last_update - res * rows, start)) - e = min((last_update, end)) - if e > s: - resolution = res - break - values = get_values(group, host, graph, start, end, resolution) + # calculate the resolution that we want + resolution = parameters.get('resolution', (end - start) / 5000) + # find the resolution with the start point in the range + resolution = min([ + r_res + for r_res, r_start, r_end in resolutions + if r_res >= resolution and start >= r_start] + + [resolutions[-1][0]]) # return the values as CSV start_response('200 OK', [ ('Content-Type', 'text/csv')]) + values = get_values(group, host, graph, start, end, resolution) if values: keys = (x for x in values[0].keys() if x != 'remove') keys = ['time'] + sorted((k for k in keys if k != 'time'), key=_field_key) @@ -38,6 +38,7 @@ group=root ignore = B902 # catching Exception is fine P103 # we have no external dependencies in setup.cfg + W504 # we put the binary operator on the preceding line max-line-length = 120 [isort] |