Skip to content

Commit

Permalink
Add slider
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Nov 13, 2024
1 parent b8274c2 commit 7af15c8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dotenv import load_dotenv
import pandas as pd
import requests
import streamlit as st


load_dotenv()
Expand Down Expand Up @@ -48,7 +49,8 @@ def api(
return response.json()


def api2(url: str, params: Optional[dict] = None) -> dict:
@st.cache_data(ttl=1000)
def api2(url: str, _params: Optional[dict] = None, key: str = "") -> dict:
"""
Fetch data from R2 storage using the simplified naming scheme.
Example: /api/health/health_distribution -> GET_api_health_health_distribution.json
Expand All @@ -61,12 +63,10 @@ def api2(url: str, params: Optional[dict] = None) -> dict:
cache_key = f"GET/api/{url}".replace("/", "_")

# Handle query parameters exactly as they appear in the URL
if params:
print(f"Params: {params}")
# Convert params to URL query string format
if _params:
print(f"Params: {_params}")
query_parts = []
for k, v in params.items():
# Replace space with + to match URL encoding
for k, v in _params.items():
if isinstance(v, str):
v = v.replace(" ", "%2B")
query_parts.append(f"{k}-{v}")
Expand Down
9 changes: 8 additions & 1 deletion src/page/asset_liability.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ def asset_liab_matrix_page():
st.write(res)

tabs = st.tabs(["FULL"] + [x.symbol for x in mainnet_spot_market_configs])
tabs[0].dataframe(df, hide_index=True)

# Add leverage filter to FULL tab
with tabs[0]:
min_leverage = st.slider("Filter by minimum leverage", 0.0, 50.0, 0.0, 0.5)
filtered_df = df[df["leverage"] >= min_leverage].sort_values(
"leverage", ascending=False
)
st.dataframe(filtered_df, hide_index=True)

for idx, tab in enumerate(tabs[1:]):
important_cols = [x for x in df.columns if "spot_" + str(idx) in x]
Expand Down
26 changes: 18 additions & 8 deletions src/page/asset_liability_cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ def asset_liab_matrix_cached_page():
)
st.query_params.update({"perp_market_index": perp_market_index})

# show_leverage_filter = st.toggle("Filter by Effective Leverage", value=False)
# min_leverage = 0.0
# if show_leverage_filter:
# min_leverage = st.slider("Minimum Effective Leverage", 0.0, 100.0, 0.0, 1.0)

try:
result = api2(
"asset-liability/matrix",
params=params,
_params=params,
key=f"asset-liability/matrix_{mode}_{perp_market_index}",
)
if "result" in result and result["result"] == "miss":
st.write("Fetching data for the first time...")
Expand Down Expand Up @@ -78,11 +74,25 @@ def asset_liab_matrix_cached_page():
st.write(res)

tabs = st.tabs(["FULL"] + [x.symbol for x in mainnet_spot_market_configs])
tabs[0].dataframe(df, hide_index=True)
with tabs[0]:
min_leverage = st.slider("Filter by minimum leverage", 0.0, 110.0, 0.0, 1.0)
filtered_df = df[df["leverage"] >= min_leverage].sort_values(
"leverage", ascending=False
)
st.write(
f"There are **{len(filtered_df)}** users with this **{min_leverage}x** leverage or more"
)
with st.expander("Totals"):
st.write(f"Total USD value: **{filtered_df['net_usd_value'].sum():,.2f}**")
st.write(f"Total collateral: **{filtered_df['spot_asset'].sum():,.2f}**")
st.write(
f"Total liabilities: **{filtered_df['spot_liability'].sum():,.2f}**"
)
st.dataframe(filtered_df, hide_index=True)

for idx, tab in enumerate(tabs[1:]):
important_cols = [x for x in df.columns if "spot_" + str(idx) in x]
# Sort columns in logical order: all, deposit, borrow

toshow = df[["user_key", "spot_asset", "net_usd_value"] + important_cols]
toshow = toshow[toshow[important_cols].abs().sum(axis=1) != 0].sort_values(
by="spot_" + str(idx) + "_all", ascending=False
Expand Down
7 changes: 4 additions & 3 deletions src/page/price_shock_cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ def price_shock_cached_page():

# Update query parameters
st.query_params.update({"cov": cov, "oracle_distort": oracle_distort})

n_scenarios = 5
try:
result = api2(
"price-shock/usermap",
params={
_params={
"asset_group": cov,
"oracle_distortion": oracle_distort,
"n_scenarios": 5,
"n_scenarios": n_scenarios,
},
key=f"price-shock/usermap_{cov}_{oracle_distort}_{n_scenarios}",
)
except Exception as e:
print("HIT AN EXCEPTION...", e)
Expand Down

0 comments on commit 7af15c8

Please sign in to comment.