comparison lemuriformes/table_size.py @ 17:4793f99b73e0

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