]> git.decadent.org.uk Git - dak.git/blobdiff - dak/stats.py
Don't repr the object; could be a long (and ends up with eg. "2L")
[dak.git] / dak / stats.py
index 53c8d2a96f8222c726bbb87df9205ffd7b8803cd..52534c5128be8f2634747d0cd8ec40b3e63bc99d 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Various statistical pr0nography fun and games
+""" Various statistical pr0nography fun and games """
 # Copyright (C) 2000, 2001, 2002, 2003, 2006  James Troup <james@nocrew.org>
 
 # This program is free software; you can redistribute it and/or modify
 
 ################################################################################
 
-import pg, sys
+import sys
 import apt_pkg
-import daklib.utils
+
+from daklib import utils
+from daklib.dbconn import DBConn, get_suite_architectures, Suite, Architecture, \
+                          BinAssociation
 
 ################################################################################
 
 Cnf = None
-projectB = None
 
 ################################################################################
 
@@ -58,22 +60,25 @@ The following MODEs are available:
 ################################################################################
 
 def per_arch_space_use():
-    q = projectB.query("""
-SELECT a.arch_string as Architecture, sum(f.size)
+    session = DBConn().session()
+    q = session.execute("""
+SELECT a.arch_string as Architecture, sum(f.size) AS sum
   FROM files f, binaries b, architecture a
   WHERE a.id=b.architecture AND f.id=b.file
-  GROUP BY a.arch_string""")
-    print q
-    q = projectB.query("SELECT sum(size) FROM files WHERE filename ~ '.(diff.gz|tar.gz|dsc)$'")
-    print q
+  GROUP BY a.arch_string ORDER BY sum""").fetchall()
+    for j in q:
+        print "%-15.15s %s" % (j[0], j[1])
+    print
+    q = session.execute("SELECT sum(size) FROM files WHERE filename ~ '.(diff.gz|tar.gz|dsc)$'").fetchall()
+    print "%-15.15s %s" % ("Source", q[0][0])
 
 ################################################################################
 
 def daily_install_stats():
     stats = {}
-    file = daklib.utils.open_file("2001-11")
-    for line in file.readlines():
-        split = line.strip().split('~')
+    f = utils.open_file("2001-11")
+    for line in f.readlines():
+        split = line.strip().split('|')
         program = split[1]
         if program != "katie" and program != "process-accepted":
             continue
@@ -136,38 +141,30 @@ def number_of_packages():
     suites = {}
     suite_ids = {}
     d = {}
+    session = DBConn().session()
     # Build up suite mapping
-    q = projectB.query("SELECT id, suite_name FROM suite")
-    suite_ql = q.getresult()
-    for i in suite_ql:
-        (id, name) = i
-        suites[id] = name
-        suite_ids[name] = id
+    for i in session.query(Suite).all():
+        suites[i.suite_id] = i.suite_name
+        suite_ids[i.suite_name] = i.suite_id
     # Build up architecture mapping
-    q = projectB.query("SELECT id, arch_string FROM architecture")
-    for i in q.getresult():
-        (id, name) = i
-        arches[id] = name
-        arch_ids[name] = id
+    for i in session.query(Architecture).all():
+        arches[i.arch_id] = i.arch_string
+        arch_ids[i.arch_string] = i.arch_id
     # Pre-create the dictionary
     for suite_id in suites.keys():
         d[suite_id] = {}
         for arch_id in arches.keys():
             d[suite_id][arch_id] = 0
     # Get the raw data for binaries
-    q = projectB.query("""
-SELECT ba.suite, b.architecture
-  FROM binaries b, bin_associations ba
- WHERE b.id = ba.bin""")
     # Simultate 'GROUP by suite, architecture' with a dictionary
-    for i in q.getresult():
-        (suite_id, arch_id) = i
+    # XXX: Why don't we just get the DB to do this?
+    for i in session.query(BinAssociation):
+        suite_id = i.suite_id
+        arch_id = i.binary.arch_id
         d[suite_id][arch_id] = d[suite_id][arch_id] + 1
     # Get the raw data for source
     arch_id = arch_ids["source"]
-    q = projectB.query("""
-SELECT suite, count(suite) FROM src_associations GROUP BY suite;""")
-    for i in q.getresult():
+    for i in session.execute('SELECT suite, COUNT(suite) FROM src_associations GROUP BY suite').all():
         (suite_id, count) = i
         d[suite_id][arch_id] = d[suite_id][arch_id] + count
     ## Print the results
@@ -179,10 +176,10 @@ SELECT suite, count(suite) FROM src_associations GROUP BY suite;""")
     for suite in suite_list:
         suite_id = suite_ids[suite]
         suite_arches[suite_id] = {}
-        for arch in Cnf.ValueList("Suite::%s::Architectures" % (suite)):
-            suite_arches[suite_id][arch] = ""
+        for arch in get_suite_architectures(suite):
+            suite_arches[suite_id][arch.arch_string] = ""
         suite_id_list.append(suite_id)
-    output_list = map(lambda x: output_format(x), suite_list)
+    output_list = [ output_format(i) for i in suite_list ]
     longest_suite = longest(output_list)
     arch_list = arches.values()
     arch_list.sort()
@@ -201,7 +198,7 @@ SELECT suite, count(suite) FROM src_associations GROUP BY suite;""")
         output = output + arch.center(longest_arch)+" |"
         for suite_id in suite_id_list:
             if suite_arches[suite_id].has_key(arch):
-                count = repr(d[suite_id][arch_id])
+                count = "%d" % d[suite_id][arch_id]
             else:
                 count = "-"
             output = output + count.rjust(longest_suite)+" |"
@@ -211,30 +208,28 @@ SELECT suite, count(suite) FROM src_associations GROUP BY suite;""")
 ################################################################################
 
 def main ():
-    global Cnf, projectB
+    global Cnf
 
-    Cnf = daklib.utils.get_conf()
+    Cnf = utils.get_conf()
     Arguments = [('h',"help","Stats::Options::Help")]
     for i in [ "help" ]:
-       if not Cnf.has_key("Stats::Options::%s" % (i)):
-           Cnf["Stats::Options::%s" % (i)] = ""
+        if not Cnf.has_key("Stats::Options::%s" % (i)):
+            Cnf["Stats::Options::%s" % (i)] = ""
 
     args = apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
 
     Options = Cnf.SubTree("Stats::Options")
     if Options["Help"]:
-       usage()
+        usage()
 
     if len(args) < 1:
-        daklib.utils.warn("dak stats requires a MODE argument")
+        utils.warn("dak stats requires a MODE argument")
         usage(1)
     elif len(args) > 1:
-        daklib.utils.warn("dak stats accepts only one MODE argument")
+        utils.warn("dak stats accepts only one MODE argument")
         usage(1)
     mode = args[0].lower()
 
-    projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
-
     if mode == "arch-space":
         per_arch_space_use()
     elif mode == "pkg-nums":
@@ -242,11 +237,10 @@ def main ():
     elif mode == "daily-install":
         daily_install_stats()
     else:
-        daklib.utils.warn("unknown mode '%s'" % (mode))
+        utils.warn("unknown mode '%s'" % (mode))
         usage(1)
 
 ################################################################################
 
 if __name__ == '__main__':
     main()
-