refactor: Enhance watchlist debugging with detailed logging and Streamlit output
This commit is contained in:
parent
e1a01a6f97
commit
8649949278
@ -227,47 +227,38 @@ def trading_system_page():
|
|||||||
# Move Add to Watch List button outside of columns
|
# Move Add to Watch List button outside of columns
|
||||||
if st.button("Add to Watch List", key="add_to_watchlist"):
|
if st.button("Add to Watch List", key="add_to_watchlist"):
|
||||||
try:
|
try:
|
||||||
# Debug information
|
# Create a container for debug output
|
||||||
debug_info = {
|
debug_container = st.container()
|
||||||
"Ticker": ticker,
|
with debug_container:
|
||||||
"Entry Price": entry_price,
|
st.write("=== PRE-WATCHLIST ITEM CREATION ===")
|
||||||
"Target Price": target_price,
|
st.json({
|
||||||
"Stop Loss": position['stop_loss'],
|
"Ticker": ticker,
|
||||||
"Notes": notes,
|
"Entry Price": entry_price,
|
||||||
"Selected Watchlist ID": selected_list[0]
|
"Target Price": target_price,
|
||||||
}
|
"Stop Loss": position['stop_loss'],
|
||||||
|
"Notes": notes,
|
||||||
st.write("=== PRE-WATCHLIST ITEM CREATION ===")
|
"Selected Watchlist ID": selected_list[0]
|
||||||
st.write(debug_info)
|
})
|
||||||
st.write("=====================================")
|
|
||||||
|
item = WatchlistItem(
|
||||||
item = WatchlistItem(
|
ticker=ticker,
|
||||||
ticker=ticker,
|
entry_price=float(entry_price),
|
||||||
entry_price=float(entry_price),
|
target_price=float(target_price),
|
||||||
target_price=float(target_price),
|
stop_loss=float(position['stop_loss']),
|
||||||
stop_loss=float(position['stop_loss']),
|
notes=str(notes) if notes else ''
|
||||||
notes=str(notes) if notes else ''
|
)
|
||||||
)
|
|
||||||
|
st.write("=== WATCHLIST ITEM DETAILS ===")
|
||||||
st.write("=== WATCHLIST ITEM DETAILS ===")
|
st.json(vars(item))
|
||||||
st.write(vars(item))
|
|
||||||
st.write("==============================")
|
success = add_to_watchlist(selected_list[0], item)
|
||||||
|
|
||||||
# Add 2 second delay to ensure debug info is visible
|
if success:
|
||||||
time.sleep(2)
|
st.success(f"Added {ticker} to watch list!")
|
||||||
|
time.sleep(3) # Give time to see success message
|
||||||
success = add_to_watchlist(selected_list[0], item)
|
st.experimental_rerun()
|
||||||
|
else:
|
||||||
# Show the result and wait before rerun
|
st.error("Failed to add to watch list. Check the details above.")
|
||||||
st.write(f"Add to watchlist result: {success}")
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
if success:
|
|
||||||
st.success(f"Added {ticker} to watch list!")
|
|
||||||
time.sleep(1)
|
|
||||||
st.rerun()
|
|
||||||
else:
|
|
||||||
st.error("Failed to add to watch list. Check the details above.")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
st.error(f"Error adding to watchlist: {str(e)}")
|
st.error(f"Error adding to watchlist: {str(e)}")
|
||||||
|
|||||||
@ -38,90 +38,112 @@ def get_watchlists() -> List[dict]:
|
|||||||
def add_to_watchlist(watchlist_id: int, item: WatchlistItem) -> bool:
|
def add_to_watchlist(watchlist_id: int, item: WatchlistItem) -> bool:
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
try:
|
try:
|
||||||
# Debug output using st.write
|
# Debug output using both logger and st.write
|
||||||
st.write("\n=== WATCHLIST INSERT ATTEMPT ===")
|
debug_msg = f"""
|
||||||
st.write("Input parameters:")
|
=== WATCHLIST INSERT ATTEMPT ===
|
||||||
st.write(f"Watchlist ID: {watchlist_id}")
|
Watchlist ID: {watchlist_id}
|
||||||
st.write("Item details:", {
|
Item details:
|
||||||
"Ticker": item.ticker,
|
- Ticker: {item.ticker}
|
||||||
"Entry Price": item.entry_price,
|
- Entry Price: {item.entry_price}
|
||||||
"Target Price": item.target_price,
|
- Target Price: {item.target_price}
|
||||||
"Stop Loss": item.stop_loss,
|
- Stop Loss: {item.stop_loss}
|
||||||
"Notes": item.notes
|
- Notes: {item.notes}
|
||||||
})
|
==============================
|
||||||
|
|
||||||
# Check if watchlist exists
|
|
||||||
check_watchlist_query = f"SELECT * FROM stock_db.watchlists WHERE id = {watchlist_id}"
|
|
||||||
watchlist_result = client.query(check_watchlist_query)
|
|
||||||
st.write("Watchlist check result:", watchlist_result.result_rows)
|
|
||||||
|
|
||||||
if not watchlist_result.result_rows:
|
|
||||||
st.error(f"Watchlist ID {watchlist_id} not found")
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Get next ID
|
|
||||||
id_query = "SELECT max(id) + 1 as next_id FROM stock_db.watchlist_items"
|
|
||||||
result = client.query(id_query)
|
|
||||||
next_id = result.first_row[0] if result.first_row[0] is not None else 1
|
|
||||||
st.write(f"Next ID: {next_id}")
|
|
||||||
|
|
||||||
# Prepare data
|
|
||||||
data = [(
|
|
||||||
int(next_id),
|
|
||||||
int(watchlist_id),
|
|
||||||
str(item.ticker),
|
|
||||||
float(item.entry_price),
|
|
||||||
float(item.target_price),
|
|
||||||
float(item.stop_loss),
|
|
||||||
str(item.notes or ''),
|
|
||||||
datetime.now()
|
|
||||||
)]
|
|
||||||
|
|
||||||
column_names = [
|
|
||||||
'id', 'watchlist_id', 'ticker', 'entry_price',
|
|
||||||
'target_price', 'stop_loss', 'notes', 'created_at'
|
|
||||||
]
|
|
||||||
|
|
||||||
column_types = [
|
|
||||||
'UInt32', 'UInt32', 'String', 'Float64',
|
|
||||||
'Float64', 'Float64', 'String', 'DateTime'
|
|
||||||
]
|
|
||||||
|
|
||||||
# Show insert details
|
|
||||||
st.write("\n=== DATABASE INSERT ATTEMPT ===")
|
|
||||||
st.write("Data to insert:", data)
|
|
||||||
st.write("Column names:", column_names)
|
|
||||||
st.write("Column types:", column_types)
|
|
||||||
|
|
||||||
# Perform insert
|
|
||||||
try:
|
|
||||||
client.insert(
|
|
||||||
'stock_db.watchlist_items',
|
|
||||||
data,
|
|
||||||
column_names=column_names,
|
|
||||||
column_types=column_types
|
|
||||||
)
|
|
||||||
st.write("Insert operation completed successfully")
|
|
||||||
except Exception as insert_error:
|
|
||||||
st.error(f"Insert error: {insert_error}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Verify the insert
|
|
||||||
verify_query = f"""
|
|
||||||
SELECT *
|
|
||||||
FROM stock_db.watchlist_items
|
|
||||||
WHERE id = {next_id}
|
|
||||||
AND watchlist_id = {watchlist_id}
|
|
||||||
AND ticker = '{item.ticker}'
|
|
||||||
"""
|
"""
|
||||||
verify_result = client.query(verify_query)
|
logger.info(debug_msg)
|
||||||
st.write("Verification result:", verify_result.result_rows)
|
st.code(debug_msg) # Using st.code for better formatting
|
||||||
|
|
||||||
if not verify_result.result_rows:
|
# Force a container to stay visible
|
||||||
st.error("Verification failed - no rows found after insert")
|
placeholder = st.empty()
|
||||||
return False
|
with placeholder.container():
|
||||||
|
# Check if watchlist exists
|
||||||
return True
|
check_watchlist_query = f"SELECT * FROM stock_db.watchlists WHERE id = {watchlist_id}"
|
||||||
|
watchlist_result = client.query(check_watchlist_query)
|
||||||
|
logger.info(f"Watchlist check result: {watchlist_result.result_rows}")
|
||||||
|
st.write("Watchlist check result:", watchlist_result.result_rows)
|
||||||
|
|
||||||
|
if not watchlist_result.result_rows:
|
||||||
|
error_msg = f"Watchlist ID {watchlist_id} not found"
|
||||||
|
logger.error(error_msg)
|
||||||
|
st.error(error_msg)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Get next ID
|
||||||
|
id_query = "SELECT max(id) + 1 as next_id FROM stock_db.watchlist_items"
|
||||||
|
result = client.query(id_query)
|
||||||
|
next_id = result.first_row[0] if result.first_row[0] is not None else 1
|
||||||
|
logger.info(f"Next ID: {next_id}")
|
||||||
|
st.write(f"Next ID: {next_id}")
|
||||||
|
|
||||||
|
# Prepare data
|
||||||
|
data = [(
|
||||||
|
int(next_id),
|
||||||
|
int(watchlist_id),
|
||||||
|
str(item.ticker),
|
||||||
|
float(item.entry_price),
|
||||||
|
float(item.target_price),
|
||||||
|
float(item.stop_loss),
|
||||||
|
str(item.notes or ''),
|
||||||
|
datetime.now()
|
||||||
|
)]
|
||||||
|
|
||||||
|
column_names = [
|
||||||
|
'id', 'watchlist_id', 'ticker', 'entry_price',
|
||||||
|
'target_price', 'stop_loss', 'notes', 'created_at'
|
||||||
|
]
|
||||||
|
|
||||||
|
column_types = [
|
||||||
|
'UInt32', 'UInt32', 'String', 'Float64',
|
||||||
|
'Float64', 'Float64', 'String', 'DateTime'
|
||||||
|
]
|
||||||
|
|
||||||
|
# Show insert details
|
||||||
|
logger.info(f"Data to insert: {data}")
|
||||||
|
logger.info(f"Column names: {column_names}")
|
||||||
|
logger.info(f"Column types: {column_types}")
|
||||||
|
|
||||||
|
st.write("=== DATABASE INSERT ATTEMPT ===")
|
||||||
|
st.json({"data": str(data), "columns": column_names, "types": column_types})
|
||||||
|
|
||||||
|
# Perform insert
|
||||||
|
try:
|
||||||
|
client.insert(
|
||||||
|
'stock_db.watchlist_items',
|
||||||
|
data,
|
||||||
|
column_names=column_names,
|
||||||
|
column_types=column_types
|
||||||
|
)
|
||||||
|
success_msg = "Insert operation completed successfully"
|
||||||
|
logger.info(success_msg)
|
||||||
|
st.success(success_msg)
|
||||||
|
except Exception as insert_error:
|
||||||
|
error_msg = f"Insert error: {insert_error}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
st.error(error_msg)
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Verify the insert
|
||||||
|
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 result: {verify_result.result_rows}")
|
||||||
|
st.write("Verification result:", verify_result.result_rows)
|
||||||
|
|
||||||
|
if not verify_result.result_rows:
|
||||||
|
error_msg = "Verification failed - no rows found after insert"
|
||||||
|
logger.error(error_msg)
|
||||||
|
st.error(error_msg)
|
||||||
|
return False
|
||||||
|
|
||||||
|
st.success("Watchlist item added successfully!")
|
||||||
|
time.sleep(3) # Give time to see the messages
|
||||||
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error adding to watchlist: {e}", exc_info=True)
|
logger.error(f"Error adding to watchlist: {e}", exc_info=True)
|
||||||
return False
|
return False
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user