kopia lustrzana https://github.com/glidernet/ogn-python
Refactoring
rodzic
28a25048f6
commit
3956a11aa5
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Ładowanie…
Reference in New Issue