|
| 1 | +import pandas as pd |
| 2 | +import streamlit as st |
| 3 | +import time |
| 4 | + |
| 5 | +from lib.api import fetch_api_data |
| 6 | +from src.utils import get_current_slot |
| 7 | + |
| 8 | +RETRY_DELAY_SECONDS = 5 |
| 9 | + |
| 10 | + |
| 11 | +def is_processing(result): |
| 12 | + """Checks if the API result indicates backend processing.""" |
| 13 | + return isinstance(result, dict) and result.get("result") == "processing" |
| 14 | + |
| 15 | + |
| 16 | +def has_error(result): |
| 17 | + """Checks if the API result indicates an error.""" |
| 18 | + return result is None or ( |
| 19 | + isinstance(result, dict) and result.get("result") == "error" |
| 20 | + ) or ("pnl" not in result if isinstance(result, dict) else False) |
| 21 | + |
| 22 | + |
| 23 | +def pnl_page(): |
| 24 | + st.title("Top PnL by User (All Time)") |
| 25 | + response = fetch_api_data("pnl", "top_pnl", retry=False) |
| 26 | + |
| 27 | + if is_processing(response): |
| 28 | + st.info( |
| 29 | + f"Backend is processing PnL data. Auto-refreshing in {RETRY_DELAY_SECONDS} seconds..." |
| 30 | + ) |
| 31 | + with st.spinner("Please wait..."): |
| 32 | + time.sleep(RETRY_DELAY_SECONDS) |
| 33 | + st.rerun() |
| 34 | + return |
| 35 | + |
| 36 | + if has_error(response): |
| 37 | + error_msg = ( |
| 38 | + response["message"] |
| 39 | + if isinstance(response, dict) and "message" in response |
| 40 | + else "Could not connect or fetch PnL data." |
| 41 | + ) |
| 42 | + st.error(f"Failed to fetch PnL data: {error_msg}") |
| 43 | + if st.button("Retry"): |
| 44 | + st.rerun() |
| 45 | + return |
| 46 | + |
| 47 | + pnl_data = response["pnl"] |
| 48 | + if not pnl_data: |
| 49 | + st.info("No PnL data available.") |
| 50 | + return |
| 51 | + |
| 52 | + slot = response.get("slot", 0) |
| 53 | + current_slot = get_current_slot() |
| 54 | + if slot > 0 and current_slot > 0: |
| 55 | + slot_age = current_slot - slot |
| 56 | + st.info(f"Data from slot {slot}, which is {slot_age} slots old.") |
| 57 | + |
| 58 | + df = pd.DataFrame(pnl_data) |
| 59 | + for col in ["realized_pnl", "unrealized_pnl", "total_pnl"]: |
| 60 | + df[col] = df[col].map("${:,.2f}".format) |
| 61 | + |
| 62 | + csv = df.to_csv(index=False) |
| 63 | + st.download_button( |
| 64 | + "Download PnL Data CSV", csv, "top_pnl.csv", "text/csv", key="download-pnl" |
| 65 | + ) |
| 66 | + st.dataframe( |
| 67 | + df, |
| 68 | + height=650, |
| 69 | + column_config={ |
| 70 | + "authority": st.column_config.TextColumn( |
| 71 | + "Authority", |
| 72 | + help="Authority address", |
| 73 | + ), |
| 74 | + "user_key": st.column_config.TextColumn( |
| 75 | + "User Account", |
| 76 | + help="User account address", |
| 77 | + ), |
| 78 | + "realized_pnl": st.column_config.NumberColumn("All Time Realized PnL"), |
| 79 | + "unrealized_pnl": st.column_config.NumberColumn("Unrealized PnL"), |
| 80 | + "total_pnl": st.column_config.NumberColumn("Total PnL"), |
| 81 | + }, |
| 82 | + hide_index=True, |
| 83 | + ) |
0 commit comments