ogn-python/ogn/commands/database.py

112 wiersze
3.1 KiB
Python
Czysty Zwykły widok Historia

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-08 18:24:33 +00:00
from ogn.model import Base, DeviceInfoOrigin, AircraftBeacon, ReceiverBeacon
2017-10-03 11:31:24 +00:00
from ogn.utils import get_airports
2017-12-08 18:24:33 +00:00
from sqlalchemy import distinct
from sqlalchemy.sql import null, func
2016-10-31 12:47:27 +00:00
2017-10-03 11:31:24 +00:00
manager = Manager()
ALEMBIC_CONFIG_FILE = "alembic.ini"
2015-11-15 18:31:58 +00:00
@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;')
session.commit()
Base.metadata.create_all(engine)
alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
2016-01-12 17:35:33 +00:00
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...")
2017-12-02 12:08:44 +00:00
address_origin = DeviceInfoOrigin.ogn_ddb
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
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
print("Import registered devices from '{}'...".format(path))
2017-12-02 12:08:44 +00:00
address_origin = DeviceInfoOrigin.user_defined
counter = update_device_infos(session,
address_origin,
csvfile=path)
print("Imported %i devices." % counter)
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()
print("Imported {} airports.".format(len(airports)))
2016-10-31 12:47:27 +00:00
2017-12-02 15:45:09 +00:00
@manager.command
def update_receivers():
2017-12-08 18:24:33 +00:00
from ogn.collect.database import update_receivers as ur
ur()
2017-12-02 21:49:12 +00:00
2017-12-02 15:45:09 +00:00
2017-12-08 18:24:33 +00:00
@manager.command
def update_receiver_stats():
"""Add/update entries in receiver stats table."""
2017-12-08 20:32:50 +00:00
asdf = session.query(
ReceiverBeacon.receiver_id,
func.count(distinct(AircraftBeacon.device_id)).label('device_count'),
func.max(AircraftBeacon.altitude).label('max_altitude'),
func.max(func.ST_Distance(AircraftBeacon.location_wkt, AircraftBeacon.location_wkt)).label('max_distance')) \
.filter(ReceiverBeacon.receiver_id == AircraftBeacon.receiver_id) \
.group_by(ReceiverBeacon.id)
2017-12-08 18:24:33 +00:00
print(asdf)
for a in asdf.all():
print(a)
2017-12-08 18:24:33 +00:00
return
2017-12-08 18:24:33 +00:00
asdf = session.query(distinct(ReceiverBeacon.receiver_id), func.DATE(ReceiverBeacon.timestamp).label('date')) \
.filter(ReceiverBeacon.receiver_id != null())