2017-10-03 11:31:24 +00:00
from sqlalchemy . ext . hybrid import hybrid_property
2020-11-15 17:27:54 +00:00
2019-08-31 08:14:41 +00:00
from app import db
2016-06-29 21:26:30 +00:00
2019-02-10 12:10:19 +00:00
class Logbook ( db . Model ) :
2020-11-15 17:27:54 +00:00
__tablename__ = " logbooks "
2016-06-29 21:26:30 +00:00
2019-02-10 12:25:24 +00:00
id = db . Column ( db . Integer , primary_key = True )
2016-06-29 21:26:30 +00:00
2019-02-10 12:25:24 +00:00
takeoff_timestamp = db . Column ( db . DateTime )
takeoff_track = db . Column ( db . SmallInteger )
landing_timestamp = db . Column ( db . DateTime )
landing_track = db . Column ( db . SmallInteger )
max_altitude = db . Column ( db . Float ( precision = 2 ) )
2016-06-29 21:26:30 +00:00
# Relations
2020-10-27 19:46:14 +00:00
sender_id = db . Column ( db . Integer , db . ForeignKey ( " senders.id " , ondelete = " CASCADE " ) , index = True )
2020-11-20 16:31:03 +00:00
sender = db . relationship ( " Sender " , foreign_keys = [ sender_id ] , backref = db . backref ( " logbook_entries " , order_by = db . case ( { True : takeoff_timestamp , False : landing_timestamp } , takeoff_timestamp != db . null ( ) ) . desc ( ) ) )
2020-10-27 19:46:14 +00:00
2019-08-31 08:14:41 +00:00
takeoff_airport_id = db . Column ( db . Integer , db . ForeignKey ( " airports.id " , ondelete = " CASCADE " ) , index = True )
2020-11-20 16:31:03 +00:00
takeoff_airport = db . relationship ( " Airport " , foreign_keys = [ takeoff_airport_id ] , backref = db . backref ( " logbook_entries_takeoff " , order_by = db . case ( { True : takeoff_timestamp , False : landing_timestamp } , takeoff_timestamp != db . null ( ) ) . desc ( ) ) )
2020-11-15 17:27:54 +00:00
takeoff_country_id = db . Column ( db . Integer , db . ForeignKey ( " countries.gid " , ondelete = " CASCADE " ) , index = True )
2020-11-20 16:31:03 +00:00
takeoff_country = db . relationship ( " Country " , foreign_keys = [ takeoff_country_id ] , backref = db . backref ( " logbook_entries_takeoff " , order_by = db . case ( { True : takeoff_timestamp , False : landing_timestamp } , takeoff_timestamp != db . null ( ) ) . desc ( ) ) )
2016-06-29 21:26:30 +00:00
2019-08-31 08:14:41 +00:00
landing_airport_id = db . Column ( db . Integer , db . ForeignKey ( " airports.id " , ondelete = " CASCADE " ) , index = True )
2020-11-20 16:31:03 +00:00
landing_airport = db . relationship ( " Airport " , foreign_keys = [ landing_airport_id ] , backref = db . backref ( " logbook_entries_landing " , order_by = db . case ( { True : takeoff_timestamp , False : landing_timestamp } , takeoff_timestamp != db . null ( ) ) . desc ( ) ) )
2020-11-15 17:27:54 +00:00
landing_country_id = db . Column ( db . Integer , db . ForeignKey ( " countries.gid " , ondelete = " CASCADE " ) , index = True )
2020-11-20 16:31:03 +00:00
landing_country = db . relationship ( " Country " , foreign_keys = [ landing_country_id ] , backref = db . backref ( " logbook_entries_landing " , order_by = db . case ( { True : takeoff_timestamp , False : landing_timestamp } , takeoff_timestamp != db . null ( ) ) . desc ( ) ) )
2016-06-29 21:26:30 +00:00
2016-10-31 19:39:07 +00:00
@hybrid_property
def duration ( self ) :
return None if ( self . landing_timestamp is None or self . takeoff_timestamp is None ) else self . landing_timestamp - self . takeoff_timestamp
@duration.expression
def duration ( cls ) :
2020-11-20 16:31:03 +00:00
return db . case ( { False : None , True : cls . landing_timestamp - cls . takeoff_timestamp } , cls . landing_timestamp != db . null ( ) and cls . takeoff_timestamp != db . null ( ) )
2020-10-27 19:46:14 +00:00
@hybrid_property
2020-11-15 17:27:54 +00:00
def reference_timestamp ( self ) :
2020-10-27 19:46:14 +00:00
return self . takeoff_timestamp if self . takeoff_timestamp is not None else self . landing_timestamp
2020-11-15 17:27:54 +00:00
@reference_timestamp.expression
def reference_timestamp ( cls ) :
2020-11-20 16:31:03 +00:00
return db . case ( { True : cls . takeoff_timestamp , False : cls . landing_timestamp } , cls . takeoff_timestamp != db . null ( ) )
2020-11-15 17:27:54 +00:00
2020-11-20 16:31:03 +00:00
#__table_args__ = (db.Index('idx_logbook_reference_timestamp', db.case({True: takeoff_timestamp, False: landing_timestamp}, takeoff_timestamp != db.null())),)
2020-11-15 17:27:54 +00:00
# FIXME: does not work...
# FIXME: this does not throw an error as the __table_args__ above, but there is no index created
2020-11-20 16:31:03 +00:00
#_wrapped_case = f"({db.case(whens={True: Logbook.takeoff_timestamp, False: Logbook.landing_timestamp}, value=Logbook.takeoff_timestamp != db.null())})"
2020-11-15 17:27:54 +00:00
#Index("idx_logbook_reference_timestamp", _wrapped_case)
# TODO:
# so execute manually: CREATE INDEX IF NOT EXISTS idx_logbook_reference_timestamp ON logbooks ((CASE takeoff_timestamp IS NULL WHEN true THEN takeoff_timestamp WHEN false THEN landing_timestamp END));