Auto-update: Tue Aug 6 21:41:49 PDT 2024
This commit is contained in:
parent
2ad5c527f0
commit
d268805253
2 changed files with 42 additions and 34 deletions
|
@ -836,18 +836,23 @@ class APIConfig(BaseModel):
|
||||||
conn = await self.get_connection()
|
conn = await self.get_connection()
|
||||||
if conn is None:
|
if conn is None:
|
||||||
raise ConnectionError("Failed to connect to local database")
|
raise ConnectionError("Failed to connect to local database")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if table_name in self.SPECIAL_TABLES:
|
if table_name in self.SPECIAL_TABLES:
|
||||||
return await self._execute_special_table_write(conn, query, *args, table_name=table_name)
|
return await self._execute_special_table_write(conn, query, *args, table_name=table_name)
|
||||||
|
|
||||||
primary_key = await self.ensure_sync_columns(conn, table_name)
|
primary_key = await self.ensure_sync_columns(conn, table_name)
|
||||||
|
|
||||||
result = await conn.execute(query, *args)
|
if query.strip().upper().startswith("INSERT") and "RETURNING" in query.upper():
|
||||||
|
# For INSERT queries with RETURNING clause, use fetch to get the returned values
|
||||||
|
result = await conn.fetch(query, *args)
|
||||||
|
else:
|
||||||
|
# For other queries, use execute
|
||||||
|
result = await conn.execute(query, *args)
|
||||||
|
|
||||||
asyncio.create_task(self._sync_changes(table_name, primary_key))
|
asyncio.create_task(self._sync_changes(table_name, primary_key))
|
||||||
|
|
||||||
return []
|
return result
|
||||||
finally:
|
finally:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
|
|
|
@ -181,37 +181,39 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||||
location_point
|
location_point
|
||||||
]
|
]
|
||||||
|
|
||||||
# Check for None values and replace with appropriate defaults
|
|
||||||
daily_weather_params = ['' if v is None else v for v in daily_weather_params]
|
|
||||||
|
|
||||||
debug(f"Prepared daily_weather_params: {daily_weather_params}")
|
debug(f"Prepared daily_weather_params: {daily_weather_params}")
|
||||||
|
|
||||||
daily_weather_query = '''
|
daily_weather_query = '''
|
||||||
INSERT INTO dailyweather (
|
INSERT INTO dailyweather (
|
||||||
sunrise, sunriseepoch, sunset, sunsetepoch, description,
|
sunrise, sunriseepoch, sunset, sunsetepoch, description,
|
||||||
tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated,
|
tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated,
|
||||||
datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike,
|
datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike,
|
||||||
dew, humidity, precip, precipprob, precipcover, preciptype,
|
dew, humidity, precip, precipprob, precipcover, preciptype,
|
||||||
snow, snowdepth, windgust, pressure, cloudcover, visibility,
|
snow, snowdepth, windgust, pressure, cloudcover, visibility,
|
||||||
solarradiation, solarenergy, severerisk, moonphase, conditions,
|
solarradiation, solarenergy, severerisk, moonphase, conditions,
|
||||||
stations, source, location
|
stations, source, location
|
||||||
) VALUES ($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)
|
) VALUES ($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)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")
|
||||||
|
|
||||||
daily_weather_id = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")
|
if not daily_weather_result:
|
||||||
|
raise ValueError("Failed to insert daily weather data: no result returned")
|
||||||
|
|
||||||
|
daily_weather_id = daily_weather_result[0]['id']
|
||||||
debug(f"Inserted daily weather data with id: {daily_weather_id}")
|
debug(f"Inserted daily weather data with id: {daily_weather_id}")
|
||||||
|
|
||||||
if 'hours' in day_data:
|
if 'hours' in day_data:
|
||||||
debug(f"Processing {len(day_data['hours'])} hourly records")
|
debug(f"Processing {len(day_data['hours'])} hourly records")
|
||||||
for hour_data in day_data['hours']:
|
for hour_data in day_data['hours']:
|
||||||
try:
|
try:
|
||||||
hour_data['datetime'] = await gis.dt(hour_data.get('datetimeEpoch'))
|
hour_datetime = await gis.dt(hour_data.get('datetimeEpoch'))
|
||||||
hour_preciptype_array = hour_data.get('preciptype', []) or []
|
hour_preciptype_array = hour_data.get('preciptype', []) or []
|
||||||
hour_stations_array = hour_data.get('stations', []) or []
|
hour_stations_array = hour_data.get('stations', []) or []
|
||||||
hourly_weather_params = [
|
hourly_weather_params = [
|
||||||
daily_weather_id, # Use the daily weather id as a foreign key
|
daily_weather_id,
|
||||||
hour_data['datetime'],
|
hour_datetime,
|
||||||
hour_data.get('datetimeEpoch'),
|
hour_data.get('datetimeEpoch'),
|
||||||
hour_data['temp'],
|
hour_data['temp'],
|
||||||
hour_data['feelslike'],
|
hour_data['feelslike'],
|
||||||
|
@ -238,17 +240,18 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||||
hour_data.get('source', ''),
|
hour_data.get('source', ''),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Check for None values and replace with appropriate defaults
|
|
||||||
hourly_weather_params = ['' if v is None else v for v in hourly_weather_params]
|
|
||||||
|
|
||||||
hourly_weather_query = '''
|
hourly_weather_query = '''
|
||||||
INSERT INTO hourlyweather (daily_weather_id, datetime, datetimeepoch, temp, feelslike, humidity, dew, precip, precipprob,
|
INSERT INTO hourlyweather (
|
||||||
preciptype, snow, snowdepth, windgust, windspeed, winddir, pressure, cloudcover, visibility, solarradiation, solarenergy,
|
daily_weather_id, datetime, datetimeepoch, temp, feelslike,
|
||||||
uvindex, severerisk, conditions, icon, stations, source)
|
humidity, dew, precip, precipprob, preciptype, snow, snowdepth,
|
||||||
VALUES ($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)
|
windgust, windspeed, winddir, pressure, cloudcover, visibility,
|
||||||
|
solarradiation, solarenergy, uvindex, severerisk, conditions,
|
||||||
|
icon, stations, source
|
||||||
|
) VALUES ($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)
|
||||||
'''
|
'''
|
||||||
await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather")
|
hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather")
|
||||||
debug(f"Inserted hourly weather data for {hour_data['datetime']}")
|
debug(f"Inserted hourly weather data for {hour_datetime}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Error processing hourly data: {e}")
|
err(f"Error processing hourly data: {e}")
|
||||||
err(f"Problematic hour_data: {hour_data}")
|
err(f"Problematic hour_data: {hour_data}")
|
||||||
|
|
Loading…
Add table
Reference in a new issue