2019-02-10 17:39:06 +00:00
|
|
|
from flask.cli import AppGroup
|
|
|
|
import click
|
2019-01-01 19:13:08 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
from datetime import datetime, timedelta
|
2019-01-01 19:13:08 +00:00
|
|
|
from sqlalchemy.sql import func
|
2017-10-03 11:31:24 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
from ogn_python.collect.database import update_device_infos, update_country_code
|
|
|
|
from ogn_python.model import *
|
|
|
|
from ogn_python.utils import get_airports, get_days
|
2019-03-11 22:26:01 +00:00
|
|
|
|
|
|
|
from ogn_python import app
|
2019-02-10 17:39:06 +00:00
|
|
|
from ogn_python import db
|
|
|
|
|
|
|
|
user_cli = AppGroup('database')
|
|
|
|
user_cli.help = "Database creation and handling."
|
|
|
|
|
2015-11-15 08:10:46 +00:00
|
|
|
|
2016-01-31 01:25:21 +00:00
|
|
|
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:
|
2019-02-10 17:39:06 +00:00
|
|
|
days_from_db = db.session.query(func.min(AircraftBeacon.timestamp).label('first_day'), func.max(AircraftBeacon.timestamp).label('last_day')).one()
|
2019-01-01 19:13:08 +00:00
|
|
|
start = days_from_db[0].date()
|
|
|
|
end = days_from_db[1].date()
|
|
|
|
else:
|
2019-01-05 10:10:10 +00:00
|
|
|
start = datetime.strptime(start, "%Y-%m-%d").date()
|
|
|
|
end = datetime.strptime(end, "%Y-%m-%d").date()
|
2019-01-01 19:13:08 +00:00
|
|
|
|
|
|
|
days = get_days(start, end)
|
|
|
|
|
|
|
|
return days
|
|
|
|
|
|
|
|
|
2019-02-25 19:00:51 +00:00
|
|
|
@user_cli.command('info')
|
|
|
|
def info():
|
2019-03-11 22:26:01 +00:00
|
|
|
print(app.config)
|
|
|
|
print(app.config['SQLALCHEMY_DATABASE_URI'])
|
2019-02-25 19:00:51 +00:00
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('init')
|
2015-11-15 08:10:46 +00:00
|
|
|
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
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
db.session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
|
|
|
|
db.session.execute('CREATE EXTENSION IF NOT EXISTS btree_gist;')
|
|
|
|
db.session.commit()
|
2019-03-04 21:14:13 +00:00
|
|
|
db.create_all()
|
2019-01-04 14:06:11 +00:00
|
|
|
|
2018-09-03 17:58:35 +00:00
|
|
|
#alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
|
|
|
|
#command.stamp(alembic_cfg, "head")
|
2015-11-15 08:10:46 +00:00
|
|
|
print("Done.")
|
2015-11-15 08:23:57 +00:00
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('init_timescaledb')
|
2019-01-04 14:06:11 +00:00
|
|
|
def init_timescaledb():
|
|
|
|
"""Initialize TimescaleDB features."""
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
db.session.execute('CREATE EXTENSION IF NOT EXISTS timescaledb;')
|
|
|
|
db.session.execute("SELECT create_hypertable('aircraft_beacons', 'timestamp', chunk_target_size => '2GB', if_not_exists => TRUE);")
|
|
|
|
db.session.execute("SELECT create_hypertable('receiver_beacons', 'timestamp', chunk_target_size => '2GB', if_not_exists => TRUE);")
|
|
|
|
db.session.commit()
|
2019-01-04 14:06:11 +00:00
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('upgrade')
|
2016-01-31 01:25:21 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('drop')
|
2019-02-25 19:00:51 +00:00
|
|
|
@click.option('--sure', default='n')
|
|
|
|
def drop(sure):
|
2016-01-12 17:36:08 +00:00
|
|
|
"""Drop all tables."""
|
|
|
|
if sure == 'y':
|
2019-03-04 21:14:13 +00:00
|
|
|
db.drop_all()
|
2016-01-12 17:36:08 +00:00
|
|
|
print('Dropped all tables.')
|
|
|
|
else:
|
|
|
|
print("Add argument '--sure y' to drop all tables.")
|
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('import_ddb')
|
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...")
|
2019-02-10 17:39:06 +00:00
|
|
|
counter = update_device_infos(db.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
|
|
|
|
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('import_file')
|
|
|
|
@click.argument('path')
|
2015-12-09 02:41:58 +00:00
|
|
|
def import_file(path='tests/custom_ddb.txt'):
|
|
|
|
"""Import registered devices from a local file."""
|
2015-12-01 22:04:13 +00:00
|
|
|
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Import registered devices from '{}'...".format(path))
|
2019-02-10 17:39:06 +00:00
|
|
|
counter = update_device_infos(db.session,
|
2018-10-21 15:34:03 +00:00
|
|
|
DeviceInfoOrigin.user_defined,
|
|
|
|
path=path)
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Imported %i devices." % counter)
|
2016-04-22 08:44:39 +00:00
|
|
|
|
2018-12-08 08:00:44 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('import_flarmnet')
|
|
|
|
@click.argument('path')
|
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))
|
2019-02-10 17:39:06 +00:00
|
|
|
counter = update_device_infos(db.session,
|
2018-10-21 15:34:03 +00:00
|
|
|
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
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('import_airports')
|
|
|
|
@click.argument('path')
|
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)
|
2019-02-10 17:39:06 +00:00
|
|
|
db.session.bulk_save_objects(airports)
|
|
|
|
db.session.commit()
|
|
|
|
db.session.execute("UPDATE airports SET border = ST_Expand(location, 0.05)")
|
|
|
|
db.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
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
@user_cli.command('update_country_codes')
|
2018-10-21 15:34:03 +00:00
|
|
|
def update_country_codes():
|
|
|
|
"""Update country codes of all receivers."""
|
2019-01-01 19:13:08 +00:00
|
|
|
|
2019-02-10 17:39:06 +00:00
|
|
|
update_country_code(session=db.session)
|