ogn-python/app/commands/database.py

134 wiersze
4.2 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
from app.model import SenderPosition
2019-08-31 08:14:41 +00:00
from app.utils import get_airports, get_days
2020-10-27 19:46:14 +00:00
from app.collect.timescaledb_views import create_timescaledb_views, create_views
from app.collect.database import read_ddb, read_flarmnet, merge_sender_infos
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:
2020-10-27 19:46:14 +00:00
days_from_db = db.session.query(func.min(SenderPosition.timestamp).label("first_day"), func.max(SenderPosition.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():
2020-11-17 13:58:23 +00:00
"""Initialize the database (with PostGIS and TimescaleDB extensions)."""
from alembic.config import Config
from alembic import command
2020-11-17 13:58:23 +00:00
# Create PostGIS and PostGIS extensions
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;")
2020-11-17 13:58:23 +00:00
db.session.execute("CREATE EXTENSION IF NOT EXISTS timescaledb;")
db.session.commit()
2020-11-17 13:58:23 +00:00
# Create Scheme
db.create_all()
2020-11-17 13:58:23 +00:00
# Change (sender|receiver)_positions to TimescaleDB table
2020-10-27 19:46:14 +00:00
db.session.execute("SELECT create_hypertable('sender_positions', 'reference_timestamp', chunk_time_interval => interval '3 hours', if_not_exists => TRUE);")
db.session.execute("SELECT create_hypertable('receiver_positions', 'reference_timestamp', chunk_time_interval => interval '1 day', if_not_exists => TRUE);")
db.session.commit()
2020-11-17 13:58:23 +00:00
print("Initialized the database (with PostGIS and TimescaleDB extensions).")
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")
@click.option('--path', default=None, help='path to a local ddb file.')
def import_ddb(path):
"""Import registered devices from the DDB."""
2015-11-24 07:20:28 +00:00
if path is None:
print("Import registered devices fom the DDB...")
sender_info_dicts = read_ddb()
else:
print("Import registered devices from '{}'...".format(path))
sender_info_dicts = read_ddb(csv_file=path)
counter = merge_sender_infos(sender_info_dicts)
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))
sender_info_dicts = read_flarmnet(path=path)
counter = merge_sender_infos(sender_info_dicts)
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()
2020-10-27 19:46:14 +00:00
# TODO: SRID 4087 ist nicht korrekt, aber spherical mercator 3857 wirft hier Fehler
db.session.execute("UPDATE airports AS a SET border = ST_Transform(ST_Buffer(ST_Transform(location, 4087), 1.5 * GREATEST(500, a.runway_length)), 4326);")
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
2020-11-22 07:55:19 +00:00
2020-10-27 19:46:14 +00:00
@user_cli.command("create_timescaledb_views")
def cmd_create_timescaledb_views():
"""Create TimescaleDB views."""
create_timescaledb_views()
print("Done")
2020-11-22 07:55:19 +00:00
2020-10-27 19:46:14 +00:00
@user_cli.command("create_views")
def cmd_create_views():
"""Create views."""
create_views()
print("Done")