From 82efb81fbae6f632fab2e43cc926033d095d9d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Tue, 24 Nov 2015 23:29:27 +0100 Subject: [PATCH] Logbook: recalculate last 5 minutes --- ogn/collect/fetchddb.py | 1 + ogn/collect/logbook.py | 20 +++++++++++++++++--- ogn/commands/logbook.py | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/ogn/collect/fetchddb.py b/ogn/collect/fetchddb.py index f228ed2..6b6fc57 100644 --- a/ogn/collect/fetchddb.py +++ b/ogn/collect/fetchddb.py @@ -36,6 +36,7 @@ def update_ddb_from_file(): .delete() devices = get_ddb('ogn/custom_ddb.txt') + logger.debug("New Devices: %s" % str(devices)) app.session.bulk_save_objects(devices) app.session.commit() diff --git a/ogn/collect/logbook.py b/ogn/collect/logbook.py index 59e835e..a0d51f1 100644 --- a/ogn/collect/logbook.py +++ b/ogn/collect/logbook.py @@ -1,6 +1,6 @@ from __future__ import absolute_import -from datetime import datetime +from datetime import datetime, timedelta from celery.utils.log import get_task_logger from ogn.collect.celery import app @@ -16,14 +16,24 @@ logger = get_task_logger(__name__) @app.task def compute_takeoff_and_landing(): + logger.info("Compute takeoffs and landings.") + takeoff_speed = 30 landing_speed = 30 - # get last takeoff_landing time as starting point for the following search + # calculate the time where the computation starts last_takeoff_landing_query = app.session.query(func.max(TakeoffLanding.timestamp)) last_takeoff_landing = last_takeoff_landing_query.one()[0] if last_takeoff_landing is None: + # if the table is empty last_takeoff_landing = datetime(2015, 1, 1, 0, 0, 0) + else: + # we get the beacons async. to be safe we delete takeoffs/landings from last 5 minutes and recalculate from then + # alternative: takeoff/landing has a primary key (timestamp,address) + last_takeoff_landing = last_takeoff_landing - timedelta(minutes=5) + app.session.query(TakeoffLanding) \ + .filter(TakeoffLanding.timestamp > last_takeoff_landing) \ + .delete() # make a query with current, previous and next position, so we can detect takeoffs and landings sq = app.session.query( @@ -75,5 +85,9 @@ def compute_takeoff_and_landing(): # ... and save them ins = insert(TakeoffLanding).from_select((TakeoffLanding.address, TakeoffLanding.timestamp, TakeoffLanding.latitude, TakeoffLanding.longitude, TakeoffLanding.track, TakeoffLanding.ground_speed, TakeoffLanding.altitude, TakeoffLanding.is_takeoff), takeoff_landing_query) - app.session.execute(ins) + result = app.session.execute(ins) + counter = result.rowcount app.session.commit() + logger.debug("New/recalculated takeoffs and landings: %s" % counter) + + return counter diff --git a/ogn/commands/logbook.py b/ogn/commands/logbook.py index 116409c..35f079c 100644 --- a/ogn/commands/logbook.py +++ b/ogn/commands/logbook.py @@ -19,7 +19,9 @@ manager = Manager() def compute(): """Compute takeoffs and landings.""" print("Compute takeoffs and landings...") - compute_takeoff_and_landing.delay() + result = compute_takeoff_and_landing.delay() + counter = result.get() + print("New/recalculated takeoffs/landings: %s" % counter) @manager.command