ogn-python/ogn/commands/database.py

128 wiersze
3.8 KiB
Python

from datetime import datetime, timedelta
from manager import Manager
from ogn.collect.database import update_device_infos, update_country_code
from ogn.commands.dbutils import engine, session
from ogn.model import Base, DeviceInfoOrigin, AircraftBeacon
from ogn.utils import get_airports, get_days
from sqlalchemy.sql import func
manager = Manager()
ALEMBIC_CONFIG_FILE = "alembic.ini"
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").date()
end = datetime.strptime(end, "%Y-%m-%d").date()
days = get_days(start, end)
return days
@manager.command
def init():
"""Initialize the database."""
from alembic.config import Config
from alembic import command
session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
session.execute('CREATE EXTENSION IF NOT EXISTS btree_gist;')
session.commit()
Base.metadata.create_all(engine)
#alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
#command.stamp(alembic_cfg, "head")
print("Done.")
@manager.command
def init_timescaledb():
"""Initialize TimescaleDB features."""
session.execute('CREATE EXTENSION IF NOT EXISTS timescaledb;')
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);")
session.commit()
@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')
@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."""
print("Import registered devices fom the DDB...")
counter = update_device_infos(session, DeviceInfoOrigin.ogn_ddb)
print("Imported %i devices." % counter)
@manager.command
def import_file(path='tests/custom_ddb.txt'):
"""Import registered devices from a local file."""
print("Import registered devices from '{}'...".format(path))
counter = update_device_infos(session,
DeviceInfoOrigin.user_defined,
path=path)
print("Imported %i devices." % counter)
@manager.command
def import_flarmnet(path=None):
"""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)
@manager.command
def import_airports(path='tests/SeeYou.cup'):
"""Import airports from a ".cup" file"""
print("Import airports from '{}'...".format(path))
airports = get_airports(path)
session.bulk_save_objects(airports)
session.commit()
session.execute("UPDATE airports SET border = ST_Expand(location, 0.05)")
session.commit()
print("Imported {} airports.".format(len(airports)))
@manager.command
def update_country_codes():
"""Update country codes of all receivers."""
update_country_code(session=session)