version 0.2: support for snapshots added
[smonitor.git] / monitor / extra / plotting.py
blobc8e2b7915899c3b0ec9219343756a9f80730c8e6
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Server monitoring system
6 # Copyright © 2011 Rodrigo Eduardo Lazo Paz
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 """Data Plotting routines.
25 Requires matplotlib with cairo support built-in."""
28 __author__ = "rlazo.paz@gmail.com (Rodrigo Lazo)"
29 __version__ = 0.2
32 import matplotlib
33 matplotlib.use('cairo.png')
35 from datetime import datetime
36 import matplotlib.pyplot as plot
37 from pylab import epoch2num
38 import os.path
41 IMAGE_DPI = 80
44 # TODO (09/20): Make this functions more generic and clear, right now
45 # they depend on `DataEntry` objects.
46 def new_multigraph(data_dict, title, target_dir=""):
47 """Creates a single image with multiple plots.
49 `data_dict` must be in the form:
50 {'key': `DataEntry`, 'key': `DataEntry`}
52 'key' will be the label of the plot.
54 Arguments:
55 - `data_dict`: Dict, data source.
56 - `title`: String, title of the plot.
57 - `target_dir`: String, path where to write the image file.
59 Returns:
60 String, filename of the generated image.
61 """
62 for name, values in data_dict.iteritems():
63 data = values.get_all()
64 xs = [x.timestamp for x in data] # pylint: disable=C0103
65 ys = [x.value for x in data] # pylint: disable=C0103
66 plot.plot_date(epoch2num(xs),
67 ys, '-', label=name)
68 plot.title(title)
69 plot.xlabel("timestamp")
70 plot.margins(0.1)
71 plot.legend(loc="upper right")
72 plot.grid(True)
73 filename = "%s-%s.png" % (title.replace(' ', '_'),
74 datetime.now().strftime("%Y%m%d_%H%M%S"))
75 target_file = str(os.path.join(target_dir, filename))
76 plot.savefig(target_file, dpi=IMAGE_DPI)
77 plot.clf()
78 return filename, target_file
81 def new_graph(data_lst, title, target_dir):
82 """Creates a single image with multiple plots.
84 `data_lst` must be a list of pairs in the form:
85 [(x, y), (x,y), (x,y)]
87 Arguments:
88 - `data_lst`: Iterable, data source.
89 - `title`: String, title of the plot.
90 - `target_dir`: String, path where to write the image file.
92 Returns:
93 String, filename of the generated image.
94 """
95 xs = [x[0] for x in data_lst] # pylint: disable=C0103
96 ys = [x[1] for x in data_lst] # pylint: disable=C0103
97 plot.plot_date(epoch2num(xs), ys, '-')
98 plot.title(title)
99 plot.xlabel("timestamp")
100 plot.margins(0.1)
101 plot.legend(loc="upper right")
102 plot.grid(True)
103 now_str = datetime.now().strftime("%Y%m%d_%H%M%S")
104 filename = "%s-%s.png" % (title.replace(' ', '_'), now_str)
105 target_file = str(os.path.join(target_dir, filename))
106 print now_str, filename, target_file
107 plot.savefig(target_file, dpi=IMAGE_DPI)
108 plot.clf()
109 return filename, target_file