From 44e21ef348b079b3f053348f6c97bec9099cff6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Tue, 24 Sep 2019 19:37:48 +0200 Subject: [PATCH] Change int aircraft_type to enum --- app/gateway/bulkimport.py | 5 +- app/model/aircraft_beacon.py | 7 +- app/model/aircraft_type.py | 39 ++++++---- app/model/device.py | 3 +- app/model/device_info.py | 3 +- app/model/device_stats.py | 4 +- .../6c19cedf5fa7_use_enum_for_aircrafttype.py | 76 +++++++++++++++++++ 7 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 migrations/versions/6c19cedf5fa7_use_enum_for_aircrafttype.py diff --git a/app/gateway/bulkimport.py b/app/gateway/bulkimport.py index 6c05d72..69f0b8d 100644 --- a/app/gateway/bulkimport.py +++ b/app/gateway/bulkimport.py @@ -9,7 +9,7 @@ from mgrs import MGRS from ogn.parser import parse, ParseError -from app.model import AircraftBeacon, ReceiverBeacon, Location +from app.model import AircraftBeacon, AircraftType, ReceiverBeacon, Location from app.utils import open_file from app.gateway.process_tools import create_indices, add_missing_devices, add_missing_receivers, update_aircraft_beacons, update_receiver_beacons, update_receiver_location, transfer_aircraft_beacons, transfer_receiver_beacons, delete_aircraft_beacons, delete_receiver_beacons, update_aircraft_beacons_bigdata, update_receiver_beacons_bigdata, create_tables @@ -115,6 +115,9 @@ def string_to_message(raw_string, reference_date): message["location_mgrs"] = location_mgrs message["location_mgrs_short"] = location_mgrs[0:5] + location_mgrs[5:7] + location_mgrs[10:12] + if message["beacon_type"] in AIRCRAFT_BEACON_TYPES and "aircraft_type" in message: + message["aircraft_type"] = AircraftType(message["aircraft_type"]).name if message["aircraft_type"] else AircraftType.UNKNOWN.name + if message["beacon_type"] in AIRCRAFT_BEACON_TYPES and "gps_quality" in message: if message["gps_quality"] is not None and "horizontal" in message["gps_quality"]: message["gps_quality_horizontal"] = message["gps_quality"]["horizontal"] diff --git a/app/model/aircraft_beacon.py b/app/model/aircraft_beacon.py index 4fd0f26..bbde49a 100644 --- a/app/model/aircraft_beacon.py +++ b/app/model/aircraft_beacon.py @@ -1,15 +1,16 @@ from sqlalchemy.sql import func -from .beacon import Beacon - from app import db +from .beacon import Beacon +from .aircraft_type import AircraftType + class AircraftBeacon(Beacon): __tablename__ = "aircraft_beacons" # Flarm specific data address_type = db.Column(db.SmallInteger) - aircraft_type = db.Column(db.SmallInteger) + aircraft_type = db.Column(db.Enum(AircraftType), nullable=False, default=AircraftType.UNKNOWN) stealth = db.Column(db.Boolean) address = db.Column(db.String) climb_rate = db.Column(db.Float(precision=2)) diff --git a/app/model/aircraft_type.py b/app/model/aircraft_type.py index 563704e..7ec95cb 100644 --- a/app/model/aircraft_type.py +++ b/app/model/aircraft_type.py @@ -1,16 +1,23 @@ -class AircraftType: - unknown = 0 - glider_or_motor_glider = 1 - tow_tug_plane = 2 - helicopter_rotorcraft = 3 - parachute = 4 - drop_plane = 5 - hang_glider = 6 - para_glider = 7 - powered_aircraft = 8 - jet_aircraft = 9 - flying_saucer = 10 - balloon = 11 - airship = 12 - unmanned_aerial_vehicle = 13 - static_object = 15 +import enum + + +class AircraftType(enum.Enum): + UNKNOWN = 0 + GLIDER_OR_MOTOR_GLIDER = 1 + TOW_TUG_PLANE = 2 + HELICOPTER_ROTORCRAFT = 3 + PARACHUTE = 4 + DROP_PLANE = 5 + HANG_GLIDER = 6 + PARA_GLIDER = 7 + POWERED_AIRCRAFT = 8 + JET_AIRCRAFT = 9 + FLYING_SAUCER = 10 + BALLOON = 11 + AIRSHIP = 12 + UNMANNED_AERIAL_VEHICLE = 13 + STATIC_OBJECT = 15 + + @staticmethod + def list(): + return list(map(lambda c: c.value, AircraftType)) diff --git a/app/model/device.py b/app/model/device.py index 4200d03..e83f2fd 100644 --- a/app/model/device.py +++ b/app/model/device.py @@ -4,6 +4,7 @@ from sqlalchemy.ext.hybrid import hybrid_property from app import db from .device_info import DeviceInfo +from app.model.aircraft_type import AircraftType class Device(db.Model): @@ -16,7 +17,7 @@ class Device(db.Model): address = db.Column(db.String, index=True) firstseen = db.Column(db.DateTime, index=True) lastseen = db.Column(db.DateTime, index=True) - aircraft_type = db.Column(db.SmallInteger, index=True) + aircraft_type = db.Column(db.Enum(AircraftType), nullable=False, default=AircraftType.UNKNOWN) stealth = db.Column(db.Boolean) software_version = db.Column(db.Float(precision=2)) hardware_version = db.Column(db.SmallInteger) diff --git a/app/model/device_info.py b/app/model/device_info.py index d721a20..7b480c7 100644 --- a/app/model/device_info.py +++ b/app/model/device_info.py @@ -1,5 +1,6 @@ from app import db from .device_info_origin import DeviceInfoOrigin +from .aircraft_type import AircraftType class DeviceInfo(db.Model): @@ -14,7 +15,7 @@ class DeviceInfo(db.Model): competition = db.Column(db.String(3)) tracked = db.Column(db.Boolean) identified = db.Column(db.Boolean) - aircraft_type = db.Column(db.SmallInteger) + aircraft_type = db.Column(db.Enum(AircraftType), nullable=False, default=AircraftType.UNKNOWN) address_origin = db.Column(db.Enum(DeviceInfoOrigin), nullable=False, default=DeviceInfoOrigin.UNKNOWN) diff --git a/app/model/device_stats.py b/app/model/device_stats.py index 5c3ac00..378be1e 100644 --- a/app/model/device_stats.py +++ b/app/model/device_stats.py @@ -1,5 +1,7 @@ from app import db +from .aircraft_type import AircraftType + class DeviceStats(db.Model): __tablename__ = "device_stats" @@ -12,7 +14,7 @@ class DeviceStats(db.Model): name = db.Column(db.String) firstseen = db.Column(db.DateTime) lastseen = db.Column(db.DateTime) - aircraft_type = db.Column(db.SmallInteger) + aircraft_type = db.Column(db.Enum(AircraftType), nullable=False, default=AircraftType.UNKNOWN) stealth = db.Column(db.Boolean) software_version = db.Column(db.Float(precision=2)) hardware_version = db.Column(db.SmallInteger) diff --git a/migrations/versions/6c19cedf5fa7_use_enum_for_aircrafttype.py b/migrations/versions/6c19cedf5fa7_use_enum_for_aircrafttype.py new file mode 100644 index 0000000..af11149 --- /dev/null +++ b/migrations/versions/6c19cedf5fa7_use_enum_for_aircrafttype.py @@ -0,0 +1,76 @@ +"""Use Enum for AircraftType + +Revision ID: 6c19cedf5fa7 +Revises: be9a6dad551e +Create Date: 2019-09-24 18:37:40.224279 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '6c19cedf5fa7' +down_revision = 'be9a6dad551e' +branch_labels = None +depends_on = None + + +aircrafttype = postgresql.ENUM('UNKNOWN', 'GLIDER_OR_MOTOR_GLIDER', 'TOW_TUG_PLANE', 'HELICOPTER_ROTORCRAFT', + 'PARACHUTE', 'DROP_PLANE', 'HANG_GLIDER', 'PARA_GLIDER', 'POWERED_AIRCRAFT', + 'JET_AIRCRAFT', 'FLYING_SAUCER', 'BALLOON', 'AIRSHIP', 'UNMANNED_AERIAL_VEHICLE', + 'STATIC_OBJECT', name='aircrafttype') + +def upgrade(): + aircrafttype.create(op.get_bind()) + + for table in ['aircraft_beacons', 'devices']: + op.add_column(table, sa.Column('aircraft_type_enum', sa.Enum( + 'UNKNOWN', 'GLIDER_OR_MOTOR_GLIDER', 'TOW_TUG_PLANE', 'HELICOPTER_ROTORCRAFT', + 'PARACHUTE', 'DROP_PLANE', 'HANG_GLIDER', 'PARA_GLIDER', 'POWERED_AIRCRAFT', + 'JET_AIRCRAFT', 'FLYING_SAUCER', 'BALLOON', 'AIRSHIP', 'UNMANNED_AERIAL_VEHICLE', + 'STATIC_OBJECT', name='aircrafttype'), nullable=False, server_default='UNKNOWN')) + op.execute(""" + UPDATE {table} SET aircraft_type_enum = 'UNKNOWN' WHERE aircraft_type = 0; + UPDATE {table} SET aircraft_type_enum = 'GLIDER_OR_MOTOR_GLIDER' WHERE aircraft_type = 1; + UPDATE {table} SET aircraft_type_enum = 'TOW_TUG_PLANE' WHERE aircraft_type = 2; + UPDATE {table} SET aircraft_type_enum = 'HELICOPTER_ROTORCRAFT' WHERE aircraft_type = 3; + UPDATE {table} SET aircraft_type_enum = 'PARACHUTE' WHERE aircraft_type = 4; + UPDATE {table} SET aircraft_type_enum = 'DROP_PLANE' WHERE aircraft_type = 5; + UPDATE {table} SET aircraft_type_enum = 'HANG_GLIDER' WHERE aircraft_type = 6; + UPDATE {table} SET aircraft_type_enum = 'PARA_GLIDER' WHERE aircraft_type = 7; + UPDATE {table} SET aircraft_type_enum = 'POWERED_AIRCRAFT' WHERE aircraft_type = 8; + UPDATE {table} SET aircraft_type_enum = 'JET_AIRCRAFT' WHERE aircraft_type = 9; + UPDATE {table} SET aircraft_type_enum = 'FLYING_SAUCER' WHERE aircraft_type = 10; + UPDATE {table} SET aircraft_type_enum = 'BALLOON' WHERE aircraft_type = 11; + UPDATE {table} SET aircraft_type_enum = 'AIRSHIP' WHERE aircraft_type = 12; + UPDATE {table} SET aircraft_type_enum = 'UNMANNED_AERIAL_VEHICLE' WHERE aircraft_type = 13; + UPDATE {table} SET aircraft_type_enum = 'STATIC_OBJECT' WHERE aircraft_type = 15; + """.format(table=table)) + op.drop_column(table, 'aircraft_type') + op.alter_column(table, 'aircraft_type_enum', new_column_name='aircraft_type') + +def downgrade(): + for table in ['aircraft_beacons', 'devices']: + op.add_column(table, sa.Column('aircraft_type_int', sa.SmallInteger)) + op.execute(""" + UPDATE {table} SET aircraft_type_int = 0 WHERE aircraft_type = 'UNKNOWN'; + UPDATE {table} SET aircraft_type_int = 1 WHERE aircraft_type = 'GLIDER_OR_MOTOR_GLIDER'; + UPDATE {table} SET aircraft_type_int = 2 WHERE aircraft_type = 'TOW_TUG_PLANE'; + UPDATE {table} SET aircraft_type_int = 3 WHERE aircraft_type = 'HELICOPTER_ROTORCRAFT'; + UPDATE {table} SET aircraft_type_int = 4 WHERE aircraft_type = 'PARACHUTE'; + UPDATE {table} SET aircraft_type_int = 5 WHERE aircraft_type = 'DROP_PLANE'; + UPDATE {table} SET aircraft_type_int = 6 WHERE aircraft_type = 'HANG_GLIDER'; + UPDATE {table} SET aircraft_type_int = 7 WHERE aircraft_type = 'PARA_GLIDER'; + UPDATE {table} SET aircraft_type_int = 8 WHERE aircraft_type = 'POWERED_AIRCRAFT'; + UPDATE {table} SET aircraft_type_int = 9 WHERE aircraft_type = 'JET_AIRCRAFT'; + UPDATE {table} SET aircraft_type_int = 10 WHERE aircraft_type = 'FLYING_SAUCER'; + UPDATE {table} SET aircraft_type_int = 11 WHERE aircraft_type = 'BALLOON'; + UPDATE {table} SET aircraft_type_int = 12 WHERE aircraft_type = 'AIRSHIP'; + UPDATE {table} SET aircraft_type_int = 13 WHERE aircraft_type = 'UNMANNED_AERIAL_VEHICLE'; + UPDATE {table} SET aircraft_type_int = 15 WHERE aircraft_type = 'STATIC_OBJECT'; + """.format(table=table)) + op.drop_column(table, 'aircraft_type') + op.alter_column(table, 'aircraft_type_int', new_column_name='aircraft_type') + + aircrafttype.drop(op.get_bind()) \ No newline at end of file