Mercurial > hg > Lemuriformes
annotate lemuriformes/table_size.py @ 18:56596902e9ae default tip
add some setup + tests
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:57:03 -0800 |
parents | 4793f99b73e0 |
children |
rev | line source |
---|---|
17
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 """ |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 monitor SQL table size |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 """ |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 import argparse |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 import csv |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 import os |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 import sys |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 import time |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 from .cli import ConfigurationParser |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 from .url2sql import url2sql |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 def main(args=sys.argv[1:]): |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 """CLI""" |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 # parse command line |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 parser = ConfigurationParser(description=__doc__) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 parser.add_argument('database', type=url2sql, |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 help="URL of SQL database to connect to") |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 parser.add_argument('table', |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 help="table to read sizes of") |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 parser.add_argument('-o', '--output', dest='output', |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 type=argparse.FileType('a'), default=sys.stdout, |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 help="CSV output file; stdout by default") |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 parser.add_argument('-w', '--wait', dest='wait', |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 type=float, default=60., |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 help="how long to wait between calls in seconds [DEFAULT: %(default)s]") |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 options = parser.parse_args(args) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 # ensure table is part of database |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 db = options.database |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 tables = db.tables() |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 if options.table not in tables: |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
38 parser.error("Table '{}' not in database tables: {}".format(options.table, |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
39 ', '.join(tables))) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
40 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
41 # instantiate writer |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 writer = csv.writer(options.output) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 # get initial data |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 previous = db.count(options.table) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 end = time.time() |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 time.sleep(options.wait) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 while True: |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 try: |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 start = time.time() |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 count = db.count(options.table) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
53 rate = (count - previous)/(start-end) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
54 writer.writerow([start, count, rate]) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 options.output.flush() |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
56 end = start |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
57 previous = count |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
58 time.sleep(options.wait - (time.time() - start)) |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
59 except KeyboardInterrupt: |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
60 break |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
61 |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 if __name__ == '__main__': |
4793f99b73e0
[lemuriformes] utility functions
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
63 main() |