From a24dd829deee72f6dc7896fcb65e6c4463cdfb44 Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Tue, 6 Aug 2024 23:11:38 -0700 Subject: [PATCH] Auto-update: Tue Aug 6 23:11:38 PDT 2024 --- sijapi/classes.py | 11 +++++++---- sijapi/routers/weather.py | 16 ++-------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/sijapi/classes.py b/sijapi/classes.py index 3574806..4840a9d 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -849,15 +849,18 @@ class APIConfig(BaseModel): if table_name in self.SPECIAL_TABLES: result = await self._execute_special_table_write(conn, query, *args, table_name=table_name) else: + # Remove newlines and extra spaces from the query + query = query.replace('\n', ' ').replace(' ', ' ').strip() + # Prepare the INSERT ... ON CONFLICT ... query - insert_cols = ', '.join(f'"{col}"' for col in query.split('(')[1].split(')')[0].split(',')) - update_cols = ', '.join([f'"{col}" = EXCLUDED."{col}"' for col in query.split('(')[1].split(')')[0].split(',') if col.strip() not in ['id', 'version', 'server_id']]) + insert_cols = ', '.join(col.strip() for col in query.split('(')[1].split(')')[0].split(',')) + update_cols = ', '.join([f'{col.strip()} = EXCLUDED.{col.strip()}' for col in query.split('(')[1].split(')')[0].split(',') if col.strip() not in ['id', 'version', 'server_id']]) modified_query = f""" WITH new_version AS ( SELECT COALESCE(MAX(version), 0) + 1 as next_version FROM {table_name} - WHERE id = (SELECT id FROM {table_name} WHERE {insert_cols.split(',')[0]} = $1 FOR UPDATE) + WHERE id = (SELECT id FROM {table_name} WHERE {insert_cols.split(',')[0].strip()} = $1 FOR UPDATE) ) INSERT INTO {table_name} ({insert_cols}, version, server_id) VALUES ({', '.join(f'${i+1}' for i in range(len(args)))}, (SELECT next_version FROM new_version), '{local_ts_id}') @@ -871,7 +874,7 @@ class APIConfig(BaseModel): """ result = await conn.fetch(modified_query, *args) - + return result except Exception as e: err(f"Error executing write query on {pool_entry['ts_id']}: {str(e)}") diff --git a/sijapi/routers/weather.py b/sijapi/routers/weather.py index 3fb9f1c..81c27d4 100644 --- a/sijapi/routers/weather.py +++ b/sijapi/routers/weather.py @@ -198,20 +198,8 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict): ] daily_weather_query = ''' - INSERT INTO dailyweather ( - location, 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 - ) 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 (location, 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) 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 + '''.replace('\n', ' ').replace(' ', ' ').strip() daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")