feat: Add detailed SQL logging and debug database state button
This commit is contained in:
parent
62346dcd04
commit
8fadc159bd
@ -205,6 +205,29 @@ def trading_system_page():
|
||||
notes = st.text_area("Notes")
|
||||
|
||||
with col2:
|
||||
if st.button("Debug Database State"):
|
||||
try:
|
||||
with create_client() as client:
|
||||
# Check watchlists table
|
||||
watchlists_query = "SELECT * FROM stock_db.watchlists"
|
||||
watchlists_result = client.query(watchlists_query)
|
||||
st.write("=== Watchlists Table ===")
|
||||
st.write(watchlists_result.result_rows)
|
||||
|
||||
# Check watchlist_items table
|
||||
items_query = "SELECT * FROM stock_db.watchlist_items"
|
||||
items_result = client.query(items_query)
|
||||
st.write("=== Watchlist Items Table ===")
|
||||
st.write(items_result.result_rows)
|
||||
|
||||
# Show table structure
|
||||
structure_query = "DESCRIBE TABLE stock_db.watchlist_items"
|
||||
structure_result = client.query(structure_query)
|
||||
st.write("=== Table Structure ===")
|
||||
st.write(structure_result.result_rows)
|
||||
except Exception as e:
|
||||
st.error(f"Debug query error: {e}")
|
||||
|
||||
if st.button("Add to Watch List", key="add_to_watchlist"):
|
||||
try:
|
||||
logger.info(f"Attempting to add {ticker} to watchlist {selected_list[0]}")
|
||||
|
||||
@ -37,21 +37,36 @@ def get_watchlists() -> List[dict]:
|
||||
def add_to_watchlist(watchlist_id: int, item: WatchlistItem) -> bool:
|
||||
with create_client() as client:
|
||||
try:
|
||||
# Get the next available ID
|
||||
result = client.query("SELECT max(id) + 1 as next_id FROM stock_db.watchlist_items")
|
||||
# Log the initial state
|
||||
logger.info("=== Starting add_to_watchlist operation ===")
|
||||
|
||||
# Check if watchlist exists
|
||||
check_watchlist_query = f"SELECT * FROM stock_db.watchlists WHERE id = {watchlist_id}"
|
||||
logger.info(f"Checking watchlist query: {check_watchlist_query}")
|
||||
watchlist_result = client.query(check_watchlist_query)
|
||||
logger.info(f"Watchlist check result: {watchlist_result.result_rows}")
|
||||
|
||||
if not watchlist_result.result_rows:
|
||||
logger.error(f"Watchlist ID {watchlist_id} not found")
|
||||
return False
|
||||
|
||||
# Get next ID query
|
||||
id_query = "SELECT max(id) + 1 as next_id FROM stock_db.watchlist_items"
|
||||
logger.info(f"Getting next ID with query: {id_query}")
|
||||
result = client.query(id_query)
|
||||
logger.info(f"Next ID query result: {result.result_rows}")
|
||||
|
||||
next_id = result.first_row[0] if result.first_row[0] is not None else 1
|
||||
logger.info(f"Next ID determined: {next_id}")
|
||||
|
||||
logger.info(f"Adding item to watchlist: ID={next_id}, WatchlistID={watchlist_id}, Ticker={item.ticker}")
|
||||
logger.info(f"Entry Price: {item.entry_price}, Target: {item.target_price}, Stop: {item.stop_loss}")
|
||||
|
||||
# Ensure all values are properly formatted
|
||||
# Prepare data with explicit type conversion
|
||||
entry_price = float(item.entry_price) if item.entry_price is not None else 0.0
|
||||
target_price = float(item.target_price) if item.target_price is not None else 0.0
|
||||
stop_loss = float(item.stop_loss) if item.stop_loss is not None else 0.0
|
||||
|
||||
data = [(
|
||||
int(next_id), # Ensure ID is integer
|
||||
int(watchlist_id), # Ensure watchlist_id is integer
|
||||
int(next_id),
|
||||
int(watchlist_id),
|
||||
str(item.ticker),
|
||||
entry_price,
|
||||
target_price,
|
||||
@ -60,33 +75,53 @@ def add_to_watchlist(watchlist_id: int, item: WatchlistItem) -> bool:
|
||||
datetime.now()
|
||||
)]
|
||||
|
||||
logger.info(f"Attempting to insert data: {data}")
|
||||
|
||||
# Add explicit column types
|
||||
column_types = [
|
||||
'UInt32', # id
|
||||
'UInt32', # watchlist_id
|
||||
'String', # ticker
|
||||
'Float64', # entry_price
|
||||
'Float64', # target_price
|
||||
'Float64', # stop_loss
|
||||
'String', # notes
|
||||
'DateTime' # created_at
|
||||
column_names = [
|
||||
'id', 'watchlist_id', 'ticker', 'entry_price',
|
||||
'target_price', 'stop_loss', 'notes', 'created_at'
|
||||
]
|
||||
|
||||
client.insert(
|
||||
'stock_db.watchlist_items',
|
||||
data,
|
||||
column_names=['id', 'watchlist_id', 'ticker', 'entry_price',
|
||||
'target_price', 'stop_loss', 'notes', 'created_at'],
|
||||
column_types=column_types
|
||||
)
|
||||
column_types = [
|
||||
'UInt32', 'UInt32', 'String', 'Float64',
|
||||
'Float64', 'Float64', 'String', 'DateTime'
|
||||
]
|
||||
|
||||
# Verify the insert
|
||||
verify_query = f"SELECT * FROM stock_db.watchlist_items WHERE id = {next_id}"
|
||||
# Log the insert operation details
|
||||
logger.info("=== Insert Operation Details ===")
|
||||
logger.info(f"Table: stock_db.watchlist_items")
|
||||
logger.info(f"Column Names: {column_names}")
|
||||
logger.info(f"Column Types: {column_types}")
|
||||
logger.info(f"Data to insert: {data}")
|
||||
|
||||
# Perform insert
|
||||
try:
|
||||
client.insert(
|
||||
'stock_db.watchlist_items',
|
||||
data,
|
||||
column_names=column_names,
|
||||
column_types=column_types
|
||||
)
|
||||
logger.info("Insert operation completed")
|
||||
except Exception as insert_error:
|
||||
logger.error(f"Insert operation failed: {insert_error}")
|
||||
raise
|
||||
|
||||
# Verify the insert immediately after
|
||||
verify_query = f"""
|
||||
SELECT *
|
||||
FROM stock_db.watchlist_items
|
||||
WHERE id = {next_id}
|
||||
AND watchlist_id = {watchlist_id}
|
||||
AND ticker = '{item.ticker}'
|
||||
"""
|
||||
logger.info(f"Verification query: {verify_query}")
|
||||
verify_result = client.query(verify_query)
|
||||
logger.info(f"Verification query result: {verify_result.result_rows}")
|
||||
logger.info(f"Verification result rows: {verify_result.result_rows}")
|
||||
|
||||
if not verify_result.result_rows:
|
||||
logger.error("Verification failed - no rows found after insert")
|
||||
return False
|
||||
|
||||
logger.info("=== add_to_watchlist operation completed successfully ===")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding to watchlist: {e}", exc_info=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user