From d2688052535ac174ec258c42597dfad97b8350d4 Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Tue, 6 Aug 2024 21:41:49 -0700 Subject: [PATCH] Auto-update: Tue Aug 6 21:41:49 PDT 2024 --- sijapi/classes.py | 19 ++++++++----- sijapi/routers/weather.py | 57 ++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/sijapi/classes.py b/sijapi/classes.py index 6ec7d52..74723b3 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -836,18 +836,23 @@ class APIConfig(BaseModel): conn = await self.get_connection() if conn is None: raise ConnectionError("Failed to connect to local database") - + try: if table_name in self.SPECIAL_TABLES: return await self._execute_special_table_write(conn, query, *args, table_name=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)) - - return [] + + return result finally: await conn.close() diff --git a/sijapi/routers/weather.py b/sijapi/routers/weather.py index 853e6ba..64adb32 100644 --- a/sijapi/routers/weather.py +++ b/sijapi/routers/weather.py @@ -181,37 +181,39 @@ 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 = ''' - INSERT INTO dailyweather ( - sunrise, sunriseepoch, sunset, sunsetepoch, description, - tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated, - datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike, - dew, humidity, precip, precipprob, precipcover, preciptype, - snow, snowdepth, windgust, pressure, cloudcover, visibility, - solarradiation, solarenergy, severerisk, moonphase, conditions, - 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) - RETURNING id - ''' + INSERT INTO dailyweather ( + sunrise, sunriseepoch, sunset, sunsetepoch, description, + tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated, + datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike, + dew, humidity, precip, precipprob, precipcover, preciptype, + snow, snowdepth, windgust, pressure, cloudcover, visibility, + solarradiation, solarenergy, severerisk, moonphase, conditions, + 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) + 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}") 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}")