From 3956a11aa581ef5a067517528eff19e2aaa78785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Sun, 10 Dec 2017 20:07:37 +0100 Subject: [PATCH] Refactoring --- ogn/collect/logbook.py | 34 +++++++++++++++++++++++++++++++++- ogn/collect/takeoff_landing.py | 12 +++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/ogn/collect/logbook.py b/ogn/collect/logbook.py index ff938a5..e99fef9 100644 --- a/ogn/collect/logbook.py +++ b/ogn/collect/logbook.py @@ -5,7 +5,7 @@ from sqlalchemy.sql import func, null from sqlalchemy.sql.expression import true, false from ogn.collect.celery import app -from ogn.model import TakeoffLanding, Logbook +from ogn.model import TakeoffLanding, Logbook, AircraftBeacon logger = get_task_logger(__name__) @@ -157,3 +157,35 @@ def update_logbook(session=None): logger.debug("New logbook entries: {}".format(insert_counter)) return "{}/{}".format(update_counter, insert_counter) + + +@app.task +def update_max_altitude(session=None): + logger.info("Update logbook max altitude.") + + if session is None: + session = app.session + + logbook_entries = session.query(Logbook.id) \ + .filter(and_(Logbook.takeoff_timestamp != null(), Logbook.landing_timestamp != null(), Logbook.max_altitude == null())) \ + .limit(1000) \ + .subquery() + + max_altitudes = session.query(Logbook.id, func.max(AircraftBeacon.altitude).label('max_altitude')) \ + .filter(Logbook.id == logbook_entries.c.id) \ + .filter(and_(AircraftBeacon.device_id == Logbook.device_id, + AircraftBeacon.timestamp >= Logbook.takeoff_timestamp, + AircraftBeacon.timestamp <= Logbook.landing_timestamp)) \ + .group_by(Logbook.id) \ + .subquery() + + update_logbook = app.session.query(Logbook) \ + .filter(Logbook.id == max_altitudes.c.id) \ + .update({ + Logbook.max_altitude: max_altitudes.c.max_altitude}, + synchronize_session='fetch') + + session.commit() + logger.info("Logbook: {} entries updated.".format(update_logbook)) + + return update_logbook diff --git a/ogn/collect/takeoff_landing.py b/ogn/collect/takeoff_landing.py index 48c0007..26f53b6 100644 --- a/ogn/collect/takeoff_landing.py +++ b/ogn/collect/takeoff_landing.py @@ -42,6 +42,12 @@ def update_takeoff_landing(session=None): AircraftBeacon.receiver_id) # make a query with current, previous and next position + beacon_selection = session.query(AircraftBeacon.id) \ + .filter(AircraftBeacon.status == null()) \ + .order_by(AircraftBeacon.timestamp) \ + .limit(1000000) \ + .subquery() + sq = session.query( AircraftBeacon.id, func.lag(AircraftBeacon.id).over(order_by=wo).label('id_prev'), @@ -49,8 +55,7 @@ def update_takeoff_landing(session=None): AircraftBeacon.device_id, func.lag(AircraftBeacon.device_id).over(order_by=wo).label('device_id_prev'), func.lead(AircraftBeacon.device_id).over(order_by=wo).label('device_id_next')) \ - .filter(AircraftBeacon.status == null()) \ - .filter(and_(AircraftBeacon.timestamp >= '2017-12-09 11:00:00', AircraftBeacon.timestamp <= '2017-12-09 18:00:00')) \ + .filter(AircraftBeacon.id == beacon_selection.c.id) \ .subquery() # consider only positions with the same device id @@ -58,9 +63,6 @@ def update_takeoff_landing(session=None): .filter(sq.c.device_id_prev == sq.c.device_id == sq.c.device_id_next) \ .subquery() - print(sq2) - return - # Get timestamps, locations, tracks, ground_speeds and altitudes prev_ab = aliased(AircraftBeacon, name="prev_ab") lead_ab = aliased(AircraftBeacon, name="lead_ab")