Auto-update: Tue Aug 6 21:41:49 PDT 2024

This commit is contained in:
sanj 2024-08-06 21:41:49 -07:00
parent 2ad5c527f0
commit d268805253
2 changed files with 42 additions and 34 deletions

View file

@ -843,11 +843,16 @@ class APIConfig(BaseModel):
primary_key = await self.ensure_sync_columns(conn, table_name)
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))
return []
return result
finally:
await conn.close()

View file

@ -181,9 +181,6 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
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}")
daily_weather_query = '''
@ -199,19 +196,24 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
RETURNING id
'''
daily_weather_id = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")
daily_weather_result = 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}")
if 'hours' in day_data:
debug(f"Processing {len(day_data['hours'])} hourly records")
for hour_data in day_data['hours']:
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_stations_array = hour_data.get('stations', []) or []
hourly_weather_params = [
daily_weather_id, # Use the daily weather id as a foreign key
hour_data['datetime'],
daily_weather_id,
hour_datetime,
hour_data.get('datetimeEpoch'),
hour_data['temp'],
hour_data['feelslike'],
@ -238,17 +240,18 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
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 = '''
INSERT INTO hourlyweather (daily_weather_id, datetime, datetimeepoch, temp, feelslike, humidity, dew, precip, precipprob,
preciptype, snow, snowdepth, 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)
INSERT INTO hourlyweather (
daily_weather_id, datetime, datetimeepoch, temp, feelslike,
humidity, dew, precip, precipprob, preciptype, snow, snowdepth,
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")
debug(f"Inserted hourly weather data for {hour_data['datetime']}")
hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather")
debug(f"Inserted hourly weather data for {hour_datetime}")
except Exception as e:
err(f"Error processing hourly data: {e}")
err(f"Problematic hour_data: {hour_data}")