ogn-python/app/commands/database.py

129 wiersze
3.8 KiB
Python
Czysty Zwykły widok Historia

2019-09-12 20:53:42 +00:00
from flask import current_app
from flask.cli import AppGroup
import click
2019-01-01 19:13:08 +00:00
2019-09-14 06:27:35 +00:00
from datetime import datetime
2019-01-01 19:13:08 +00:00
from sqlalchemy.sql import func
2017-10-03 11:31:24 +00:00
2019-08-31 08:14:41 +00:00
from app.collect.database import update_device_infos, update_country_code
2019-09-14 06:27:35 +00:00
from app.model import AircraftBeacon, DeviceInfoOrigin
2019-08-31 08:14:41 +00:00
from app.utils import get_airports, get_days
2019-08-31 08:14:41 +00:00
from app import db
2019-08-31 08:14:41 +00:00
user_cli = AppGroup("database")
user_cli.help = "Database creation and handling."
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-08-31 08:14:41 +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-08-31 08:14:41 +00:00
@user_cli.command("info")
2019-02-25 19:00:51 +00:00
def info():
2019-09-12 20:53:42 +00:00
print(current_app.config)
print(current_app.config["SQLALCHEMY_DATABASE_URI"])
2019-02-25 19:00:51 +00:00
2019-08-31 08:14:41 +00:00
@user_cli.command("init")
def init():
"""Initialize the database."""
from alembic.config import Config
from alembic import command
2019-08-31 08:14:41 +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()
print("Done.")
2019-08-31 08:14:41 +00:00
@user_cli.command("init_timescaledb")
def init_timescaledb():
"""Initialize TimescaleDB features."""
2019-08-31 08:14:41 +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()
print("Done.")
2019-08-31 08:14:41 +00:00
@user_cli.command("drop")
@click.option("--sure", default="n")
2019-02-25 19:00:51 +00:00
def drop(sure):
2016-01-12 17:36:08 +00:00
"""Drop all tables."""
2019-08-31 08:14:41 +00:00
if sure == "y":
2019-03-04 21:14:13 +00:00
db.drop_all()
2019-08-31 08:14:41 +00:00
print("Dropped all tables.")
2016-01-12 17:36:08 +00:00
else:
print("Add argument '--sure y' to drop all tables.")
2019-08-31 08:14:41 +00:00
@user_cli.command("import_ddb")
def import_ddb():
"""Import registered devices from the DDB."""
2015-11-24 07:20:28 +00:00
print("Import registered devices fom the DDB...")
2019-09-15 13:39:05 +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-08-31 08:14:41 +00:00
@user_cli.command("import_file")
@click.argument("path")
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))
2019-09-15 13:39:05 +00:00
counter = update_device_infos(db.session, 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
2019-08-31 08:14:41 +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-09-15 13:39:05 +00:00
counter = update_device_infos(db.session, DeviceInfoOrigin.FLARMNET, path=path)
2018-10-21 15:34:03 +00:00
print("Imported %i devices." % counter)
2016-04-22 08:44:39 +00:00
2018-12-08 08:00:44 +00:00
2019-08-31 08:14:41 +00:00
@user_cli.command("import_airports")
@click.argument("path")
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)
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-08-31 08:14:41 +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
update_country_code(session=db.session)