-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
224 lines (170 loc) · 6.27 KB
/
Copy pathdatabase.py
File metadata and controls
224 lines (170 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import psycopg2
import streamlit as st
class DB:
def __init__(self):
self.conn = None
self.columns = None
self.connect()
def connect(self):
try:
self.conn = psycopg2.connect(
host=st.secrets["DB_HOST"],
user=st.secrets["DB_USER"],
password=st.secrets["DB_PASSWORD"],
dbname=st.secrets["DB_NAME"],
port=st.secrets["DB_PORT"],
sslmode="require"
)
self.conn.autocommit = True
st.success("Database Connected Successfully")
except Exception as e:
st.error(f"Database Connection Error: {e}")
raise e
def _ensure_connection(self):
if self.conn is None or self.conn.closed:
self.connect()
@staticmethod
def _quote_identifier(identifier):
return '"' + identifier.replace('"', '""') + '"'
def _load_columns(self):
query = """
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'flights'
"""
try:
self.conn.rollback()
with self.conn.cursor() as cursor:
cursor.execute(query)
columns = [row[0] for row in cursor.fetchall()]
except psycopg2.Error as e:
if self.conn and not self.conn.closed:
self.conn.rollback()
detail = e.pgerror.strip() if e.pgerror else str(e)
raise RuntimeError(f"Could not read database schema: {detail}") from e
if not columns:
raise RuntimeError('Table "public.flights" was not found in the connected database')
self.columns = {column.lower(): column for column in columns}
def _column(self, name):
if self.columns is None:
self._load_columns()
column = self.columns.get(name.lower())
if column is None:
available = ", ".join(sorted(self.columns.values()))
raise RuntimeError(f'Column "{name}" was not found in flights. Available columns: {available}')
return self._quote_identifier(column)
def _fetchall(self, query, params=None):
self._ensure_connection()
try:
self.conn.rollback()
with self.conn.cursor() as cursor:
cursor.execute(query, params)
return cursor.fetchall()
except psycopg2.Error as e:
if self.conn and not self.conn.closed:
self.conn.rollback()
detail = e.pgerror.strip() if e.pgerror else str(e)
raise RuntimeError(f"Database query failed: {detail}") from e
# ============================
# FETCH CITY NAMES
# ============================
def fetch_city_names(self):
destination_col = self._column("Destination")
source_col = self._column("Source")
query = f"""
SELECT DISTINCT {destination_col} FROM flights
UNION
SELECT DISTINCT {source_col} FROM flights
"""
data = self._fetchall(query)
return [item[0] for item in data]
def fetch_source_names(self):
source_col = self._column("Source")
query = f"""
SELECT DISTINCT TRIM({source_col})
FROM flights
WHERE {source_col} IS NOT NULL
ORDER BY TRIM({source_col})
"""
data = self._fetchall(query)
return [item[0] for item in data]
def fetch_destinations_for_source(self, source):
source_col = self._column("Source")
destination_col = self._column("Destination")
query = f"""
SELECT DISTINCT TRIM({destination_col})
FROM flights
WHERE TRIM({source_col}) = TRIM(%s)
AND {destination_col} IS NOT NULL
ORDER BY TRIM({destination_col})
"""
data = self._fetchall(query, (source,))
return [item[0] for item in data]
# ============================
# FETCH FLIGHTS
# ============================
def fetch_all_flights(self, source, destination):
airline_col = self._column("Airline")
route_col = self._column("Route")
dep_time_col = self._column("Dep_Time")
duration_col = self._column("Duration")
price_col = self._column("Price")
source_col = self._column("Source")
destination_col = self._column("Destination")
query = f"""
SELECT {airline_col}, {route_col}, {dep_time_col}, {duration_col}, {price_col}
FROM flights
WHERE TRIM({source_col}) = TRIM(%s)
AND TRIM({destination_col}) = TRIM(%s)
"""
return self._fetchall(query, (source, destination))
# ============================
# AIRLINE FREQUENCY
# ============================
def fetch_airline_frequency(self):
airline_col = self._column("Airline")
query = f"""
SELECT {airline_col}, COUNT(*)
FROM flights
GROUP BY {airline_col}
"""
data = self._fetchall(query)
airline = [item[0] for item in data]
frequency = [item[1] for item in data]
return airline, frequency
# ============================
# BUSY AIRPORTS
# ============================
def busy_airport(self):
source_col = self._column("Source")
destination_col = self._column("Destination")
query = f"""
SELECT city, COUNT(*) FROM (
SELECT {source_col} AS city FROM flights
UNION ALL
SELECT {destination_col} AS city FROM flights
) t
GROUP BY city
ORDER BY COUNT(*) DESC
"""
data = self._fetchall(query)
city = [item[0] for item in data]
frequency = [item[1] for item in data]
return city, frequency
# ============================
# DAILY FLIGHT TREND
# ============================
def daily_frequency(self):
date_col = self._column("Date_of_Journey")
query = f"""
SELECT {date_col}, COUNT(*)
FROM flights
GROUP BY {date_col}
"""
data = self._fetchall(query)
date = [item[0] for item in data]
frequency = [item[1] for item in data]
return date, frequency
def close(self):
if self.conn and not self.conn.closed:
self.conn.close()