Auto-update: Tue Aug 6 23:24:46 PDT 2024

This commit is contained in:
sanj 2024-08-06 23:24:46 -07:00
parent 3c02ac65b9
commit 6ec5102f4c

View file

@ -213,70 +213,70 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
daily_weather_result = 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: if not daily_weather_result:
raise ValueError("Failed to insert daily weather data: no result returned") 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}")
# Hourly weather insertion daily_weather_id = daily_weather_result[0]['id']
if 'hours' in day_data: debug(f"Inserted daily weather data with id: {daily_weather_id}")
debug(f"Processing {len(day_data['hours'])} hourly records")
for hour_data in day_data['hours']: # Hourly weather insertion
hour_preciptype_array = hour_data.get('preciptype', []) or [] if 'hours' in day_data:
hour_stations_array = hour_data.get('stations', []) or [] debug(f"Processing {len(day_data['hours'])} hourly records")
hourly_weather_params = [ for hour_data in day_data['hours']:
daily_weather_id, hour_preciptype_array = hour_data.get('preciptype', []) or []
await gis.dt(hour_data.get('datetimeEpoch')), hour_stations_array = hour_data.get('stations', []) or []
hour_data.get('datetimeEpoch'), hourly_weather_params = [
hour_data.get('temp'), daily_weather_id,
hour_data.get('feelslike'), await gis.dt(hour_data.get('datetimeEpoch')),
hour_data.get('humidity'), hour_data.get('datetimeEpoch'),
hour_data.get('dew'), hour_data.get('temp'),
hour_data.get('precip'), hour_data.get('feelslike'),
hour_data.get('precipprob'), hour_data.get('humidity'),
hour_preciptype_array, hour_data.get('dew'),
hour_data.get('snow'), hour_data.get('precip'),
hour_data.get('snowdepth'), hour_data.get('precipprob'),
hour_data.get('windgust'), hour_preciptype_array,
hour_data.get('windspeed'), hour_data.get('snow'),
hour_data.get('winddir'), hour_data.get('snowdepth'),
hour_data.get('pressure'), hour_data.get('windgust'),
hour_data.get('cloudcover'), hour_data.get('windspeed'),
hour_data.get('visibility'), hour_data.get('winddir'),
hour_data.get('solarradiation'), hour_data.get('pressure'),
hour_data.get('solarenergy'), hour_data.get('cloudcover'),
hour_data.get('uvindex'), hour_data.get('visibility'),
hour_data.get('severerisk', 0), hour_data.get('solarradiation'),
hour_data.get('conditions'), hour_data.get('solarenergy'),
hour_data.get('icon'), hour_data.get('uvindex'),
hour_stations_array, hour_data.get('severerisk', 0),
hour_data.get('source', '') hour_data.get('conditions'),
] hour_data.get('icon'),
hour_stations_array,
hourly_weather_query = ''' hour_data.get('source', '')
INSERT INTO hourlyweather ( ]
daily_weather_id, datetime, datetimeepoch, temp, feelslike,
humidity, dew, precip, precipprob, preciptype, snow, snowdepth, hourly_weather_query = '''
windgust, windspeed, winddir, pressure, cloudcover, visibility, INSERT INTO hourlyweather (
solarradiation, solarenergy, uvindex, severerisk, conditions, daily_weather_id, datetime, datetimeepoch, temp, feelslike,
icon, stations, source humidity, dew, precip, precipprob, preciptype, snow, snowdepth,
) VALUES ( windgust, windspeed, winddir, pressure, cloudcover, visibility,
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, solarradiation, solarenergy, uvindex, severerisk, conditions,
$16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26 icon, stations, source
) RETURNING id ) VALUES (
''' $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,
hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather") $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26
if not hourly_result: ) RETURNING id
warn(f"Failed to insert hourly weather data for {hour_data.get('datetimeEpoch')}") '''
else: hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather")
debug(f"Inserted hourly weather data with id: {hourly_result[0]['id']}") if not hourly_result:
warn(f"Failed to insert hourly weather data for {hour_data.get('datetimeEpoch')}")
return "SUCCESS" else:
except Exception as e: debug(f"Inserted hourly weather data with id: {hourly_result[0]['id']}")
err(f"Error in weather storage: {e}")
err(f"Traceback: {traceback.format_exc()}") return "SUCCESS"
return "FAILURE" except Exception as e:
err(f"Error in weather storage: {e}")
err(f"Traceback: {traceback.format_exc()}")
return "FAILURE"
async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude: float): async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude: float):