-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
178 lines (147 loc) · 5.07 KB
/
database.py
File metadata and controls
178 lines (147 loc) · 5.07 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
import time
from collections import Counter
from pytz import timezone
import requests
import psycopg2
from datetime import datetime
# OpenWeatherMap API Configuration
API_KEY = "7ef4fb0d4778af5be6af867af0f0fcb7"
URL = f"https://api.openweathermap.org/data/3.0/onecall?lat=43.7&lon=-79.42&exclude=hourly,daily&appid={API_KEY}&units=metric"
# Database configuration
DB_CONFIG = {
"dbname": "neondb",
"user": "neondb_owner",
"password": "npg_wcnb1VdW5ELa",
"host": "ep-square-band-a53at0wv-pooler.us-east-2.aws.neon.tech",
"port": "5432",
"sslmode": "require"
}
# Utility to get DB connection
def get_db_connection():
return psycopg2.connect(**DB_CONFIG)
# Weather data
def get_latest_weather():
try:
api_key = "dd1b05ad0a13c55a14d14964ce36bed0"
city = "Toronto"
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
if data["cod"] != 200:
print("Weather API error:", data)
return None
toronto = timezone("America/Toronto")
timestamp = datetime.fromtimestamp(data["dt"], toronto).strftime("%Y-%m-%d %H:%M:%S")
weather = {
"city": data["name"],
"temperature": data["main"]["temp"],
"precipitation": data.get("rain", {}).get("1h", 0.0),
"timestamp": timestamp
}
return weather
except Exception as e:
print("Error fetching real-time weather:", e)
return None
# Get park reports
def get_reports():
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
SELECT parks.park_name, park_reports.report_type, park_reports.details, park_reports.date, park_reports.status, park_reports.photo, park_reports.id
FROM park_reports
JOIN parks ON park_reports.park = parks.id
ORDER BY park_reports.date DESC
""")
rows = cur.fetchall()
cur.close()
conn.close()
return rows
except Exception as e:
print(f"Error fetching reports: {e}")
return []
# Get maintenance tasks
def get_tasks():
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM maintenance_tasks")
tasks = cur.fetchall()
cur.close()
conn.close()
return tasks
except Exception as e:
print("Error fetching tasks:", e)
return []
def get_task_summary(tasks, parks):
park_id_to_name = {park[0]: park[1] for park in parks}
task_counts = {}
for task in tasks:
park_id = task[3]
park_name = park_id_to_name.get(park_id, None)
if park_name:
task_counts[park_name] = task_counts.get(park_name, 0) + 1
# else skip unknown parks
return task_counts
#get weather for last 7 days
def weather_data_7days():
try:
conn = get_db_connection()
cur = conn.cursor()
# Get all weather data
cur.execute("SELECT * FROM weather_data ORDER BY timestamp DESC")
rows = cur.fetchall()
# Close database connection
cur.close()
conn.close()
# Filter rows to only include today's data
today = datetime.now().date()
filtered_rows = [row for row in rows
if (row[1].date() - today).days <= 7 and row[1].date() >= today
]
return filtered_rows
except Exception as e:
print(f"Error fetching weather data: {e}")
return []
def weather_data_24hours():
try:
conn = get_db_connection()
cur = conn.cursor()
# Get all weather data
cur.execute("SELECT * FROM weather_data ORDER BY timestamp DESC")
rows = cur.fetchall()
# Close database connection
cur.close()
conn.close()
# Filter rows to only include today's data
today = datetime.now().date()
filtered_rows = [row for row in rows if row[1].date() == today]
return filtered_rows
except Exception as e:
print(f"Error fetching weather data: {e}")
return []
def get_parks():
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM parks")
parks = cur.fetchall()
cur.close()
conn.close()
return parks
except Exception as e:
print("Error fetching parks:", e)
return []
def update_park(id, plow_paths, water_flowers, cut_grass, high_winds, heavy_rain, heavy_snow):
try:
conn = get_db_connection()
cur = conn.cursor()
cur.execute("UPDATE parks SET plow_paths = %s, water_flowers = %s, cut_grass = %s, high_winds = %s, heavy_rain = %s, heavy_snow = %s WHERE id = %s",
(plow_paths, water_flowers, cut_grass, high_winds, heavy_rain, heavy_snow, id))
conn.commit()
cur.close()
conn.close()
return True
except Exception as e:
print("Error updating park:", e)
return False