ogn-python/ogn/commands/database.py

121 wiersze
3.7 KiB
Python
Czysty Zwykły widok Historia

2019-01-01 19:13:08 +00:00
from datetime import datetime, timedelta
2017-10-03 11:31:24 +00:00
from manager import Manager
2018-10-21 15:34:03 +00:00
from ogn.collect.database import update_device_infos, update_country_code
2015-12-01 22:04:13 +00:00
from ogn.commands.dbutils import engine, session
2019-01-01 19:13:08 +00:00
from ogn.model import Base, DeviceInfoOrigin, AircraftBeacon
from ogn.utils import get_airports, get_days
from sqlalchemy.sql import func
2017-10-03 11:31:24 +00:00
manager = Manager()
ALEMBIC_CONFIG_FILE = "alembic.ini"
2015-11-15 18:31:58 +00:00
2019-01-01 19:13:08 +00:00
def get_database_days(start, end):
"""Returns the first and the last day in aircraft_beacons table."""
if start is None and end is None:
days_from_db = session.query(func.min(AircraftBeacon.timestamp).label('first_day'), func.max(AircraftBeacon.timestamp).label('last_day')).one()
start = days_from_db[0].date()
end = days_from_db[1].date()
else:
start = datetime.strptime(start, "%Y-%m-%d")
end = datetime.strptime(end, "%Y-%m-%d")
days = get_days(start, end)
return days
@manager.command
def init():
"""Initialize the database."""
from alembic.config import Config
from alembic import command
2016-06-05 06:33:13 +00:00
session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
2018-10-21 15:34:03 +00:00
session.execute('CREATE EXTENSION IF NOT EXISTS btree_gist;')
2018-11-28 06:37:35 +00:00
session.execute('CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;')
2016-06-05 06:33:13 +00:00
session.commit()
Base.metadata.create_all(engine)
2019-01-01 19:13:08 +00:00
session.execute("SELECT create_hypertable('aircraft_beacons', 'timestamp', chunk_target_size => '2GB', if_not_exists => TRUE);")
session.execute("SELECT create_hypertable('receiver_beacons', 'timestamp', chunk_target_size => '2GB', if_not_exists => TRUE);")
2018-11-28 06:37:35 +00:00
session.commit()
2018-09-03 17:58:35 +00:00
#alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
#command.stamp(alembic_cfg, "head")
print("Done.")
@manager.command
def upgrade():
"""Upgrade database to the latest version."""
from alembic.config import Config
from alembic import command
alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
command.upgrade(alembic_cfg, 'head')
2016-01-12 17:36:08 +00:00
@manager.command
def drop(sure='n'):
"""Drop all tables."""
if sure == 'y':
Base.metadata.drop_all(engine)
print('Dropped all tables.')
else:
print("Add argument '--sure y' to drop all tables.")
@manager.command
def import_ddb():
"""Import registered devices from the DDB."""
2015-11-24 07:20:28 +00:00
print("Import registered devices fom the DDB...")
2018-10-21 15:34:03 +00:00
counter = update_device_infos(session, DeviceInfoOrigin.ogn_ddb)
2015-11-15 11:10:20 +00:00
print("Imported %i devices." % counter)
2015-12-01 22:04:13 +00:00
@manager.command
def import_file(path='tests/custom_ddb.txt'):
"""Import registered devices from a local file."""
2015-12-01 22:04:13 +00:00
print("Import registered devices from '{}'...".format(path))
counter = update_device_infos(session,
2018-10-21 15:34:03 +00:00
DeviceInfoOrigin.user_defined,
path=path)
print("Imported %i devices." % counter)
2016-04-22 08:44:39 +00:00
2018-12-08 08:00:44 +00:00
2018-10-21 15:34:03 +00:00
@manager.command
2018-12-08 08:00:44 +00:00
def import_flarmnet(path=None):
2018-10-21 15:34:03 +00:00
"""Import registered devices from a local file."""
print("Import registered devices from '{}'...".format("internet" if path is None else path))
counter = update_device_infos(session,
DeviceInfoOrigin.flarmnet,
path=path)
print("Imported %i devices." % counter)
2016-04-22 08:44:39 +00:00
2018-12-08 08:00:44 +00:00
2016-04-22 08:44:39 +00:00
@manager.command
def import_airports(path='tests/SeeYou.cup'):
2016-04-22 08:44:39 +00:00
"""Import airports from a ".cup" file"""
print("Import airports from '{}'...".format(path))
airports = get_airports(path)
session.bulk_save_objects(airports)
session.commit()
2019-01-01 19:13:08 +00:00
session.execute("UPDATE airports SET border = ST_Expand(location, 0.05)")
session.commit()
2016-04-22 08:44:39 +00:00
print("Imported {} airports.".format(len(airports)))
2018-10-21 15:34:03 +00:00
2018-12-08 08:00:44 +00:00
2018-10-21 15:34:03 +00:00
@manager.command
def update_country_codes():
"""Update country codes of all receivers."""
2019-01-01 19:13:08 +00:00
update_country_code(session=session)