aboutsummaryrefslogtreecommitdiff
path: root/poller
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2021-02-25 22:31:38 -0500
committerGalen Guyer <galen@galenguyer.com>2021-02-25 22:31:38 -0500
commit51079aa249b509e9160dd675fed13b3bacda661c (patch)
tree31437842911749e868d88be1a481426c012805e2 /poller
parent987a1db2a7b5d84aa906c23eb586930df7cd0d50 (diff)
Add import history command
Diffstat (limited to 'poller')
-rw-r--r--poller/__init__.py3
-rw-r--r--poller/commands.py36
2 files changed, 39 insertions, 0 deletions
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()