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