]> git.decadent.org.uk Git - dak.git/blob - dak/graph.py
fixup code
[dak.git] / dak / graph.py
1 #!/usr/bin/env python
2
3 """ Produces a set of graphs of NEW/BYHAND/DEFERRED
4
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2011 Paul Wise <pabs@debian.org>
7 """
8
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 import os
24 import sys
25
26 import rrdtool
27 import apt_pkg
28
29 from daklib import utils
30 from daklib.dak_exceptions import *
31
32 Cnf = None
33 default_names = ["byhand", "new", "deferred"]
34
35 ################################################################################
36
37 def usage(exit_code=0):
38     print """Usage: dak graph
39 Graphs the number of packages in queue directories (usually new and byhand).
40
41   -h, --help                show this help and exit.
42   -r, --rrd=key             Directory where rrd files to be updated are stored
43   -x, --extra-rrd=key       File containing extra options to rrdtool graphing
44   -i, --images=key          Directory where image graphs to be updated are stored
45   -n, --names=key           A comma seperated list of rrd files to be scanned
46
47 """
48     sys.exit(exit_code)
49
50 ################################################################################
51
52 def graph(rrd_dir, image_dir, name, extra_args, graph, title, start, year_lines=False):
53     image_file = os.path.join(image_dir, "%s-%s.png" % (name, graph))
54     rrd_file = os.path.join(rrd_dir, "%s.rrd" % name)
55     rrd_args = [image_file, "--start", start]
56     rrd_args += ("""
57 --end
58 now
59 --width
60 600
61 --height
62 150
63 --vertical-label
64 packages
65 --title
66 %s package count for the last %s
67 --lower-limit
68 0
69 -E
70 """ % (name.upper(), title) ).strip().split("\n")
71
72     if year_lines:
73         rrd_args += ["--x-grid", "MONTH:1:YEAR:1:YEAR:1:31536000:%Y"]
74
75     rrd_args += ("""
76 DEF:ds1=%s:ds1:AVERAGE
77 LINE2:ds1#D9382B:changes count
78 VDEF:lds1=ds1,LAST
79 VDEF:minds1=ds1,MINIMUM
80 VDEF:maxds1=ds1,MAXIMUM
81 VDEF:avgds1=ds1,AVERAGE
82 GPRINT:lds1:cur\\: %%.0lf
83 GPRINT:minds1:min\\: %%.0lf
84 GPRINT:maxds1:max\\: %%.0lf
85 GPRINT:avgds1:avg\\: %%.0lf\\j
86 DEF:ds0=%s:ds0:AVERAGE
87 VDEF:lds0=ds0,LAST
88 VDEF:minds0=ds0,MINIMUM
89 VDEF:maxds0=ds0,MAXIMUM
90 VDEF:avgds0=ds0,AVERAGE
91 LINE2:ds0#3069DA:src pkg count
92 GPRINT:lds0:cur\\: %%.0lf
93 GPRINT:minds0:min\\: %%.0lf
94 GPRINT:maxds0:max\\: %%.0lf
95 GPRINT:avgds0:avg\\: %%.0lf\\j
96 """ % (rrd_file, rrd_file)).strip().split("\n")
97
98     rrd_args += extra_args
99     try:
100         ret = rrdtool.graph(*rrd_args)
101     except rrdtool.error, e:
102         print('warning: graph: rrdtool error, skipping %s-%s.png: %s' % (name, graph, e))
103
104 ################################################################################
105
106 def main():
107     global Cnf
108
109     Cnf = utils.get_conf()
110     Arguments = [('h',"help","Graph::Options::Help"),
111                  ('x',"extra-rrd","Graph::Options::Extra-Rrd", "HasArg"),
112                  ('r',"rrd","Graph::Options::Rrd", "HasArg"),
113                  ('i',"images","Graph::Options::Images", "HasArg"),
114                  ('n',"names","Graph::Options::Names", "HasArg")]
115     for i in [ "help" ]:
116         if not Cnf.has_key("Graph::Options::%s" % (i)):
117             Cnf["Graph::Options::%s" % (i)] = ""
118
119     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
120
121     Options = Cnf.SubTree("Graph::Options")
122     if Options["Help"]:
123         usage()
124
125     names = []
126
127     if Cnf.has_key("Graph::Options::Names"):
128         for i in Cnf["Graph::Options::Names"].split(","):
129             names.append(i)
130     elif Cnf.has_key("Graph::Names"):
131         names = Cnf.ValueList("Graph::Names")
132     else:
133         names = default_names
134
135     extra_rrdtool_args = []
136
137     if Cnf.has_key("Graph::Options::Extra-Rrd"):
138         for i in Cnf["Graph::Options::Extra-Rrd"].split(","):
139             f = open(i)
140             extra_rrdtool_args.extend(f.read().strip().split("\n"))
141             f.close()
142     elif Cnf.has_key("Graph::Extra-Rrd"):
143         for i in Cnf.ValueList("Graph::Extra-Rrd"):
144             f = open(i)
145             extra_rrdtool_args.extend(f.read().strip().split("\n"))
146             f.close()
147
148     if Cnf.has_key("Graph::Options::Rrd"):
149         rrd_dir = Cnf["Graph::Options::Rrd"]
150     elif Cnf.has_key("Dir::Rrd"):
151         rrd_dir = Cnf["Dir::Rrd"]
152     else:
153         print >> sys.stderr, "No directory to read RRD files from\n"
154         sys.exit(1)
155
156     if Cnf.has_key("Graph::Options::Images"):
157         image_dir = Cnf["Graph::Options::Images"]
158     else:
159         print >> sys.stderr, "No directory to write graph images to\n"
160         sys.exit(1)
161
162     for name in names:
163         stdargs = [rrd_dir, image_dir, name, extra_rrdtool_args]
164         graph(*(stdargs+['day', 'day', 'now-1d']))
165         graph(*(stdargs+['week', 'week', 'now-1w']))
166         graph(*(stdargs+['month', 'month', 'now-1m']))
167         graph(*(stdargs+['year', 'year', 'now-1y']))
168         graph(*(stdargs+['5years', '5 years', 'now-5y', True]))
169         graph(*(stdargs+['10years', '10 years', 'now-10y', True]))
170
171 ################################################################################
172
173 if __name__ == '__main__':
174     main()