]> git.decadent.org.uk Git - dak.git/blob - dak/stats.py
convert stats to sqla
[dak.git] / dak / stats.py
1 #!/usr/bin/env python
2
3 """ Various statistical pr0nography fun and games """
4 # Copyright (C) 2000, 2001, 2002, 2003, 2006  James Troup <james@nocrew.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 ################################################################################
21
22 # <aj>    can we change the standards instead?
23 # <neuro> standards?
24 # <aj>    whatever we're not conforming to
25 # <aj>    if there's no written standard, why don't we declare linux as
26 #         the defacto standard
27 # <aj>    go us!
28
29 # [aj's attempt to avoid ABI changes for released architecture(s)]
30
31 ################################################################################
32
33 import pg, sys
34 import apt_pkg
35 from daklib import utils
36 from daklib.dbconn import DBConn, get_suite_architectures, Suite, Architecture, \
37                           BinAssociation
38
39 ################################################################################
40
41 Cnf = None
42
43 ################################################################################
44
45 def usage(exit_code=0):
46     print """Usage: dak stats MODE
47 Print various stats.
48
49   -h, --help                show this help and exit.
50
51 The following MODEs are available:
52
53   arch-space    - displays space used by each architecture
54   pkg-nums      - displays the number of packages by suite/architecture
55   daily-install - displays daily install stats suitable for graphing
56 """
57     sys.exit(exit_code)
58
59 ################################################################################
60
61 def per_arch_space_use():
62     session = DBConn().session()
63     q = session.execute("""
64 SELECT a.arch_string as Architecture, sum(f.size) AS sum
65   FROM files f, binaries b, architecture a
66   WHERE a.id=b.architecture AND f.id=b.file
67   GROUP BY a.arch_string ORDER BY sum""").fetchall()
68     for j in q:
69         print "%-15.15s %s" % (j[0], j[1])
70     print
71     q = session.execute("SELECT sum(size) FROM files WHERE filename ~ '.(diff.gz|tar.gz|dsc)$'").fetchall()
72     print "%-15.15s %s" % ("Source", q[0][0])
73
74 ################################################################################
75
76 def daily_install_stats():
77     stats = {}
78     f = utils.open_file("2001-11")
79     for line in f.readlines():
80         split = line.strip().split('|')
81         program = split[1]
82         if program != "katie" and program != "process-accepted":
83             continue
84         action = split[2]
85         if action != "installing changes" and action != "installed":
86             continue
87         date = split[0][:8]
88         if not stats.has_key(date):
89             stats[date] = {}
90             stats[date]["packages"] = 0
91             stats[date]["size"] = 0.0
92         if action == "installing changes":
93             stats[date]["packages"] += 1
94         elif action == "installed":
95             stats[date]["size"] += float(split[5])
96
97     dates = stats.keys()
98     dates.sort()
99     for date in dates:
100         packages = stats[date]["packages"]
101         size = int(stats[date]["size"] / 1024.0 / 1024.0)
102         print "%s %s %s" % (date, packages, size)
103
104 ################################################################################
105
106 def longest(list):
107     longest = 0
108     for i in list:
109         l = len(i)
110         if l > longest:
111             longest = l
112     return longest
113
114 def suite_sort(a, b):
115     if Cnf.has_key("Suite::%s::Priority" % (a)):
116         a_priority = int(Cnf["Suite::%s::Priority" % (a)])
117     else:
118         a_priority = 0
119     if Cnf.has_key("Suite::%s::Priority" % (b)):
120         b_priority = int(Cnf["Suite::%s::Priority" % (b)])
121     else:
122         b_priority = 0
123     return cmp(a_priority, b_priority)
124
125 def output_format(suite):
126     output_suite = []
127     for word in suite.split("-"):
128         output_suite.append(word[0])
129     return "-".join(output_suite)
130
131 # Obvious query with GROUP BY and mapped names                  -> 50 seconds
132 # GROUP BY but ids instead of suite/architecture names          -> 28 seconds
133 # Simple query                                                  -> 14 seconds
134 # Simple query into large dictionary + processing               -> 21 seconds
135 # Simple query into large pre-created dictionary + processing   -> 18 seconds
136
137 def number_of_packages():
138     arches = {}
139     arch_ids = {}
140     suites = {}
141     suite_ids = {}
142     d = {}
143     session = DBConn().session()
144     # Build up suite mapping
145     for i in session.query(Suite).all():
146         suites[i.suite_id] = i.suite_name
147         suite_ids[i.suite_name] = i.suite_id
148     # Build up architecture mapping
149     for i in session.query(Architecture).all():
150         arches[i.arch_id] = i.arch_string
151         arch_ids[i.arch_string] = i.arch_id
152     # Pre-create the dictionary
153     for suite_id in suites.keys():
154         d[suite_id] = {}
155         for arch_id in arches.keys():
156             d[suite_id][arch_id] = 0
157     # Get the raw data for binaries
158     # Simultate 'GROUP by suite, architecture' with a dictionary
159     # XXX: Why don't we just get the DB to do this?
160     for i in session.query(BinAssociation):
161         suite_id = i.suite_id
162         arch_id = i.binary.arch_id
163         d[suite_id][arch_id] = d[suite_id][arch_id] + 1
164     # Get the raw data for source
165     arch_id = arch_ids["source"]
166     for i in session.execute('SELECT suite, COUNT(suite) FROM src_associations GROUP BY suite').all():
167         (suite_id, count) = i
168         d[suite_id][arch_id] = d[suite_id][arch_id] + count
169     ## Print the results
170     # Setup
171     suite_list = suites.values()
172     suite_list.sort(suite_sort)
173     suite_id_list = []
174     suite_arches = {}
175     for suite in suite_list:
176         suite_id = suite_ids[suite]
177         suite_arches[suite_id] = {}
178         for arch in get_suite_architectures(suite):
179             suite_arches[suite_id][arch.arch_string] = ""
180         suite_id_list.append(suite_id)
181     output_list = [ output_format(i) for i in suite_list ]
182     longest_suite = longest(output_list)
183     arch_list = arches.values()
184     arch_list.sort()
185     longest_arch = longest(arch_list)
186     # Header
187     output = (" "*longest_arch) + " |"
188     for suite in output_list:
189         output = output + suite.center(longest_suite)+" |"
190     output = output + "\n"+(len(output)*"-")+"\n"
191     # per-arch data
192     arch_list = arches.values()
193     arch_list.sort()
194     longest_arch = longest(arch_list)
195     for arch in arch_list:
196         arch_id = arch_ids[arch]
197         output = output + arch.center(longest_arch)+" |"
198         for suite_id in suite_id_list:
199             if suite_arches[suite_id].has_key(arch):
200                 count = repr(d[suite_id][arch_id])
201             else:
202                 count = "-"
203             output = output + count.rjust(longest_suite)+" |"
204         output = output + "\n"
205     print output
206
207 ################################################################################
208
209 def main ():
210     global Cnf
211
212     Cnf = utils.get_conf()
213     Arguments = [('h',"help","Stats::Options::Help")]
214     for i in [ "help" ]:
215         if not Cnf.has_key("Stats::Options::%s" % (i)):
216             Cnf["Stats::Options::%s" % (i)] = ""
217
218     args = apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
219
220     Options = Cnf.SubTree("Stats::Options")
221     if Options["Help"]:
222         usage()
223
224     if len(args) < 1:
225         utils.warn("dak stats requires a MODE argument")
226         usage(1)
227     elif len(args) > 1:
228         utils.warn("dak stats accepts only one MODE argument")
229         usage(1)
230     mode = args[0].lower()
231
232     if mode == "arch-space":
233         per_arch_space_use()
234     elif mode == "pkg-nums":
235         number_of_packages()
236     elif mode == "daily-install":
237         daily_install_stats()
238     else:
239         utils.warn("unknown mode '%s'" % (mode))
240         usage(1)
241
242 ################################################################################
243
244 if __name__ == '__main__':
245     main()