~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6916.4.1 by Stuart Bishop
Report on Librarian space usage
5
6
"""Report a breakdown of Librarian disk space usage."""
7
8
__metaclass__ = type
9
__all__ = []
10
11
import _pythonpath
12
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
13
from optparse import OptionParser
6916.4.1 by Stuart Bishop
Report on Librarian space usage
14
import sys
15
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
16
from canonical.database.sqlbase import connect, quoteIdentifier, sqlvalues
6916.4.1 by Stuart Bishop
Report on Librarian space usage
17
from canonical.database.postgresql import listReferences
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
18
from canonical.launchpad.scripts import db_options
6916.4.1 by Stuart Bishop
Report on Librarian space usage
19
20
21
def main():
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
22
    parser = OptionParser()
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
23
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
24
    db_options(parser)
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
25
    parser.add_option(
26
        "-f", "--from", dest="from_date", default=None,
27
        metavar="DATE", help="Only count new files since DATE (yyyy/mm/dd)")
28
    parser.add_option(
29
        "-u", "--until", dest="until_date", default=None,
30
        metavar="DATE", help="Only count new files until DATE (yyyy/mm/dd)")
31
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
32
    options, args = parser.parse_args()
33
    if len(args) > 0:
34
        parser.error("Too many command line arguments.")
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
35
36
    # Handle date filters. We use LibraryFileContent.datecreated rather
37
    # than LibraryFileAlias.datecreated as this report is about actual
38
    # disk space usage. A new row in the database linking to a
39
    # previously existing file in the Librarian takes up no new space.
40
    if options.from_date is not None:
41
        from_date = 'AND LFC.datecreated >= %s' % sqlvalues(
42
            options.from_date)
43
    else:
44
        from_date = ''
45
    if options.until_date is not None:
46
        until_date = 'AND LFC.datecreated <= %s' % sqlvalues(
47
            options.until_date)
48
    else:
49
        until_date = ''
50
10054.15.1 by Stuart Bishop
Fix scripts/librarian-report.py and add database connection command line options
51
    con = connect(options.dbuser)
6916.4.1 by Stuart Bishop
Report on Librarian space usage
52
    cur = con.cursor()
53
54
    # Collect direct references to the LibraryFileAlias table.
55
    references = set(
6916.4.2 by Stuart Bishop
Review feedback
56
        (from_table, from_column)
57
        # Note that listReferences is recursive, which we don't
58
        # care about in this simple report. We also ignore the
59
        # irrelevant constraint type update and delete flags.
60
        for from_table, from_column, to_table, to_column, update, delete
6916.4.1 by Stuart Bishop
Report on Librarian space usage
61
            in listReferences(cur, 'libraryfilealias', 'id')
6916.4.2 by Stuart Bishop
Review feedback
62
        if to_table == 'libraryfilealias'
6916.4.1 by Stuart Bishop
Report on Librarian space usage
63
        )
64
65
    totals = set()
6916.4.2 by Stuart Bishop
Review feedback
66
    for referring_table, referring_column in sorted(references):
10054.15.2 by Stuart Bishop
Ignore libraryfiledownloadcount table, as it is ignored by the garbage collector
67
        if referring_table == 'libraryfiledownloadcount':
68
            continue
6916.4.2 by Stuart Bishop
Review feedback
69
        quoted_referring_table = quoteIdentifier(referring_table)
70
        quoted_referring_column = quoteIdentifier(referring_column)
6916.4.1 by Stuart Bishop
Report on Librarian space usage
71
        cur.execute("""
72
            SELECT
73
                COALESCE(SUM(filesize), 0),
7675.395.29 by Stuart Bishop
Update librarian-report.py to cope with bigint filesize
74
                pg_size_pretty(CAST(COALESCE(SUM(filesize), 0) AS bigint)),
6916.4.1 by Stuart Bishop
Report on Librarian space usage
75
                COUNT(*)
6985.1.1 by Stuart Bishop
Fix librarian report SQL
76
            FROM (
77
                SELECT DISTINCT ON (LFC.id) LFC.id, LFC.filesize
78
                FROM LibraryFileContent AS LFC, LibraryFileAlias AS LFA, %s
79
                WHERE LFC.id = LFA.content
80
                    AND LFA.id = %s.%s
81
                    AND (
82
                        LFA.expires IS NULL
83
                        OR LFA.expires > CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
84
                    %s %s
6985.1.1 by Stuart Bishop
Fix librarian report SQL
85
                ORDER BY LFC.id
86
                ) AS Whatever
6916.4.2 by Stuart Bishop
Review feedback
87
            """ % (
88
                quoted_referring_table, quoted_referring_table,
10054.15.5 by Stuart Bishop
Support for date ranges in librarian-report.py
89
                quoted_referring_column, from_date, until_date))
6916.4.1 by Stuart Bishop
Report on Librarian space usage
90
        total_bytes, formatted_size, num_files = cur.fetchone()
6916.4.2 by Stuart Bishop
Review feedback
91
        totals.add((total_bytes, referring_table, formatted_size, num_files))
6916.4.1 by Stuart Bishop
Report on Librarian space usage
92
93
    for total_bytes, tab_name, formatted_size, num_files in sorted(
94
        totals, reverse=True):
95
        print '%-10s %s in %d files' % (formatted_size, tab_name, num_files)
96
97
    return 0
98
99
100
if __name__ == '__main__':
101
    sys.exit(main())