Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
208 lines (178 loc) · 6.27 KB
/
Copy pathtest.py
File metadata and controls
208 lines (178 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import datetime
import httpx
import psycopg2
from typing import Optional
from fastapi import FastAPI, HTTPException, Query
from apscheduler.schedulers.background import BackgroundScheduler
from psycopg2.extras import RealDictCursor
from dotenv import load_dotenv
import os
load_dotenv()
dbuser = os.getenv("DBUSER")
dbpassword = os.getenv("DBPASSWORD")
print(f"DBUSER: {dbuser}")
print(f"DBPASSWORD: {dbpassword}")
app = FastAPI()
pg_conn_info = f"dbname=aircraft user={dbuser} password={dbpassword} host=localhost port=5432"
def get_pg_conn():
return psycopg2.connect(pg_conn_info)
def initialize_pg_db():
conn = get_pg_conn()
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS aircraft(
hex TEXT,
lat REAL,
lon REAL,
flight TEXT,
squawk TEXT,
altitude REAL,
timestamp TEXT,
category TEXT,
heading REAL,
PRIMARY KEY (hex, timestamp)
)
""")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_timestamp ON aircraft(timestamp)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_hex ON aircraft(hex)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_flight ON aircraft(flight)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_category ON aircraft(category)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_altitude ON aircraft(altitude)")
cursor.execute("CREATE INDEX IF NOT EXISTS idx_latlon ON aircraft(lat, lon)")
conn.commit()
conn.close()
def insert_to_postgresql(data):
conn = get_pg_conn()
cursor = conn.cursor()
insert_sql = """
INSERT INTO aircraft (hex, lat, lon, flight, squawk, altitude, timestamp, category, heading)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (hex, timestamp) DO UPDATE SET
lat = EXCLUDED.lat,
lon = EXCLUDED.lon,
flight = EXCLUDED.flight,
squawk = EXCLUDED.squawk,
altitude = EXCLUDED.altitude,
category = EXCLUDED.category,
heading = EXCLUDED.heading
"""
for row in data:
cursor.execute(insert_sql, row)
conn.commit()
conn.close()
def put_positions_live():
url = os.getenv("ip")
try:
response = httpx.get(url, timeout=5.0)
response.raise_for_status()
except Exception as e:
print(f"Fetch error: {e}")
return
data = response.json()
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
rows = []
for aircraft in data.get("aircraft", []):
if "lat" not in aircraft or "lon" not in aircraft:
continue
rows.append((
aircraft.get("hex", "N/A"),
aircraft["lat"],
aircraft["lon"],
aircraft.get("flight", "N/A"),
aircraft.get("squawk", "N/A"),
aircraft.get("alt_baro", 0),
now,
aircraft.get("category", "N/A"),
aircraft.get("track", 0)
))
if rows:
insert_to_postgresql(rows)
@app.on_event("startup")
def on_startup():
initialize_pg_db()
scheduler = BackgroundScheduler()
scheduler.add_job(put_positions_live, "interval", seconds=1)
scheduler.start()
@app.get("/")
def root():
return {"message": "Hello World"}
@app.get("/positions/live")
def get_positions_live():
url = os.getenv("ip")
try:
response = httpx.get(url, timeout=5.0)
response.raise_for_status()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to fetch data: {e}")
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data = response.json()
aircraft_list = []
for a in data.get("aircraft", []):
if a.get("lat") is None or a.get("lon") is None:
continue
aircraft_list.append({
"hex": a.get("hex", "N/A"),
"lat": a["lat"],
"lon": a["lon"],
"flight": a.get("flight", "N/A"),
"squawk": a.get("squawk", "N/A"),
"altitude": a.get("alt_baro", 0),
"timestamp": now,
"category": a.get("category", "N/A"),
"heading": a.get("track", 0)
})
return {"aircraft": aircraft_list}
@app.get("/positions/history")
def get_positions_history(
start: Optional[datetime.datetime] = Query(datetime.datetime.now() - datetime.timedelta(days=1)),
end: Optional[datetime.datetime] = Query(datetime.datetime.now()),
hex_code: Optional[str] = None,
flight: Optional[str] = None,
squawk: Optional[str] = None,
altitude_min: Optional[float] = None,
altitude_max: Optional[float] = None,
lat_min: Optional[float] = None,
lat_max: Optional[float] = None,
lon_min: Optional[float] = None,
lon_max: Optional[float] = None,
category: Optional[str] = None,
heading: Optional[float] = None,
):
if any([lat_min, lat_max, lon_min, lon_max]) and not all([lat_min, lat_max, lon_min, lon_max]):
raise HTTPException(status_code=400, detail="All coordinate bounds must be provided if one is used.")
filters = ["timestamp BETWEEN %s AND %s"]
params = [start.strftime("%Y-%m-%d %H:%M:%S"), end.strftime("%Y-%m-%d %H:%M:%S")]
if hex_code:
filters.append("hex = %s")
params.append(hex_code)
if flight:
filters.append("flight ILIKE %s")
params.append(f"%{flight}%")
if squawk:
filters.append("squawk = %s")
params.append(squawk)
if category:
filters.append("category = %s")
params.append(category)
if heading is not None:
filters.append("heading = %s")
params.append(heading)
if altitude_min is not None:
filters.append("altitude >= %s")
params.append(altitude_min)
if altitude_max is not None:
filters.append("altitude <= %s")
params.append(altitude_max)
if lat_min is not None:
filters.append("lat BETWEEN %s AND %s")
params.extend([lat_min, lat_max])
if lon_min is not None:
filters.append("lon BETWEEN %s AND %s")
params.extend([lon_min, lon_max])
query = f"SELECT * FROM aircraft WHERE {' AND '.join(filters)}"
conn = get_pg_conn()
cursor = conn.cursor(cursor_factory=RealDictCursor)
cursor.execute(query, params)
results = cursor.fetchall()
conn.close()
return {"results": results}