2015-11-15 08:10:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-01 19:13:08 +00:00
|
|
|
from datetime import datetime
|
2015-11-15 08:10:46 +00:00
|
|
|
|
2017-10-03 11:31:24 +00:00
|
|
|
from manager import Manager
|
2017-12-10 16:30:27 +00:00
|
|
|
from ogn.collect.logbook import update_logbook
|
2018-01-19 18:14:57 +00:00
|
|
|
from ogn.collect.takeoff_landings import update_takeoff_landings
|
2017-10-03 11:31:24 +00:00
|
|
|
from ogn.commands.dbutils import session
|
2019-01-01 19:13:08 +00:00
|
|
|
from ogn.model import Airport, Logbook
|
2019-01-10 07:36:51 +00:00
|
|
|
from sqlalchemy import or_, between
|
2017-10-03 11:31:24 +00:00
|
|
|
from sqlalchemy.sql import func
|
2019-01-01 19:13:08 +00:00
|
|
|
from tqdm import tqdm
|
|
|
|
from ogn.commands.database import get_database_days
|
2019-01-10 07:36:51 +00:00
|
|
|
from ogn.utils import date_to_timestamps
|
2015-11-15 08:10:46 +00:00
|
|
|
|
|
|
|
manager = Manager()
|
|
|
|
|
2015-11-15 18:31:58 +00:00
|
|
|
|
2015-11-24 20:00:31 +00:00
|
|
|
@manager.command
|
2019-01-01 19:13:08 +00:00
|
|
|
def compute_takeoff_landing(start=None, end=None):
|
2015-11-24 20:00:31 +00:00
|
|
|
"""Compute takeoffs and landings."""
|
2019-01-01 19:13:08 +00:00
|
|
|
|
|
|
|
days = get_database_days(start, end)
|
|
|
|
|
|
|
|
pbar = tqdm(days)
|
|
|
|
for single_date in pbar:
|
|
|
|
pbar.set_description(datetime.strftime(single_date, '%Y-%m-%d'))
|
2018-10-21 15:34:03 +00:00
|
|
|
result = update_takeoff_landings(session=session, date=single_date)
|
2019-01-01 19:13:08 +00:00
|
|
|
|
|
|
|
|
2016-06-29 21:26:30 +00:00
|
|
|
@manager.command
|
2019-01-01 19:13:08 +00:00
|
|
|
def compute_logbook(start=None, end=None):
|
2016-06-29 21:26:30 +00:00
|
|
|
"""Compute logbook."""
|
2019-01-01 19:13:08 +00:00
|
|
|
|
|
|
|
days = get_database_days(start, end)
|
|
|
|
|
|
|
|
pbar = tqdm(days)
|
|
|
|
for single_date in pbar:
|
|
|
|
pbar.set_description(datetime.strftime(single_date, '%Y-%m-%d'))
|
|
|
|
result = update_logbook(session=session, date=single_date)
|
2016-06-29 21:26:30 +00:00
|
|
|
|
|
|
|
|
2016-06-21 18:24:15 +00:00
|
|
|
@manager.arg('date', help='date (format: yyyy-mm-dd)')
|
2015-11-15 08:10:46 +00:00
|
|
|
@manager.command
|
2018-10-21 15:34:03 +00:00
|
|
|
def show(airport_name, date=None):
|
2016-04-22 08:44:39 +00:00
|
|
|
"""Show a logbook for <airport_name>."""
|
|
|
|
airport = session.query(Airport) \
|
2016-04-24 17:34:25 +00:00
|
|
|
.filter(Airport.name == airport_name) \
|
2016-04-22 08:44:39 +00:00
|
|
|
.first()
|
|
|
|
|
|
|
|
if (airport is None):
|
|
|
|
print('Airport "{}" not found.'.format(airport_name))
|
|
|
|
return
|
|
|
|
|
2016-06-01 17:41:32 +00:00
|
|
|
or_args = []
|
|
|
|
if date is not None:
|
|
|
|
date = datetime.strptime(date, "%Y-%m-%d")
|
2019-01-10 07:36:51 +00:00
|
|
|
(start, end) = date_to_timestamps(date)
|
|
|
|
or_args = [between(Logbook.reftime, start, end)]
|
2016-06-21 18:24:15 +00:00
|
|
|
|
2016-06-29 21:26:30 +00:00
|
|
|
# get all logbook entries and add device and airport infos
|
2017-12-13 09:45:43 +00:00
|
|
|
logbook_query = session.query(func.row_number().over(order_by=Logbook.reftime).label('row_number'),
|
2018-10-21 15:34:03 +00:00
|
|
|
Logbook) \
|
|
|
|
.filter(*or_args) \
|
2016-06-29 21:26:30 +00:00
|
|
|
.filter(or_(Logbook.takeoff_airport_id == airport.id,
|
|
|
|
Logbook.landing_airport_id == airport.id)) \
|
|
|
|
.order_by(Logbook.reftime)
|
2015-11-15 08:10:46 +00:00
|
|
|
|
2016-06-21 18:24:15 +00:00
|
|
|
# ... and finally print out the logbook
|
2016-02-03 22:22:04 +00:00
|
|
|
print('--- Logbook ({}) ---'.format(airport_name))
|
2015-11-15 18:31:58 +00:00
|
|
|
|
2016-01-12 21:22:07 +00:00
|
|
|
def none_datetime_replacer(datetime_object):
|
2016-01-12 21:33:37 +00:00
|
|
|
return '--:--:--' if datetime_object is None else datetime_object.time()
|
2016-01-12 21:22:07 +00:00
|
|
|
|
|
|
|
def none_track_replacer(track_object):
|
2016-01-12 21:33:37 +00:00
|
|
|
return '--' if track_object is None else round(track_object / 10.0)
|
2016-01-12 21:22:07 +00:00
|
|
|
|
|
|
|
def none_timedelta_replacer(timedelta_object):
|
2016-01-12 21:33:37 +00:00
|
|
|
return '--:--:--' if timedelta_object is None else timedelta_object
|
2016-01-12 21:22:07 +00:00
|
|
|
|
2018-10-21 15:34:03 +00:00
|
|
|
def none_registration_replacer(device_object):
|
|
|
|
return '[' + device_object.address + ']' if len(device_object.infos) == 0 else device_object.infos[0].registration
|
2016-01-12 21:22:07 +00:00
|
|
|
|
2018-10-21 15:34:03 +00:00
|
|
|
def none_aircraft_replacer(device_object):
|
|
|
|
return '(unknown)' if len(device_object.infos) == 0 else device_object.infos[0].aircraft
|
2015-11-15 18:31:58 +00:00
|
|
|
|
2018-10-21 15:34:03 +00:00
|
|
|
def airport_marker(logbook_object):
|
|
|
|
if logbook_object.takeoff_airport is not None and logbook_object.takeoff_airport.name is not airport.name:
|
|
|
|
return ('FROM: {}'.format(logbook_object.takeoff_airport.name))
|
|
|
|
elif logbook_object.landing_airport is not None and logbook_object.landing_airport.name is not airport.name:
|
|
|
|
return ('TO: {}'.format(logbook_object.landing_airport.name))
|
2016-05-31 19:27:24 +00:00
|
|
|
else:
|
|
|
|
return ('')
|
|
|
|
|
2018-10-21 15:34:03 +00:00
|
|
|
def none_altitude_replacer(logbook_object):
|
|
|
|
return "?" if logbook_object.max_altitude is None else "{:5d}m ({:+5d}m)".format(logbook_object.max_altitude, logbook_object.max_altitude - logbook_object.takeoff_airport.altitude)
|
2016-06-29 21:26:30 +00:00
|
|
|
|
2018-10-21 15:34:03 +00:00
|
|
|
for [row_number, logbook] in logbook_query.all():
|
2017-12-13 09:45:43 +00:00
|
|
|
print('%3d. %10s %8s (%2s) %8s (%2s) %8s %15s %8s %17s %20s' % (
|
|
|
|
row_number,
|
2016-06-29 21:26:30 +00:00
|
|
|
logbook.reftime.date(),
|
|
|
|
none_datetime_replacer(logbook.takeoff_timestamp),
|
|
|
|
none_track_replacer(logbook.takeoff_track),
|
|
|
|
none_datetime_replacer(logbook.landing_timestamp),
|
|
|
|
none_track_replacer(logbook.landing_track),
|
|
|
|
none_timedelta_replacer(logbook.duration),
|
2018-10-21 15:34:03 +00:00
|
|
|
none_altitude_replacer(logbook),
|
|
|
|
none_registration_replacer(logbook.device),
|
|
|
|
none_aircraft_replacer(logbook.device),
|
|
|
|
airport_marker(logbook)))
|