Skip to content

Commit

Permalink
Allow configuring how many hours back to collect with HOURS env var
Browse files Browse the repository at this point in the history
  • Loading branch information
grosskur committed Apr 19, 2013
1 parent 81c83cc commit bfd644f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 10 additions & 1 deletion ec2price/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'/(?P<dbname>.+)'
)
DATABASE_URL_EXAMPLE = 'postgres://username:password@host:port/dbname'
_HOURS = 8


class ArgumentParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -119,6 +120,7 @@ def main(args):
_start_tornado_app(debug, cookie_secret, port, address, handlers)
elif opts.cmd == 'collector':
database_url = os.getenv('DATABASE_URL')
hours = os.getenv('HOURS')

database_dsn = None
if database_url:
Expand All @@ -131,9 +133,16 @@ def main(args):
if not database_url:
parser.error('DATABASE_URL is required')

if not hours:
hours = _HOURS
try:
hours = int(hours)
except ValueError:
parser.error('HOURS must be an integer')

db_conn = _get_db_conn(database_dsn)

collect(db_conn)
collect(db_conn, hours)
return 0


Expand Down
5 changes: 2 additions & 3 deletions ec2price/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import uuid


_HOURS = 8
_FMT = '%Y-%m-%dT%H:%M:%S.000Z'

_SELECT_SPOT_PRICE = """
Expand Down Expand Up @@ -39,12 +38,12 @@
logging.getLogger('requests.packages.urllib3').setLevel(logging.WARN)


def collect(db_conn):
def collect(db_conn, hours):
session = botocore.session.get_session()
ec2 = session.get_service('ec2')
operation = ec2.get_operation('DescribeSpotPriceHistory')

d = datetime.datetime.utcnow() - datetime.timedelta(hours=_HOURS)
d = datetime.datetime.utcnow() - datetime.timedelta(hours=hours)
start_time = d.strftime(_FMT)

with contextlib.closing(db_conn.cursor()) as cursor:
Expand Down

0 comments on commit bfd644f

Please sign in to comment.