Skip to content

Commit

Permalink
update backend api to have LRU cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 21, 2024
1 parent 03bfbf3 commit be7e249
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
31 changes: 19 additions & 12 deletions backend/api/asset_liability.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
router = APIRouter()


@router.get("/matrix/{mode}/{perp_market_index}")
async def get_asset_liability_matrix(
request: BackendRequest, mode: int, perp_market_index: int
):
backend_state: BackendState = request.state.backend_state
vat: Vat = backend_state.vat

with waiting_for("Getting asset liability matrix"):
res, df = await get_matrix(vat, mode, perp_market_index)

with waiting_for("Converting to dict"):
res_dict = res.to_dict()
df_dict = df.to_dict()
snapshot_path: str, vat: Vat, mode: int, perp_market_index: int
) -> dict:
print("==> Getting asset liability matrix...")
res, df = await get_matrix(vat, mode, perp_market_index)
res_dict = res.to_dict()
df_dict = df.to_dict()
print("==> Asset liability matrix fetched")

return {
"res": res_dict,
"df": df_dict,
}


@router.get("/matrix/{mode}/{perp_market_index}")
async def get_asset_liability_matrix(
request: BackendRequest, mode: int, perp_market_index: int
):
return await get_asset_liability_matrix(
request.state.current_pickle_path,
request.state.backend_state.vat,
mode,
perp_market_index,
)
32 changes: 23 additions & 9 deletions backend/api/price_shock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
from typing import Optional

from backend.state import BackendRequest
Expand All @@ -11,23 +12,36 @@
router = APIRouter()


@functools.lru_cache()
async def _get_price_shock(
snapshot_path: str,
vat: Vat,
drift_client: DriftClient,
oracle_distortion: float = 0.1,
asset_group: Optional[str] = None,
n_scenarios: int = 5,
) -> dict:
return get_user_leverages_for_price_shock(
drift_client,
vat.users,
oracle_distortion,
asset_group,
n_scenarios,
)


@router.get("/usermap")
async def get_price_shock(
request: BackendRequest,
oracle_distortion: float = 0.1,
asset_group: Optional[str] = None,
n_scenarios: int = 5,
):
backend_state: BackendState = request.state.backend_state
vat: Vat = backend_state.vat
drift_client: DriftClient = backend_state.dc

result = get_user_leverages_for_price_shock(
drift_client,
vat.users,
return await _get_price_shock(
request.state.backend_state.current_pickle_path,
request.state.backend_state.vat,
request.state.backend_state.dc,
oracle_distortion,
asset_group,
n_scenarios,
)

return result

0 comments on commit be7e249

Please sign in to comment.