Auto-update: Tue Aug 6 23:20:17 PDT 2024

This commit is contained in:
sanj 2024-08-06 23:20:17 -07:00
parent 210059e186
commit 0d99ad1f33
2 changed files with 16 additions and 23 deletions

View file

@ -833,34 +833,30 @@ class APIConfig(BaseModel):
async def execute_write_query(self, query: str, *args, table_name: str): async def execute_write_query(self, query: str, *args, table_name: str):
local_ts_id = os.environ.get('TS_ID')
conn = await self.get_connection(self.local_db) conn = await self.get_connection(self.local_db)
if conn is None: if conn is None:
err(f"Unable to connect to local database. Write operation failed.") err(f"Unable to connect to local database. Write operation failed.")
return [] return []
try: try:
# Remove any formatting from the query # Execute the original query
query = ' '.join(query.split()) result = await conn.fetch(query, *args)
# Add version and server_id to the query if result:
columns = query.split('(')[1].split(')')[0] # Update version and server_id
values = query.split('VALUES')[1].split('RETURNING')[0] update_query = f"""
UPDATE {table_name}
modified_query = f""" SET version = COALESCE(version, 0) + 1,
INSERT INTO {table_name} ({columns}, version, server_id) server_id = $1
VALUES {values[:-1]}, (SELECT COALESCE(MAX(version), 0) + 1 FROM {table_name}), '{local_ts_id}') WHERE id = $2
ON CONFLICT (id) DO UPDATE SET RETURNING id, version
version = {table_name}.version + 1, """
server_id = EXCLUDED.server_id update_result = await conn.fetch(update_query, os.environ.get('TS_ID'), result[0]['id'])
RETURNING id, version return update_result
"""
result = await conn.fetch(modified_query, *args)
return result return result
except Exception as e: except Exception as e:
err(f"Error executing write query: {str(e)}") err(f"Error executing write query: {str(e)}")
err(f"Query: {modified_query}") err(f"Query: {query}")
err(f"Args: {args}") err(f"Args: {args}")
err(f"Traceback: {traceback.format_exc()}") err(f"Traceback: {traceback.format_exc()}")
return [] return []
@ -869,7 +865,6 @@ class APIConfig(BaseModel):
async def get_table_columns(self, conn, table_name: str) -> List[str]: async def get_table_columns(self, conn, table_name: str) -> List[str]:
query = """ query = """
SELECT column_name SELECT column_name

View file

@ -139,10 +139,8 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
async def store_weather_to_db(date_time: dt_datetime, weather_data: dict): async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
debug(f"Starting store_weather_to_db for datetime: {date_time.strftime('%Y-%m-%d %H:%M:%S')}")
try: try:
day_data = weather_data.get('days', [{}])[0] day_data = weather_data.get('days', [{}])[0]
debug(f"RAW DAY_DATA: {day_data}")
preciptype_array = day_data.get('preciptype', []) or [] preciptype_array = day_data.get('preciptype', []) or []
stations_array = day_data.get('stations', []) or [] stations_array = day_data.get('stations', []) or []