Rewrote python jobs.

pull/586/head
jprochazka 2024-07-12 15:30:18 -04:00
rodzic 68671e17a8
commit 4d964302b1
3 zmienionych plików z 288 dodań i 349 usunięć

Wyświetl plik

@ -1,36 +1,5 @@
#!/bin/bash
#####################################################################################
# ADS-B RECEIVER #
#####################################################################################
# #
# This script is not meant to be executed directly. #
# Instead execute install.sh to begin the installation process. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Copyright (c) 2015-2024 Joseph A. Prochazka #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
## VARIABLES
PORTAL_BUILD_DIRECTORY="${RECEIVER_BUILD_DIRECTORY}/portal"
@ -273,7 +242,7 @@ if [[ "${ADVANCED}" = "true" ]] ; then
case "${DATABASEENGINE}" in
"MySQL")
CheckPackage mariadb-client
CheckPackage python3-mysqldb
CheckPackage python3-mysql.connector
CheckPackage php${DISTRO_PHP_VERSION}-mysql
;;
"SQLite")

Wyświetl plik

@ -1,211 +1,175 @@
#!/usr/bin/python
#================================================================================#
# ADS-B FEEDER PORTAL #
# ------------------------------------------------------------------------------ #
# Copyright and Licensing Information: #
# #
# The MIT License (MIT) #
# #
# Copyright (c) 2015-2024 Joseph A. Prochazka #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
#================================================================================#
# WHAT THIS DOES:
# ---------------------------------------------------------------
#
# 1) Read aircraft.json generated by dump1090-fa.
# 2) Add the flight to the database if it does not already exist.
# 3) Update the last time the flight was seen.
import datetime
import fcntl
import json
import logging
import os
import time
from urllib.request import urlopen
from urllib import urlopen
def log(string):
#print(string) # uncomment to enable debug logging
return
now = None
# Do not allow another instance of the script to run.
lock_file = open('/tmp/flights.py.lock','w')
class AircraftProcessor(object):
try:
fcntl.flock(lock_file, fcntl.LOCK_EX|fcntl.LOCK_NB)
except (IOError, OSError):
log('another instance already running')
quit()
# Create database connection
def create_connection():
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
config = json.load(config_file)
lock_file.write('%d\n'%os.getpid())
lock_file.flush()
match config["database"]["type"].lower():
case 'mysql':
import mysql.connector
return mysql.connector.connect(
host=config["database"]["host"],
user=config["database"]["user"],
password=config["database"]["passwd"],
database=config["database"]["db"]
)
case 'sqlite':
import sqlite3
return sqlite3.connect(config["database"]["db"])
# Read the configuration file.
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
config = json.load(config_file)
# Import the needed database library.
if config["database"]["type"] == "mysql":
import MySQLdb
else:
import sqlite3
class FlightsProcessor(object):
def __init__(self, config):
self.config = config
self.dbType = config["database"]["type"]
# List of required keys for position data entries
self.position_keys = ('lat', 'lon', 'nav_altitude', 'gs', 'track', 'geom_rate', 'hex')
def setupDBStatements(self, formatSymbol):
if hasattr(self, 'STMTS'):
# Read JSON supplied by dump1090
def read_json():
try:
raw_json = urlopen('http://127.0.0.1/dump1090/data/aircraft.json')
json_object = json.load(raw_json)
return json_object
except:
logging.error("There was a problem consuming aircraft.json")
return
mapping = { "s": formatSymbol }
self.STMTS = {
'select_aircraft_count':"SELECT COUNT(*) FROM adsb_aircraft WHERE icao = %(s)s" % mapping,
'select_aircraft_id': "SELECT id FROM adsb_aircraft WHERE icao = %(s)s" % mapping,
'select_flight_count': "SELECT COUNT(*) FROM adsb_flights WHERE flight = %(s)s" % mapping,
'select_flight_id': "SELECT id FROM adsb_flights WHERE flight = %(s)s" % mapping,
'select_position': "SELECT message FROM adsb_positions WHERE flight = %(s)s AND message = %(s)s ORDER BY time DESC LIMIT 1" % mapping,
'insert_aircraft': "INSERT INTO adsb_aircraft (icao, firstSeen, lastSeen) VALUES (%(s)s, %(s)s, %(s)s)" % mapping,
'insert_flight': "INSERT INTO adsb_flights (aircraft, flight, firstSeen, lastSeen) VALUES (%(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
'insert_position_sqwk': "INSERT INTO adsb_positions (flight, time, message, squawk, latitude, longitude, track, altitude, verticleRate, speed, aircraft) VALUES (%(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
'insert_position': "INSERT INTO adsb_positions (flight, time, message, latitude, longitude, track, altitude, verticleRate, speed, aircraft) VALUES (%(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
'update_aircraft_seen': "UPDATE adsb_aircraft SET lastSeen = %(s)s WHERE icao = %(s)s" % mapping,
'update_flight_seen': "UPDATE adsb_flights SET aircraft = %(s)s, lastSeen = %(s)s WHERE flight = %(s)s" % mapping
}
def connectDB(self):
if self.dbType == "sqlite": ## Connect to a SQLite database.
self.setupDBStatements("?")
return sqlite3.connect(self.config["database"]["db"])
else: ## Connect to a MySQL database.
self.setupDBStatements("%s")
return MySQLdb.connect(host=self.config["database"]["host"],
user=self.config["database"]["user"],
passwd=self.config["database"]["passwd"],
db=self.config["database"]["db"])
# Begin processing data retrived from dump1090
def process_all_aircraft(self, all_aircraft):
def processAircraftList(self, aircraftList):
db = self.connectDB()
# Get Database cursor handle
self.cursor = db.cursor()
# Assign the time to a variable.
self.time_now = datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S")
connection = self.create_connection()
self.cursor = connection.cursor()
for aircraft in aircraftList:
self.processAircraft(aircraft)
for aircraft in all_aircraft:
self.process_aircraft(aircraft)
# Close the database connection.
db.commit()
db.close()
connection.commit()
connection.close()
def processAircraft(self, aircraft):
hexcode = aircraft["hex"]
# Check if this aircraft was already seen.
self.cursor.execute(self.STMTS['select_aircraft_count'], (hexcode,))
row_count = self.cursor.fetchone()
if row_count[0] == 0:
# Insert the new aircraft.
log("Added Aircraft: " + hexcode)
self.cursor.execute(self.STMTS['insert_aircraft'], (hexcode, self.time_now, self.time_now,))
return
# Process the aircraft
def process_aircraft(self, aircraft):
tracked=False
aircraft_id=None
try:
self.cursor.execute("SELECT COUNT(*) FROM aircraft WHERE icao = %s", (aircraft["hex"],))
if self.cursor.fetchone()[0] > 0:
tracked=True
except Exception as ex:
logging.error(f"Error encountered while checking if aircraft '{aircraft["hex"]}' has already been added", exc_info=ex)
return
if tracked:
query = "UPDATE aircraft SET lastSeen = %s WHERE icao = %s",
parameters = (now, aircraft["hex"])
error_message = f"Error encountered while trying to update aircraft '{aircraft["hex"]}'"
else:
# Update the existing aircraft.
self.cursor.execute(self.STMTS['update_aircraft_seen'], (self.time_now, hexcode,))
log("Updating Aircraft: " + hexcode)
# Get the ID of this aircraft.
self.cursor.execute(self.STMTS['select_aircraft_id'], (hexcode,))
row = self.cursor.fetchone()
aircraft_id = row[0]
log("\tFound Aircraft ID: " + str(aircraft_id))
query = "INSERT INTO aircraft (icao, firstSeen, lastSeen) VALUES (%s, %s, %s)",
parameters = (aircraft["hex"], now, now)
error_message = f"Error encountered while trying to insert aircraft '{aircraft["hex"]}'"
# Check that a flight is tied to this track.
if 'flight' in aircraft:
self.processFlight(aircraft_id, aircraft)
try:
self.cursor.execute(query, parameters)
aircraft_id = self.cursor.lastrowid
except Exception as ex:
logging.error(error_message, exc_info=ex)
return
def processFlight(self, aircraft_id, aircraft):
flight = aircraft["flight"].strip()
# Check to see if the flight already exists in the database.
self.cursor.execute(self.STMTS['select_flight_count'], (flight,))
row_count = self.cursor.fetchone()
if row_count[0] == 0:
# If the flight does not exist in the database add it.
params = (aircraft_id, flight, self.time_now, self.time_now,)
self.cursor.execute(self.STMTS['insert_flight'], params)
log("\t\tAdded Flight: " + flight)
if 'flight' in aircraft:
self.process_flight(aircraft_id, aircraft)
return
# Process the flight
def process_flight(self, aircraft_id, aircraft):
tracked=False
try:
self.cursor.execute("SELECT COUNT(*) FROM flights WHERE flight = %s", (aircraft["flight"],))
if self.cursor.fetchone()[0] > 0:
tracked=True
except Exception as ex:
logging.error(f"Error encountered while checking if flight '{aircraft["flight"]}' has already been added", exc_info=ex)
return
if tracked:
query = "UPDATE flights SET lastSeen = %s WHERE icao = %s",
parameters = (now, aircraft["flight"])
error_message = f"Error encountered while trying to update flight '{aircraft["flight"]}'"
else:
# If it already exists pdate the time it was last seen.
params = (aircraft_id, self.time_now, flight,)
self.cursor.execute(self.STMTS['update_flight_seen'], params)
log("\t\tUpdated Flight: " + flight)
# Get the ID of this flight.
self.cursor.execute(self.STMTS['select_flight_id'], (flight,))
row = self.cursor.fetchone()
flight_id = row[0]
query = "INSERT INTO flights (aircraft, flight, firstSeen, lastSeen) VALUES (%s, %s, %s, %s)",
parameters = (aircraft_id, aircraft["flight"], now, now)
error_message = f"Error encountered while trying to insert flight '{aircraft["flight"]}'"
# Check if position data is available.
if (all (k in aircraft for k in self.position_keys) and aircraft["altitude"] != "ground"):
self.processPositions(flight_id, aircraft)
try:
self.cursor.execute(query, parameters)
flight_id = self.cursor.lastrowid
except Exception as ex:
logging.error(error_message, exc_info=ex)
return
def processPositions(self, flight_id, aircraft):
# Get the ID of this aircraft.
hexcode = aircraft["hex"]
self.cursor.execute(self.STMTS['select_aircraft_id'], (hexcode,))
row = self.cursor.fetchone()
aircraft_id = row[0]
position_keys = ('lat', 'lon', 'nav_altitude', 'gs', 'track', 'geom_rate', 'hex')
if (all(key in aircraft for key in position_keys) and aircraft["altitude"] != "ground"):
self.process_positions(aircraft_id, flight_id, aircraft)
# Check that this message has not already been added to the database.
params = (flight_id, aircraft["messages"],)
self.cursor.execute(self.STMTS['select_position'], params)
row = self.cursor.fetchone()
return
if row == None or row[0] != aircraft["messages"]:
# Add this position to the database.
if 'squawk' in aircraft:
params = (flight_id, self.time_now, aircraft["messages"], aircraft["squawk"],
aircraft["lat"], aircraft["lon"], aircraft["track"],
aircraft["nav_altitude"], aircraft["geom_rate"], aircraft["gs"], aircraft_id,)
self.cursor.execute(self.STMTS['insert_position_sqwk'], params)
log("\t\t\tInserted position w/ Squawk " + repr(params))
else:
params = (flight_id, self.time_now, aircraft["messages"], aircraft["lat"], aircraft["lon"],
aircraft["track"], aircraft["nav_altitude"], aircraft["geom_rate"], aircraft["gs"], aircraft_id,)
self.cursor.execute(self.STMTS['insert_position'], params)
log("\t\t\tInserted position w/o Squawk " + repr(params))
else:
log("\t\t\tMessage is the same")
# Process positions
def process_positions(self, aircraft_id , flight_id, aircraft):
tracked=False
try:
self.cursor.execute("SELECT COUNT(*) FROM positions WHERE flight = %s AND message = %s", (flight_id, aircraft["messages"]))
if self.cursor.fetchone()[0] > 0:
tracked=True
except Exception as ex:
logging.error(f"Error encountered while checking if position has already been added for message ID '{aircraft["messages"]}' related to flight '{flight_id}'", exc_info=ex)
return
if tracked:
return
squawk = None
if 'squawk' in aircraft:
squawk = aircraft["squawk"]
try:
self.cursor.execute(
"INSERT INTO positions (flight, time, message, squawk, latitude, longitude, track, altitude, verticleRate, speed, aircraft) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(flight_id, now, aircraft["messages"], squawk, aircraft["lat"], aircraft["lon"], aircraft["track"], aircraft["nav_altitude"], aircraft["geom_rate"], aircraft["gs"], aircraft_id)
)
flight_id = self.cursor.lastrowid
except Exception as ex:
logging.error(f"Error encountered while inserting position data for message ID '{aircraft["messages"]}' related to flight '{flight_id}'", exc_info=ex)
return
return
if __name__ == "__main__":
processor = FlightsProcessor(config)
processor = AircraftProcessor()
# Main run loop
logging.info(f"Beginning flight recording job on {datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")}")
# Do not allow another instance of the job to run
lock_file = open('/tmp/flights.py.lock','w')
try:
fcntl.flock(lock_file, fcntl.LOCK_EX|fcntl.LOCK_NB)
except (IOError, OSError):
logging.info('Another instance already running')
quit()
# Begin flight recording job
lock_file.write('%d\n'%os.getpid())
while True:
# Read dump1090 aircraft.json.
response = urlopen('http://localhost/dump1090/data/aircraft.json')
data = json.load(response)
processor.processAircraftList(data["aircraft"])
log("Last Run: " + datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
time.sleep(15)
now = datetime.datetime.now()
data = processor.read_json()
processor.process_all_aircraft(data["aircraft"])
logging.info(f"Flight recording job ended on {datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")}")
time.sleep(15)

Wyświetl plik

@ -1,167 +1,173 @@
#!/usr/bin/python
#================================================================================#
# ADS-B FEEDER PORTAL #
# ------------------------------------------------------------------------------ #
# Copyright and Licensing Information: #
# #
# The MIT License (MIT) #
# #
# Copyright (c) 2015-2016 Joseph A. Prochazka #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
#================================================================================#
import datetime
import fcntl
import json
import logging
import os
import time
import yaml
# Do not allow another instance of the script to run.
lock_file = open('/tmp/flights.py.lock','w')
from datetime import datetime, timedelta
try:
fcntl.flock(lock_file, fcntl.LOCK_EX|fcntl.LOCK_NB)
except (IOError, OSError):
quit()
config = yaml.safe_load(open("config.yml"))
lock_file.write('%d\n'%os.getpid())
lock_file.flush()
class MaintenanceProcessor(object):
while True:
# Create database connection
def create_connection():
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
config = json.load(config_file)
## Read the configuration file.
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
config = json.load(config_file)
match config["database"]["type"].lower():
case 'mysql':
import mysql.connector
return mysql.connector.connect(
host=config["database"]["host"],
user=config["database"]["user"],
password=config["database"]["passwd"],
database=config["database"]["db"]
)
case 'sqlite':
import sqlite3
return sqlite3.connect(config["database"]["db"])
## Import the needed database library and set up database connection.
if config["database"]["type"] == "mysql":
import MySQLdb
db = MySQLdb.connect(host=config["database"]["host"], user=config["database"]["user"], passwd=config["database"]["passwd"], db=config["database"]["db"])
# Begin maintenance
def begin_maintenance(self):
connection = self.create_connection()
self.cursor = connection.cursor()
if config["database"]["type"] == "sqlite":
import sqlite3
db = sqlite3.connect(config["database"]["db"])
purge_old_aircraft = False
try:
self.cursor.execute("SELECT value FROM settings WHERE name 'purgeAircraft'")
result = self.cursor.fetchone()[0]
purge_old_aircraft = result.lower() in ['true', '1']
except Exception as ex:
logging.error(f"Error encountered while getting value for setting 'purgeAircraft'", exc_info=ex)
return
cursor = db.cursor()
if purge_old_aircraft:
cutoff_date = datetime.now() - timedelta(years = 20)
try:
self.cursor.execute("SELECT value FROM settings WHERE name 'purgeDaysOld'")
days_to_save = self.cursor.fetchone()[0]
except Exception as ex:
logging.error(f"Error encountered while getting value for setting 'purgeDaysOld'", exc_info=ex)
return
cutoff_date = datetime.now() - timedelta(days = days_to_save)
## Get maintenance settings.
self.purge_aircraft(cutoff_date)
self.purge_flights(cutoff_date)
self.purge_positions(cutoff_date)
purge_aircraft = False
# MySQL and SQLite
cursor.execute("SELECT value FROM adsb_settings WHERE name = 'purgeAircraft'")
row = cursor.fetchone()
if row:
purge_aircraft = row
connection.commit()
connection.close()
purge_flights = False
# MySQL and SQLite
cursor.execute("SELECT value FROM adsb_settings WHERE name = 'purgeFlights'")
row = cursor.fetchone()
if row:
purge_flights = row
return
purge_positions = False
# MySQL and SQLite
cursor.execute("SELECT value FROM adsb_settings WHERE name = 'purgePositions'")
row = cursor.fetchone()
if row:
purge_positions = row
# Remove aircraft not seen since the specified date
def purge_aircraft(self, cutoff_date):
try:
self.cursor.execute("SELECT id FROM aircraft WHERE lastSeen < %s", (cutoff_date,))
aircraft_ids = self.cursor.fetchall()
except Exception as ex:
logging.error(f"Error encountered while getting aircraft IDs not seen since '{cutoff_date}'", exc_info=ex)
return
purge_days_old = False
# MySQL and SQLite
cursor.execute("SELECT value FROM adsb_settings WHERE name = 'purgeDaysOld'")
row = cursor.fetchone()
if row:
purge_days_old = row
if len(aircraft_ids) > 0:
id = tuple(aircraft_ids)
aircraft_id_params = {'id': id}
## Create the purge date from the age specified.
try:
self.cursor.execute("DELETE FROM aircraft WHERE id IN %(t)s", aircraft_id_params)
except Exception as ex:
logging.error(f"Error deleting aircraft not seen since '{cutoff_date}'", exc_info=ex)
return
self.purge_flights_related_to_aircraft(aircraft_id_params, cutoff_date)
self.purge_positions_related_to_aircraft(aircraft_id_params, cutoff_date)
if purge_days_old:
purge_datetime = datetime.datetime.utcnow() - timedelta(days=purge_days_old)
purge_date = purge_datetime.strftime("%Y/%m/%d %H:%M:%S")
else:
purge_datetime = None
purge_date = None
return
## Remove aircraft not seen since the specified date.
# Remove flights related to aircraft not seen since the specified date
def purge_flights_related_to_aircraft(self, aircraft_id_params, cutoff_date):
try:
self.cursor.execute("DELETE FROM flights WHERE aircraft = %(t)s", aircraft_id_params)
except Exception as ex:
logging.error(f"Error deleting flights related to aircraft not seen since '{cutoff_date}'", exc_info=ex)
return
return
if purge_aircraft and purge_date:
# MySQL
if config["database"]["type"] == "mysql":
cursor.execute("SELECT id FROM adsb_aircraft WHERE lastSeen < %s", purge_date)
rows = cursor.fetchall()
for row in rows:
cursor.execute("DELETE FROM adsb_positions WHERE aircraft = %s", row[0])
cursor.execute("DELETE FROM adsb_flights WHERE aircraft = %s", row[0])
cursor.execute("DELETE FROM adsb_aircraft WHERE id = %s", row[0])
# Remove positions related to aircraft not seen since the specified date
def purge_positions_related_to_aircraft(self, aircraft_id_params, cutoff_date):
try:
self.cursor.execute("DELETE FROM positions WHERE aircraft = %(t)s", aircraft_id_params)
except Exception as ex:
logging.error(f"Error deleting positions related to aircraft not seen since '{cutoff_date}'", exc_info=ex)
return
return
# Remove positions older than the specified date
def purge_flights(self, cutoff_date):
try:
self.cursor.execute("SELECT id FROM flights WHERE lastSeen < %s", (cutoff_date,))
flight_ids = self.cursor.fetchall()
except Exception as ex:
logging.error(f"Error encountered while getting aircraft IDs not seen since '{cutoff_date}'", exc_info=ex)
return
# SQLite
if config["database"]["type"] == "sqlite":
params = (purge_date,)
cursor.execute("SELECT id FROM adsb_aircraft WHERE lastSeen < ?", params)
rows = cursor.fetchall()
for row in rows:
params = (row[0],)
cursor.execute("DELETE FROM adsb_positions WHERE aircraft = ?", params)
cursor.execute("DELETE FROM adsb_flights WHERE aircraft = ?", params)
cursor.execute("DELETE FROM adsb_aircraft WHERE id = ?", params)
if len(flight_ids) > 0:
id = tuple(flight_ids)
flight_id_params = {'id': id}
## Remove flights not seen since the specified date.
try:
self.cursor.execute("DELETE FROM flights WHERE id IN %(t)s", flight_id_params)
except Exception as ex:
logging.error(f"Error deleting flights older than the cut off date of '{cutoff_date}'", exc_info=ex)
return
self.purge_positions_related_to_flights(flight_id_params, cutoff_date)
if purge_flights and purge_date:
# MySQL
if config["database"]["type"] == "mysql":
cursor.execute("SELECT id FROM adsb_flights WHERE lastSeen < %s", purge_date)
rows = cursor.fetchall()
for row in rows:
cursor.execute("DELETE FROM adsb_positions WHERE flight = %s", row[0])
cursor.execute("DELETE FROM adsb_flights WHERE id = %s", row[0])
return
# Remove positions related to aircraft not seen since the specified date
def purge_positions_related_to_flights(self, flight_id_params, cutoff_date):
try:
self.cursor.execute("DELETE FROM positions WHERE flight = %(t)s", flight_id_params)
except Exception as ex:
logging.error(f"Error deleting positions related to flights not seen since '{cutoff_date}'", exc_info=ex)
return
return
#SQLite
if config["database"]["type"] == "sqlite":
params = (purge_date,)
cursor.execute("SELECT id FROM adsb_flights WHERE lastSeen < ?", params)
rows = cursor.fetchall()
for row in rows:
params = (row[0],)
cursor.execute("DELETE FROM adsb_positions WHERE flight = ?", params)
cursor.execute("DELETE FROM adsb_flights WHERE id = ?", params)
# Remove positions older than the specified date
def purge_positions(self, cutoff_date):
try:
self.cursor.execute("DELETE FROM positions WHERE time < %s", (cutoff_date,))
except Exception as ex:
logging.error(f"Error deleting positions older than the cut off date of '{cutoff_date}'", exc_info=ex)
return
return
if __name__ == "__main__":
processor = MaintenanceProcessor()
## Remove positions older than the specified date.
logging.info(f"Beginning maintenance job on {datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")}")
if purge_positions and purge_date:
# MySQL
if config["database"]["type"] == "mysql":
cursor.execute("DELETE FROM adsb_positions WHERE time < %s", purge_date)
#SQLite
if config["database"]["type"] == "sqlite":
params = (purge_date,)
cursor.execute("DELETE FROM adsb_positions WHERE time < ?", params)
## Close the database connection.
db.commit()
db.close()
## Sleep until the next run.
time.sleep(3600)
# Do not allow another instance of the job to run
lock_file = open('/tmp/maintenance.py.lock','w')
try:
fcntl.flock(lock_file, fcntl.LOCK_EX|fcntl.LOCK_NB)
except (IOError, OSError):
logging.info('Another instance already running')
quit()
# Begin maintenance job
lock_file.write('%d\n'%os.getpid())
while True:
processor.begin_maintenance()
logging.info(f"Maintenance job ended on {datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")}")
time.sleep(15)