Change int aircraft_type to enum

pull/78/head
Konstantin Gründger 2019-09-24 19:37:48 +02:00
rodzic 87ee519f0d
commit 44e21ef348
7 zmienionych plików z 114 dodań i 23 usunięć

Wyświetl plik

@ -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"]

Wyświetl plik

@ -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))

Wyświetl plik

@ -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))

Wyświetl plik

@ -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)

Wyświetl plik

@ -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)

Wyświetl plik

@ -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)

Wyświetl plik

@ -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())