Skip to content

Commit

Permalink
Temporarily don't reload snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 23, 2024
1 parent 88d8757 commit 0f79b63
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
3 changes: 3 additions & 0 deletions backend/api/asset_liability.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import functools

from backend.state import BackendRequest
from backend.state import BackendState
from backend.utils.matrix import get_matrix
Expand All @@ -9,6 +11,7 @@
router = APIRouter()


@functools.lru_cache()
async def _get_asset_liability_matrix(
snapshot_path: str,
vat: Vat,
Expand Down
24 changes: 18 additions & 6 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def clean_cache(state: BackendState) -> None:

pickles = glob.glob("pickles/*")

# check for pickle folders with less than 8 files (error in write)
# check for pickle folders with less than 4 files (error in write)
incomplete_pickles = []
for pickle in pickles:
if len(glob.glob(f"{pickle}/*")) < 8:
if len(glob.glob(f"{pickle}/*")) < 4:
incomplete_pickles.append(pickle)

for incomplete_pickle in incomplete_pickles:
Expand Down Expand Up @@ -91,15 +91,27 @@ async def lifespan(app: FastAPI):
if len(cached_vat_path) > 0:
print("Loading cached vat")
await state.load_pickle_snapshot(cached_vat_path[-1])
await repeatedly_clean_cache(state)
await repeatedly_retake_snapshot(state)
# await repeatedly_clean_cache(state)
# await repeatedly_retake_snapshot(state)
else:
print("No cached vat found, bootstrapping")
await state.bootstrap()
await state.take_pickle_snapshot()
await repeatedly_clean_cache(state)
await repeatedly_retake_snapshot(state)
# await repeatedly_clean_cache(state)
# await repeatedly_retake_snapshot(state)
state.ready = True
# print("Checking price shock")
# await price_shock._get_price_shock(
# state.current_pickle_path, state.vat, state.dc, 0.05, "ignore stables", 5
# )
# print("Checking asset liability matrix")
# await asset_liability._get_asset_liability_matrix(
# state.current_pickle_path, state.vat, 0, 0
# )
import random
import time

time.sleep(random.randint(1, 10))
print("Starting app")
yield

Expand Down
2 changes: 2 additions & 0 deletions backend/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def initialize(
self.spot_map,
self.perp_map,
)
self.ready = False
self.current_pickle_path = "bootstrap"

async def bootstrap(self):
with waiting_for("drift client"):
Expand Down
6 changes: 3 additions & 3 deletions gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
workers = 4
workers = 2
worker_class = "uvicorn.workers.UvicornWorker"
bind = "0.0.0.0:8000"
timeout = 180
timeout = 300
keepalive = 65
max_requests = 1000
max_requests_jitter = 50
Expand All @@ -11,4 +11,4 @@

# Restart workers that die unexpectedly
worker_exit_on_restart = True
worker_restart_delay = 2
worker_restart_delay = 20
18 changes: 10 additions & 8 deletions src/page/price_shock.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ def price_shock_plot(user_leverages, oracle_distort: float):
num_scenarios = len(levs["leverages_down"])
oracle_moves = generate_oracle_moves(num_scenarios, oracle_distort)

# Create and sort the DataFrame BEFORE plotting
df_plot = pd.DataFrame(
{
"Oracle Move (%)": oracle_moves,
"Total Bankruptcy ($)": total_bankruptcies,
"Spot Bankruptcy ($)": spot_bankruptcies,
}
).sort_values("Oracle Move (%)")
)

# Sort by Oracle Move to ensure correct line connection order
df_plot = df_plot.sort_values("Oracle Move (%)")

# Calculate Perp Bankruptcy AFTER sorting
df_plot["Perp Bankruptcy ($)"] = (
df_plot["Total Bankruptcy ($)"] - df_plot["Spot Bankruptcy ($)"]
)

# Create the figure with sorted data
fig = go.Figure()
for column in [
"Total Bankruptcy ($)",
Expand All @@ -92,6 +98,7 @@ def price_shock_plot(user_leverages, oracle_distort: float):
xaxis_title="Oracle Move (%)",
yaxis_title="Bankruptcy ($)",
legend_title="Bankruptcy Type",
template="plotly_dark",
)

return fig
Expand Down Expand Up @@ -151,19 +158,14 @@ def price_shock_page():

fig = price_shock_plot(result, oracle_distort)
st.plotly_chart(fig)

oracle_down_max = pd.DataFrame(
result["leverages_down"][-1][-1], index=result["user_keys"]
)
oracle_down_max = pd.DataFrame(result["leverages_down"][-1])
with st.expander(
str("oracle down max bankrupt count=")
+ str(len(oracle_down_max[oracle_down_max.net_usd_value < 0]))
):
st.dataframe(oracle_down_max)

oracle_up_max = pd.DataFrame(
result["leverages_up"][-1][-1], index=result["user_keys"]
)
oracle_up_max = pd.DataFrame(result["leverages_up"][-1], index=result["user_keys"])
with st.expander(
str("oracle up max bankrupt count=")
+ str(len(oracle_up_max[oracle_up_max.net_usd_value < 0]))
Expand Down

0 comments on commit 0f79b63

Please sign in to comment.