kopia lustrzana https://github.com/glidernet/ogn-python
Bulk insert once per second
rodzic
6aa99bd18f
commit
61939913b5
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue