2017-10-03 11:31:24 +00:00
|
|
|
from manager import Manager
|
|
|
|
from ogn.collect.database import update_device_infos
|
2015-12-01 22:04:13 +00:00
|
|
|
from ogn.commands.dbutils import engine, session
|
2017-12-02 12:08:44 +00:00
|
|
|
from ogn.model import Base, DeviceInfoOrigin, AircraftBeacon, ReceiverBeacon, Device, Receiver
|
2017-10-03 11:31:24 +00:00
|
|
|
from ogn.utils import get_airports
|
2016-10-31 12:47:27 +00:00
|
|
|
from sqlalchemy import insert, distinct
|
2017-12-02 15:45:09 +00:00
|
|
|
from sqlalchemy.sql import null, and_, or_, func, not_
|
2016-10-31 12:47:27 +00:00
|
|
|
|
2017-10-03 11:31:24 +00:00
|
|
|
|
2015-11-15 08:10:46 +00:00
|
|
|
manager = Manager()
|
|
|
|
|
2016-01-31 01:25:21 +00:00
|
|
|
ALEMBIC_CONFIG_FILE = "alembic.ini"
|
|
|
|
|
2015-11-15 18:31:58 +00:00
|
|
|
|
2015-11-15 08:10:46 +00:00
|
|
|
@manager.command
|
|
|
|
def init():
|
|
|
|
"""Initialize the database."""
|
2015-12-09 02:37:25 +00:00
|
|
|
|
2016-01-29 01:38:55 +00:00
|
|
|
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;')
|
|
|
|
session.commit()
|
2015-11-15 08:10:46 +00:00
|
|
|
Base.metadata.create_all(engine)
|
2016-01-31 01:25:21 +00:00
|
|
|
alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
|
2016-01-12 17:35:33 +00:00
|
|
|
command.stamp(alembic_cfg, "head")
|
2015-11-15 08:10:46 +00:00
|
|
|
print("Done.")
|
2015-11-15 08:23:57 +00:00
|
|
|
|
|
|
|
|
2016-01-31 01:25:21 +00:00
|
|
|
@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.")
|
|
|
|
|
|
|
|
|
2015-11-15 08:23:57 +00:00
|
|
|
@manager.command
|
2015-12-09 02:41:58 +00:00
|
|
|
def import_ddb():
|
|
|
|
"""Import registered devices from the DDB."""
|
2015-11-24 07:20:28 +00:00
|
|
|
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Import registered devices fom the DDB...")
|
2017-12-02 12:08:44 +00:00
|
|
|
address_origin = DeviceInfoOrigin.ogn_ddb
|
2016-07-17 20:57:54 +00:00
|
|
|
counter = update_device_infos(session,
|
|
|
|
address_origin)
|
2015-11-15 11:10:20 +00:00
|
|
|
print("Imported %i devices." % counter)
|
2015-12-01 22:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
2015-12-09 02:41:58 +00:00
|
|
|
def import_file(path='tests/custom_ddb.txt'):
|
|
|
|
"""Import registered devices from a local file."""
|
|
|
|
# (flushes previously manually imported entries)
|
2015-12-01 22:04:13 +00:00
|
|
|
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Import registered devices from '{}'...".format(path))
|
2017-12-02 12:08:44 +00:00
|
|
|
address_origin = DeviceInfoOrigin.user_defined
|
2016-07-17 20:57:54 +00:00
|
|
|
counter = update_device_infos(session,
|
|
|
|
address_origin,
|
|
|
|
csvfile=path)
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Imported %i devices." % counter)
|
2016-04-22 08:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
2016-05-22 05:27:21 +00:00
|
|
|
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()
|
|
|
|
print("Imported {} airports.".format(len(airports)))
|
2016-10-31 12:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
2017-12-02 21:49:12 +00:00
|
|
|
def update_devices():
|
|
|
|
"""Add/update entries in devices table and update foreign keys in aircraft beacons."""
|
2016-10-31 12:47:27 +00:00
|
|
|
|
|
|
|
# Create missing Device from AircraftBeacon
|
2017-12-02 21:49:12 +00:00
|
|
|
available_devices = session.query(Device.address) \
|
2016-10-31 12:47:27 +00:00
|
|
|
.subquery()
|
|
|
|
|
2017-12-02 21:49:12 +00:00
|
|
|
missing_devices_query = session.query(distinct(AircraftBeacon.address)) \
|
2016-10-31 12:47:27 +00:00
|
|
|
.filter(AircraftBeacon.device_id == null()) \
|
2017-12-02 21:49:12 +00:00
|
|
|
.filter(~AircraftBeacon.address.in_(available_devices))
|
2016-10-31 12:47:27 +00:00
|
|
|
|
2017-12-02 21:49:12 +00:00
|
|
|
ins = insert(Device).from_select([Device.address], missing_devices_query)
|
|
|
|
res = session.execute(ins)
|
|
|
|
insert_count = res.rowcount
|
2016-10-31 12:47:27 +00:00
|
|
|
|
2017-12-02 21:49:12 +00:00
|
|
|
# Update relations to aircraft beacons
|
2016-10-31 12:47:27 +00:00
|
|
|
upd = session.query(AircraftBeacon) \
|
|
|
|
.filter(AircraftBeacon.device_id == null()) \
|
|
|
|
.filter(AircraftBeacon.address == Device.address) \
|
2017-12-02 21:49:12 +00:00
|
|
|
.update({AircraftBeacon.device_id: Device.id},
|
2016-10-31 12:47:27 +00:00
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
session.commit()
|
2017-12-02 21:49:12 +00:00
|
|
|
print("Inserted {} Devices".format(insert_count))
|
|
|
|
print("Updated {} AircraftBeacons".format(upd))
|
2017-12-02 15:45:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
|
|
|
def update_receivers():
|
|
|
|
"""Add/update entries in receiver table and update foreign keys in aircraft beacons and receiver beacons."""
|
|
|
|
# Create missing Receiver from ReceiverBeacon
|
|
|
|
available_receivers = session.query(Receiver.name) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
missing_receiver_query = session.query(distinct(ReceiverBeacon.name)) \
|
|
|
|
.filter(ReceiverBeacon.receiver_id == null()) \
|
|
|
|
.filter(~ReceiverBeacon.name.in_(available_receivers))
|
|
|
|
|
|
|
|
ins = insert(Receiver).from_select([Receiver.name], missing_receiver_query)
|
|
|
|
res = session.execute(ins)
|
|
|
|
insert_count = res.rowcount
|
|
|
|
|
|
|
|
# Update missing or changed location, update it and set country code to None
|
|
|
|
last_beacon_update = session.query(ReceiverBeacon.name, func.max(ReceiverBeacon.timestamp).label("timestamp")) \
|
|
|
|
.filter(ReceiverBeacon.receiver_id == null()) \
|
|
|
|
.group_by(ReceiverBeacon.name) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
last_position = session.query(ReceiverBeacon) \
|
|
|
|
.filter(and_(ReceiverBeacon.name == last_beacon_update.c.name, ReceiverBeacon.timestamp == last_beacon_update.c.timestamp,
|
|
|
|
ReceiverBeacon.location_wkt != null(), ReceiverBeacon.altitude != null())) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
location_changed = session.query(last_position) \
|
|
|
|
.filter(and_(last_position.c.name == Receiver.name)) \
|
|
|
|
.filter(or_(Receiver.location_wkt == null(), not_(func.ST_Equals(Receiver.location_wkt, last_position.columns.location)), last_position.c.altitude != Receiver.altitude)) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
upd = session.query(Receiver) \
|
|
|
|
.filter(Receiver.name == location_changed.c.name) \
|
|
|
|
.update({Receiver.location_wkt: location_changed.c.location,
|
|
|
|
Receiver.altitude: location_changed.c.altitude,
|
|
|
|
Receiver.country_code: None},
|
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
# Update missing or changed status
|
|
|
|
last_status = session.query(ReceiverBeacon) \
|
|
|
|
.filter(and_(ReceiverBeacon.name == last_beacon_update.columns.name, ReceiverBeacon.timestamp == last_beacon_update.c.timestamp,
|
|
|
|
ReceiverBeacon.version != null(), ReceiverBeacon.platform != null())) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
status_changed = session.query(last_status) \
|
|
|
|
.filter(and_(last_status.columns.name == Receiver.name)) \
|
|
|
|
.filter(or_(Receiver.version == null(), Receiver.platform == null(), Receiver.version != last_status.columns.version)) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
upd2 = session.query(Receiver) \
|
|
|
|
.filter(Receiver.name == status_changed.columns.name) \
|
|
|
|
.update({Receiver.version: status_changed.c.version,
|
|
|
|
Receiver.platform: status_changed.c.platform},
|
|
|
|
synchronize_session='fetch')
|
2017-12-02 21:49:12 +00:00
|
|
|
|
2017-12-02 15:45:09 +00:00
|
|
|
# Update relations to aircraft beacons
|
|
|
|
upd3 = session.query(AircraftBeacon) \
|
|
|
|
.filter(and_(AircraftBeacon.receiver_id == null(), AircraftBeacon.receiver_name == Receiver.name)) \
|
|
|
|
.update({AircraftBeacon.receiver_id: Receiver.id},
|
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
# Update relations to receiver beacons
|
|
|
|
upd4 = session.query(ReceiverBeacon) \
|
|
|
|
.filter(and_(ReceiverBeacon.receiver_id == null(), ReceiverBeacon.name == Receiver.name)) \
|
|
|
|
.update({ReceiverBeacon.receiver_id: Receiver.id},
|
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
print("Inserted {} Receivers".format(insert_count))
|
|
|
|
print("Updated Receivers: {} positions, {} status".format(upd, upd2))
|
|
|
|
print("Updated Relations: {} aircraft beacons, {} receiver beacons".format(upd3, upd4))
|
|
|
|
return
|