diff --git a/ogn/gateway/process.py b/ogn/gateway/process.py index 0c4b797..d50554d 100644 --- a/ogn/gateway/process.py +++ b/ogn/gateway/process.py @@ -3,6 +3,7 @@ import logging from ogn.commands.dbutils import session from ogn.model import AircraftBeacon, ReceiverBeacon, Location from ogn.parser import parse, ParseError +from datetime import datetime, timedelta logger = logging.getLogger(__name__) @@ -42,10 +43,24 @@ def message_to_beacon(raw_message, reference_date): return beacon +beacons = list() +last_commit = datetime.utcnow() + def process_beacon(raw_message, reference_date=None): + global beacons + global last_commit + beacon = message_to_beacon(raw_message, reference_date) if beacon is not None: - session.add(beacon) - session.commit() + beacons.append(beacon) logger.debug('Received message: {}'.format(raw_message)) + + current_time = datetime.utcnow() + elapsed_time = current_time - last_commit + if elapsed_time >= timedelta(seconds=1): + session.bulk_save_objects(beacons) + session.commit() + logger.debug('Commited beacons') + beacons = list() + last_commit = current_time