kopia lustrzana https://github.com/glidernet/ogn-python
Update: complete flights only
rodzic
d857f88512
commit
08c0a73d54
|
@ -101,32 +101,7 @@ def compute_logbook_entries(session=None):
|
||||||
sq.c.is_takeoff_next == true(),
|
sq.c.is_takeoff_next == true(),
|
||||||
sq.c.is_takeoff_next == null()))
|
sq.c.is_takeoff_next == null()))
|
||||||
|
|
||||||
# update 'incomplete' logbook entries with 'complete flights'
|
# unite all computated flights
|
||||||
complete_flights = complete_flight_query.subquery()
|
|
||||||
|
|
||||||
upd = update(Logbook) \
|
|
||||||
.where(and_(Logbook.device_id == complete_flights.c.device_id,
|
|
||||||
or_(and_(Logbook.takeoff_airport_id == complete_flights.c.takeoff_airport_id,
|
|
||||||
Logbook.takeoff_timestamp == complete_flights.c.takeoff_timestamp,
|
|
||||||
Logbook.landing_airport_id == null()),
|
|
||||||
and_(Logbook.takeoff_airport_id == null(),
|
|
||||||
Logbook.landing_airport_id == complete_flights.c.landing_airport_id,
|
|
||||||
Logbook.landing_timestamp == complete_flights.c.landing_timestamp)))) \
|
|
||||||
.values({"takeoff_timestamp": complete_flights.c.takeoff_timestamp,
|
|
||||||
"takeoff_track": complete_flights.c.takeoff_track,
|
|
||||||
"takeoff_airport_id": complete_flights.c.takeoff_airport_id,
|
|
||||||
"landing_timestamp": complete_flights.c.landing_timestamp,
|
|
||||||
"landing_track": complete_flights.c.landing_track,
|
|
||||||
"landing_airport_id": complete_flights.c.landing_airport_id,
|
|
||||||
"duration": complete_flights.c.duration,
|
|
||||||
"max_altitude": 1})
|
|
||||||
|
|
||||||
result = session.execute(upd)
|
|
||||||
update_counter = result.rowcount
|
|
||||||
session.commit()
|
|
||||||
logger.debug("Updated logbook entries: {}".format(update_counter))
|
|
||||||
|
|
||||||
# unite all computated flights ('incomplete' and 'complete')
|
|
||||||
union_query = complete_flight_query.union(
|
union_query = complete_flight_query.union(
|
||||||
split_start_query,
|
split_start_query,
|
||||||
split_landing_query,
|
split_landing_query,
|
||||||
|
@ -134,7 +109,31 @@ def compute_logbook_entries(session=None):
|
||||||
only_starts_query) \
|
only_starts_query) \
|
||||||
.subquery()
|
.subquery()
|
||||||
|
|
||||||
# consider only if not already stored
|
# if a logbook entry exist --> update it
|
||||||
|
upd = update(Logbook) \
|
||||||
|
.where(and_(Logbook.device_id == union_query.c.device_id,
|
||||||
|
union_query.c.takeoff_airport_id != null(),
|
||||||
|
union_query.c.landing_airport_id != null(),
|
||||||
|
or_(and_(Logbook.takeoff_airport_id == union_query.c.takeoff_airport_id,
|
||||||
|
Logbook.takeoff_timestamp == union_query.c.takeoff_timestamp,
|
||||||
|
Logbook.landing_airport_id == null()),
|
||||||
|
and_(Logbook.takeoff_airport_id == null(),
|
||||||
|
Logbook.landing_airport_id == union_query.c.landing_airport_id,
|
||||||
|
Logbook.landing_timestamp == union_query.c.landing_timestamp)))) \
|
||||||
|
.values({"takeoff_timestamp": union_query.c.takeoff_timestamp,
|
||||||
|
"takeoff_track": union_query.c.takeoff_track,
|
||||||
|
"takeoff_airport_id": union_query.c.takeoff_airport_id,
|
||||||
|
"landing_timestamp": union_query.c.landing_timestamp,
|
||||||
|
"landing_track": union_query.c.landing_track,
|
||||||
|
"landing_airport_id": union_query.c.landing_airport_id,
|
||||||
|
"duration": union_query.c.duration})
|
||||||
|
|
||||||
|
result = session.execute(upd)
|
||||||
|
update_counter = result.rowcount
|
||||||
|
session.commit()
|
||||||
|
logger.debug("Updated logbook entries: {}".format(update_counter))
|
||||||
|
|
||||||
|
# if a logbook entry doesnt exist --> insert it
|
||||||
new_logbook_entries = session.query(union_query) \
|
new_logbook_entries = session.query(union_query) \
|
||||||
.filter(~exists().where(
|
.filter(~exists().where(
|
||||||
and_(Logbook.device_id == union_query.c.device_id,
|
and_(Logbook.device_id == union_query.c.device_id,
|
||||||
|
@ -147,7 +146,6 @@ def compute_logbook_entries(session=None):
|
||||||
and_(Logbook.landing_airport_id == null(),
|
and_(Logbook.landing_airport_id == null(),
|
||||||
union_query.c.landing_airport_id == null())))))
|
union_query.c.landing_airport_id == null())))))
|
||||||
|
|
||||||
# ... and save them
|
|
||||||
ins = insert(Logbook).from_select((Logbook.reftime,
|
ins = insert(Logbook).from_select((Logbook.reftime,
|
||||||
Logbook.device_id,
|
Logbook.device_id,
|
||||||
Logbook.takeoff_timestamp,
|
Logbook.takeoff_timestamp,
|
||||||
|
|
|
@ -46,6 +46,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/1')
|
self.assertEqual(entries_changed, '0/1')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_single_landing(self):
|
def test_single_landing(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
@ -55,6 +58,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/1')
|
self.assertEqual(entries_changed, '0/1')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_different_takeoffs(self):
|
def test_different_takeoffs(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
@ -65,6 +71,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/2')
|
self.assertEqual(entries_changed, '0/2')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_takeoff_and_landing(self):
|
def test_takeoff_and_landing(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
@ -75,6 +84,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/1')
|
self.assertEqual(entries_changed, '0/1')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_takeoff_and_landing_on_different_days(self):
|
def test_takeoff_and_landing_on_different_days(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
@ -85,6 +97,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/2')
|
self.assertEqual(entries_changed, '0/2')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_update(self):
|
def test_update(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
@ -106,6 +121,9 @@ class TestDB(unittest.TestCase):
|
||||||
entries_changed = compute_logbook_entries(session)
|
entries_changed = compute_logbook_entries(session)
|
||||||
self.assertEqual(entries_changed, '0/1')
|
self.assertEqual(entries_changed, '0/1')
|
||||||
|
|
||||||
|
entries_changed = compute_logbook_entries(session)
|
||||||
|
self.assertEqual(entries_changed, '0/0')
|
||||||
|
|
||||||
def test_update_wrong_order(self):
|
def test_update_wrong_order(self):
|
||||||
session = self.session
|
session = self.session
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue