Skip to content

Commit

Permalink
Fixes #43: Fix bounds fetching, add some safety checks for empty results
Browse files Browse the repository at this point in the history
Contributed by @Ikariusrb in #45
  • Loading branch information
ajsquared committed Mar 24, 2016
1 parent c63c1ea commit b1a4a84
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions visualization/graphite_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def get_bounds(host, prefix):
url = 'http://%s/metrics/expand' % host
json_url = requests.get(url, params=params)
json_results = json_url.json()
bounds = [int(bound.replace(prefix + '.', '')) for bound in json_results['results']]
bounds = []
for bound in json_results['results']:
boundresult = bound.replace(prefix + '.', '')
if boundresult.isdigit():
bounds.append(int(boundresult))

return (min(bounds), max(bounds))

Expand All @@ -40,7 +44,10 @@ def get_max_metric(host, metric, start, end):
url = 'http://%s/render' % host
json_url = requests.get(url, params=params)
json_results = json_url.json()
return max([point[0] for point in json_results[0]['datapoints']])
if not json_results:
return None
else:
return max([point[0] for point in json_results[0]['datapoints']])


def get_tree(host, prefix, start, end):
Expand All @@ -50,7 +57,9 @@ def get_tree(host, prefix, start, end):

results = {}
for leaf in leaves:
results[leaf] = get_max_metric(host, leaf, start, end)
leafmetric = get_max_metric(host, leaf, start, end)
if leafmetric is not None:
results[leaf] = leafmetric

return results

Expand All @@ -64,7 +73,8 @@ def format_metric(metric, prefix):

def format_output(prefix, results):
for metric, value in results.iteritems():
print '%s %d' % (format_metric(metric, prefix), value)
if value is not None:
print '%s %d' % (format_metric(metric, prefix), value)


if __name__ == '__main__':
Expand Down

0 comments on commit b1a4a84

Please sign in to comment.