view 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
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
monitor SQL table size
"""

import argparse
import csv
import os
import sys
import time
from .cli import ConfigurationParser
from .url2sql import url2sql


def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line
    parser = ConfigurationParser(description=__doc__)
    parser.add_argument('database', type=url2sql,
                        help="URL of SQL database to connect to")
    parser.add_argument('table',
                        help="table to read sizes of")
    parser.add_argument('-o', '--output', dest='output',
                        type=argparse.FileType('a'), default=sys.stdout,
                        help="CSV output file; stdout by default")
    parser.add_argument('-w', '--wait', dest='wait',
                        type=float, default=60.,
                        help="how long to wait between calls in seconds [DEFAULT: %(default)s]")
    options = parser.parse_args(args)

    # ensure table is part of database
    db = options.database
    tables = db.tables()
    if options.table not in tables:
        parser.error("Table '{}' not in database tables: {}".format(options.table,
                                                                    ', '.join(tables)))

    # instantiate writer
    writer = csv.writer(options.output)

    # get initial data
    previous = db.count(options.table)
    end = time.time()
    time.sleep(options.wait)

    while True:
        try:
            start = time.time()
            count = db.count(options.table)
            rate = (count - previous)/(start-end)
            writer.writerow([start, count, rate])
            options.output.flush()
            end = start
            previous = count
            time.sleep(options.wait - (time.time() - start))
        except KeyboardInterrupt:
            break

if __name__ == '__main__':
    main()