Skip to content
Newer
Older
100755 93 lines (66 sloc) 2.31 KB
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 17, 2012
1 #!/usr/bin/env python
2 from optparse import OptionParser
3 import subprocess
4 import sys
5 from tempfile import mkdtemp
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
6
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
7 from matplotlib.pyplot import figure, grid
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
8 from seriesly import Seriesly
9
10
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
11 def get_metric(db, metric):
12 """Query data using metric as key"""
13 # get query response
14 query_params = {'group': 15000, # 15 seconds
15 'ptr': '/{0}'.format(metric),
16 'reducer': 'avg'}
17 response = db.query(query_params)
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
18
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
19 # convert data and generate sorted lists of timestamps and values
20 data = dict((k, v[0]) for k, v in response.iteritems())
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
21
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
22 timestamps = list()
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
23 values = list()
24
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
25 for timestamp, value in sorted(data.iteritems()):
26 timestamps.append(int(timestamp))
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
27 values.append(value)
28
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
29 # Substract first timestamp; conver to seconds
30 timestamps = [(key - timestamps[0]) / 1000 for key in timestamps]
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
31
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
32 return timestamps, values
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
33
34
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
35 def plot_metric(metric, keys, values, outdir):
36 """Plot chart and save it as PNG file"""
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
37 fig = figure()
38 ax = fig.add_subplot(1, 1, 1)
39
40 ax.set_title(metric)
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
41 ax.set_xlabel('Time elapsed (sec)')
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
42
43 grid()
44
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
45 ax.plot(keys, values, '.')
46 fig.savefig('{0}/{1}.png'.format(outdir, metric))
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
47
48
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
49 def parse_args():
50 """Parse CLI arguments"""
51 usage = "usage: %prog database\n\n" +\
52 "Example: %prog ns_db "
53
54 parser = OptionParser(usage)
55 options, args = parser.parse_args()
56
57 if len(args) != 1:
58 parser.print_help()
59 sys.exit()
60
61 return args[0]
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
62
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
63
64 def main():
65 # parse database name from cli arguments
66 db_name = parse_args()
67
68 # initialize seriesly client
69 db = Seriesly()[db_name]
70
71 # get a set of all unique keys
72 all_docs = db.get_all()
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
73 all_keys = set(key for doc in all_docs.itervalues()
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
74 for key in doc.iterkeys())
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
75
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
76 # plot all metrics to PNG images
77 outdir = mkdtemp()
d955a8c @pavel-paulau add new utils: read from ns_server, then export to PNG
pavel-paulau authored Sep 17, 2012
78 for metric in all_keys:
79 print metric
cb9865d @pavel-paulau fix and improve plotter
pavel-paulau authored Sep 18, 2012
80 if '/' not in metric: # views and xdcr stuff
81 keys, values = get_metric(db, metric)
82 plot_metric(metric, keys, values, outdir)
83
84 try:
85 subprocess.call(['convert', '{0}/*'.format(outdir), 'report.pdf'])
86 print "PDF report was successfully generated!"
87 except OSError:
88 print "All images saved to: {0}".format(outdir)
89
90
91 if __name__ == '__main__':
92 main()
Something went wrong with that request. Please try again.