auto-sync: 2026-04-26 17:10:01
This commit is contained in:
@@ -621,69 +621,28 @@ def api_air_corridors():
|
||||
|
||||
@app.route("/api/tracks", methods=["GET"])
|
||||
def get_tracks():
|
||||
"""Треки из PostgreSQL в формате GeoJSON"""
|
||||
date_filter = request.args.get("date") # YYYY-MM-DD
|
||||
"""Треки через fr24-api (proxy)"""
|
||||
import requests as req
|
||||
date_filter = request.args.get("date")
|
||||
limit = min(int(request.args.get("limit", 500)), 2000)
|
||||
|
||||
fr24_api_base = os.getenv("FR24_API_BASE", "http://192.168.2.67:8080")
|
||||
params = {"limit": limit}
|
||||
if date_filter:
|
||||
params["date"] = date_filter
|
||||
else:
|
||||
params["minutes"] = 4320 # 3 days
|
||||
|
||||
try:
|
||||
conn = get_db_conn()
|
||||
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
|
||||
where = "WHERE 1=1"
|
||||
params = []
|
||||
if date_filter:
|
||||
where += " AND DATE(tp.observed_at) = %s"
|
||||
params.append(date_filter)
|
||||
|
||||
cur.execute(f"""
|
||||
SELECT
|
||||
t.track_id,
|
||||
t.flight_id,
|
||||
t.point_count,
|
||||
t.min_altitude_m,
|
||||
t.max_altitude_m,
|
||||
json_agg(
|
||||
json_build_array(ST_X(tp.geom), ST_Y(tp.geom))
|
||||
ORDER BY tp.point_order
|
||||
) as coords
|
||||
FROM fr24.tracks t
|
||||
JOIN fr24.track_points tp ON tp.track_id = t.track_id
|
||||
{where}
|
||||
GROUP BY t.track_id, t.flight_id, t.point_count, t.min_altitude_m, t.max_altitude_m
|
||||
ORDER BY t.track_id
|
||||
LIMIT %s
|
||||
""", params + [limit])
|
||||
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
features = []
|
||||
for row in rows:
|
||||
features.append({
|
||||
"type": "Feature",
|
||||
"geometry": {
|
||||
"type": "LineString",
|
||||
"coordinates": row["coords"]
|
||||
},
|
||||
"properties": {
|
||||
"track_id": row["track_id"],
|
||||
"flight_id": row["flight_id"],
|
||||
"point_count": row["point_count"],
|
||||
"min_altitude_m": float(row["min_altitude_m"]) if row["min_altitude_m"] else None,
|
||||
"max_altitude_m": float(row["max_altitude_m"]) if row["max_altitude_m"] else None,
|
||||
}
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
"type": "FeatureCollection",
|
||||
"features": features,
|
||||
"meta": {"count": len(features), "date": date_filter}
|
||||
})
|
||||
|
||||
resp = req.get(f"{fr24_api_base}/api/tracks", params=params, timeout=10)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
# fr24-api возвращает {ok: true, data: {type: FeatureCollection, ...}}
|
||||
geojson = data.get("data", data)
|
||||
return jsonify(geojson)
|
||||
except Exception as e:
|
||||
logger.error(f"/api/tracks error: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
logger.error(f"/api/tracks proxy error: {e}")
|
||||
return jsonify({"error": str(e), "type": "FeatureCollection", "features": []}), 500
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user