From 51079aa249b509e9160dd675fed13b3bacda661c Mon Sep 17 00:00:00 2001 From: Galen Guyer Date: Thu, 25 Feb 2021 22:31:38 -0500 Subject: Add import history command --- .gitignore | 2 +- config.env.py | 2 +- poller/__init__.py | 3 +++ poller/commands.py | 36 ++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 poller/commands.py diff --git a/.gitignore b/.gitignore index 48d472c..e1dec52 100644 --- a/.gitignore +++ b/.gitignore @@ -166,5 +166,5 @@ dmypy.json *.code-workspace # End of https://www.toptal.com/developers/gitignore/api/python,vscode,linux - config.py +data.db diff --git a/config.env.py b/config.env.py index 5241003..a56a930 100644 --- a/config.env.py +++ b/config.env.py @@ -15,5 +15,5 @@ LOG_LEVEL = environ.get('PACKET_LOG_LEVEL', 'INFO') VERSION = '0.1.0' # SQLAlchemy config -SQLALCHEMY_DATABASE_URI = environ.get('POLLER_DATABASE_URI', 'sqlite:////tmp/rit-covid-poller.db') +SQLALCHEMY_DATABASE_URI = environ.get('POLLER_DATABASE_URI', f'sqlite:///data.db') SQLALCHEMY_TRACK_MODIFICATIONS = False diff --git a/poller/__init__.py b/poller/__init__.py index e6eda4d..c0c25c0 100644 --- a/poller/__init__.py +++ b/poller/__init__.py @@ -29,3 +29,6 @@ APP.logger.info('SQLAlchemy pointed at ' + repr(db.engine.url)) #pylint: disable=wrong-import-position from . import models +from . import commands + +db.create_all() diff --git a/poller/commands.py b/poller/commands.py new file mode 100644 index 0000000..fc45dc7 --- /dev/null +++ b/poller/commands.py @@ -0,0 +1,36 @@ +""" +CLI commands for data management +""" +import json +import click +from dateutil import parser + +from . import APP, db +from .models import Day + +@APP.cli.command('import-history') +@click.argument('history_file') +def import_history(history_file): + """ + Import history from JSON file + """ + data = '{}' + with open(history_file, 'r') as file: + data = file.read() + parsed = json.loads(data) + for item in parsed: + if not parser.parse(item['last_updated']) in [day.last_updated for day in Day.get_all()]: + db.session.add(Day( + last_updated=parser.parse(item['last_updated']), + alert_level=item['alert_level'], + beds_available=item['beds_available'], + isolation_off_campus=item['isolation_off_campus'], + isolation_on_campus=item['isolation_on_campus'], + new_staff=item['new_staff'], + new_students=item['new_students'], + quarantine_off_campus=item['quarantine_off_campus'], + quarantine_on_campus=item['quarantine_on_campus'], + tests_administered=item['tests_administered'], + total_staff=item['total_staff'], + total_students=item['total_students'])) + db.session.commit() diff --git a/requirements.txt b/requirements.txt index ea4aff3..16b231a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,7 +15,9 @@ MarkupSafe==1.1.1 mccabe==0.6.1 pylint==2.6.0 pylint-quotes==0.2.1 +python-dateutil==2.8.1 requests==2.24.0 +six==1.15.0 soupsieve==2.2 SQLAlchemy==1.3.23 toml==0.10.2 -- cgit v1.2.3