-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
285 lines (249 loc) · 10.2 KB
/
database.py
File metadata and controls
285 lines (249 loc) · 10.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import math
from datetime import datetime
from typing import Any
import mysql.connector
import pandas as pd
def _convert_data_to_df(query_data: list) -> pd.DataFrame:
"""Convert query data to pandas dataframe.
:param query_data: List of query data.
:return: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir",
"gusts"
"""
df = pd.DataFrame(query_data,
columns=["id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir", "gusts"])
df["temp"] = pd.to_numeric(df["temp"], errors="coerce")
df["rain"] = pd.to_numeric(df["rain"], errors="coerce")
df["wind"] = pd.to_numeric(df["wind"], errors="coerce")
df["gusts"] = pd.to_numeric(df["gusts"], errors="coerce")
return df
def _to_none(value: Any) -> float | None:
"""Convert values that aren't float or int to None so that they can be entered into the database.
:param value: Value to be converted.
:return: None or the value
"""
if isinstance(value, float) and math.isnan(value):
return None
return value
class Database:
"""Database connector for weatherlogging"""
def __init__(self, host: str, user: str, pw: str, db: str) -> None:
"""Create new instance of Database.
:param host: database host ip or "localhost"
:param user: database user
:param pw: password for database user
:param db: database name
:return: None
"""
self.mydb = mysql.connector.connect(
host=host,
user=user,
password=pw,
database=db
)
self.cursor = self.mydb.cursor()
def add_coords(self, id_: int, lat: float, lon: float, print_debug: bool = False) -> None:
"""Add new entry to the coords-table.
Expected types are hinted, the coordinate format is decimal degrees (Berlin would be 52.5200, 13.4050)
:param id_:
:param print_debug:
:param lon:
:param lat:
"""
query = """
INSERT INTO coords (id, lat, lon)
VALUES (%s, %s, %s);
"""
values = (id_, lat, lon)
if print_debug: print(query, values)
self.cursor.execute(query, values)
self.mydb.commit()
def add_coords_from_df(self, df: pd.DataFrame, print_debug: bool = False) -> None:
"""Add the contents of the dataframe to the database.
:param df: pandas.DataFrame with columns "id", "lat", "lon"
:param print_debug: Print the query and values for debugging purposes
:return:
"""
query = """
INSERT INTO coords (id, lat, lon)
VALUES (%s, %s, %s)
"""
values = []
for _, row in df.iterrows():
cleaned = (
row["id"],
row["lat"],
row["lon"],
)
values.append(cleaned)
if print_debug: print(query, values)
self.cursor.executemany(query, values)
self.mydb.commit()
def add_data(
self,
id_: int,
time: datetime,
temp: float,
humidity: int,
clouds: int,
rain: float,
wind: float,
wind_dir: int,
gusts: float,
print_debug: bool = False
) -> None:
"""Add new entry to the data-table.
"nan" gets converted to "NULL" for the database.
:param id_: foreign key id (id from coords-table)
:param time: timestamp for this entry
:param temp: temperature in degrees C
:param humidity: humidity in %
:param clouds: cloud coverage in %
:param rain: rain in mm
:param wind: wind speed in km/h
:param wind_dir: wind direction in degrees
:param gusts: gust speeds in km/h
:param print_debug: Print the query and values for debugging purposes
:return:
"""
temp = _to_none(temp)
humidity = _to_none(humidity)
clouds = _to_none(clouds)
rain = _to_none(rain)
wind = _to_none(wind)
wind_dir = _to_none(wind_dir)
gusts = _to_none(gusts)
query = """
INSERT INTO data (id, time, temp, humidity, clouds, rain, wind, wind_dir, gusts)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = (id_, time, temp, humidity, clouds, rain, wind, wind_dir, gusts)
if print_debug: print(query, values)
self.cursor.execute(query, values)
self.mydb.commit()
def add_data_from_df(self, df: pd.DataFrame, print_debug: bool = False) -> None:
"""Add the contents of the dataframe to the database.
:param df: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir", "gusts"
:param print_debug: Print the query and values for debugging purposes
:return:
"""
query = """
INSERT INTO data (id, time, temp, humidity, clouds, rain, wind, wind_dir, gusts)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = []
for _, row in df.iterrows():
cleaned = (
row["id"],
row["time"],
_to_none(row["temp"]),
_to_none(row["humidity"]),
_to_none(row["clouds"]),
_to_none(row["rain"]),
_to_none(row["wind"]),
_to_none(row["wind_dir"]),
_to_none(row["gusts"]),
)
values.append(cleaned)
if print_debug: print(query, values)
self.cursor.executemany(query, values)
self.mydb.commit()
def get_coords(self, print_debug: bool = False) -> pd.DataFrame:
"""Read the coord ids from the database.
:return: pandas.DataFrame with columns "id", "lat", "lon"
"""
print(f"Getting coords...")
query = """
SELECT * FROM coords
"""
self.cursor.execute(query)
df = pd.DataFrame(self.cursor.fetchall(), columns=["id", "lat", "lon"])
df["lat"] = pd.to_numeric(df["lat"], errors="coerce")
df["lon"] = pd.to_numeric(df["lon"], errors="coerce")
self.mydb.commit()
if print_debug: print(df)
return df
def get_coords_from_id(self, id_: int, print_debug: bool = False) -> pd.DataFrame:
"""Reads the coords for a specific id from the database
:param id_:
:param print_debug: Print the dataframe being returned for debugging purposes
:return: pandas.DataFrame with columns "id", "lat", "lon" containing one row
"""
print(f"Getting coords for id {id_}...")
query = """
SELECT * FROM coords WHERE id = %s
"""
values = (id_,)
self.cursor.execute(query, values)
df = pd.DataFrame(self.cursor.fetchall(), columns=["id", "lat", "lon"])
df["lat"] = pd.to_numeric(df["lat"], errors="coerce")
df["lon"] = pd.to_numeric(df["lon"], errors="coerce")
self.mydb.commit()
if print_debug: print(df)
return df
def get_data(self, print_debug: bool = False) -> pd.DataFrame:
"""Reads the data from the database
:param print_debug: Print the dataframe being returned for debugging purposes
:return: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir",
"gusts"
"""
print(f"Getting data... (this might take a while)")
query = """
SELECT * FROM data
"""
self.cursor.execute(query)
df = _convert_data_to_df(self.cursor.fetchall())
self.mydb.commit()
if print_debug: print(df)
return df
def get_data_from_id(self, id_: int, print_debug: bool = False) -> pd.DataFrame:
"""Reads the data for a specific id from the database
:param id_:
:param print_debug: Print the dataframe being returned for debugging purposes
:return: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir",
"gusts"
"""
print(f"Getting data for id {id_}...")
query = f"SELECT * FROM data WHERE id = {id_}"
self.cursor.execute(query)
df = _convert_data_to_df(self.cursor.fetchall())
self.mydb.commit()
if print_debug: print(df)
return df
def get_data_from_datetime(self, time: datetime, print_debug: bool = False) -> pd.DataFrame:
"""Reads the data for a specific time from the database
:param time: Specific time you want the data from
:param print_debug: Print the dataframe being returned for debugging purposes
:return: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir",
"gusts"
"""
print(f"Getting data from {time}...")
query = """
SELECT * FROM data WHERE time = %s
"""
values = (time,)
self.cursor.execute(query, values)
df = _convert_data_to_df(self.cursor.fetchall())
self.mydb.commit()
if print_debug: print(df)
return df
def get_data_between_datetimes(self, start: datetime, end: datetime, print_debug: bool = False) -> pd.DataFrame:
"""Reads the data for a specific time period from the database
:param start: start of the timeframe
:param end: end of the timeframe
:param print_debug: Print the dataframe being returned for debugging purposes
:return: pandas.DataFrame with columns "id", "time", "temp", "humidity", "clouds", "rain", "wind", "wind_dir",
"gusts"
"""
print(f"Getting data between {start} and {end}...")
query = """
SELECT * FROM data WHERE time >= %s AND time <= %s
"""
values = (start, end)
self.cursor.execute(query, values)
df = _convert_data_to_df(self.cursor.fetchall())
self.mydb.commit()
if print_debug: print(df)
return df
def close(self) -> None:
"""Closes the connection to the database"""
self.mydb.close()