Skip to content
Fabian P. Schmidt edited this page Mar 30, 2016 · 6 revisions

The gateway of ogn-python stores all beacons to a database and your program connects to this database. If you want to process the incoming beacons before they hit any database, use python-ogn-client directly.

Before you can execute the following example, you have to

Example: Sum of received messages per receiver

This example connects with the default database from ogn-python and shows the sum of received messages per receiver in the last hour.

from datetime import datetime, timedelta
from sqlalchemy import func

from ogn.model import AircraftBeacon
from ogn.commands.dbutils import session

def show_last_hour():
    query = session.query(AircraftBeacon.receiver_name, func.count(AircraftBeacon.id)) \
        .filter(AircraftBeacon.timestamp > (datetime.utcnow() - timedelta(hours=1))) \
        .group_by(AircraftBeacon.receiver_name) \
        .order_by(AircraftBeacon.receiver_name)

    for [receiver_name, message_count] in query.all():
        print("{0:9s}: {1:5d}".format(receiver_name, message_count))

if __name__ == '__main__':
    show_last_hour()
Clone this wiki locally