-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
184 lines (143 loc) · 4.49 KB
/
app.py
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
from flask import Flask, jsonify, render_template
from sqlalchemy import create_engine
import pandas as pd
import os
import json
# # Import database user and password
# from api_keys import mysql_hostname
# from api_keys import mysql_port
# from api_keys import mysql_user_project2
# from api_keys import mysql_pass_project2
# mysql_hostname = os.environ['MYSQL_HOSTNAME']
# print(mysql_hostname)
# mysql_port = os.environ['MYSQL_PORT']
# print(mysql_port)
# mysql_user_project2 = os.environ['MYSQL_USERNAME']
# mysql_pass_project2 = os.environ['MYSQL_PASSWORD']
# Database name and database tables
database_name = "project_2"
table_airplanes = "aircraft_data"
table_airports = "airport_data"
# MySQL specific connection string
try:
database_uri = os.environ['DATABASE_URL']
except KeyError:
database_uri = f"mysql+mysqlconnector://{mysql_user_project2}:{mysql_pass_project2}@{mysql_hostname}:{mysql_port}/{database_name}"
print(database_uri)
# Create the engine to connect to the database
engine = create_engine(database_uri)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
# Use Flask to create your routes.
# Route
# Home page.
@app.route("/")
def welcome():
return render_template("index_Gabriel.html")
# Return the APIs route available
@app.route("/api/v1.0/")
def api_routes():
return (
f"<h3>API routes available:</h3>"
f"/aircrafts-data<br/>"
f"/airports-data<br/>"
f"/api/v1.0/aircrafts-data/icao24/<icao24><br/>"
f"/api/v1.0/aircrafts-data/callsign/<callsign><br/>"
f"/api/v1.0/aircrafts-data/byhour"
)
# Return a json with the query results for the aircrafts table
@app.route("/api/v1.0/aircrafts-data")
def api_aircrafts():
# MySQL query to return all table elements that have not null latitute and have the newest time stamp
aircraft_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airplanes}
WHERE
longitude IS NOT NULL
AND
time = (SELECT MAX(time)
FROM
{table_airplanes})
ORDER BY
id
DESC;
""",
engine)
result = aircraft_df.to_json(orient="records")
parsed = json.loads(result)
return jsonify(parsed)
# Return a json with the query results for the airports table
@app.route("/api/v1.0/airports-data")
def api_airports():
airports_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airports}
ORDER BY
AirportID;
""",
engine)
result = airports_df.to_json(orient="records")
parsed = json.loads(result)
return jsonify(parsed)
# Return a json with the query results for the aircrafts table for a specific icao24
@app.route("/api/v1.0/aircrafts-data/icao24/<icao24>")
def api_aircrafts_icao24(icao24):
aircraft_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airplanes}
WHERE
icao24 = '{str(icao24)}';
""",
engine)
result = aircraft_df.to_json(orient="records")
parsed = json.loads(result)
# Return a json with the query results for the aircrafts table for a specific callsign
@app.route("/api/v1.0/aircrafts-data/callsign/<callsign>")
def api_aircrafts_callsign(callsign):
aircraft_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airplanes}
WHERE
callsign = '{str(callsign)}';
""",
engine)
result = aircraft_df.to_json(orient="records")
parsed = json.loads(result)
return jsonify(parsed)
# Return a json with the query results for the aircrafts table for a specific callsign
@app.route("/api/v1.0/aircrafts-data/byhour")
def api_aircrafts_byhour():
aircraft_hour = pd.read_sql(
f"""
SELECT
COUNT(*) AS totalDataPoints,
FROM_UNIXTIME(time, '%Y-%m-%d %H') AS timeData
FROM
{table_airplanes}
GROUP BY FROM_UNIXTIME(time, '%Y-%m-%d %H');
""",
engine)
result = aircraft_hour.to_json(orient="records")
parsed = json.loads(result)
return jsonify(parsed)
# The server is set to run on the computer IP address on the port 5100
# Go to your http://ipaddress:5100
if __name__ == "__main__":
app.run(debug=True)