Skip to content

Commit

Permalink
Update fetch_with_retry to repeatedly fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 17, 2024
1 parent b2ce677 commit 03bfbf3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
14 changes: 3 additions & 11 deletions src/lib/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
Common page functionality
"""

import asyncio
from asyncio import AbstractEventLoop
from datetime import datetime
import os
import time

from anchorpy import Wallet
from driftpy.account_subscription_config import AccountSubscriptionConfig
from driftpy.drift_client import DriftClient
import humanize
from lib.api import api
from solana.rpc.async_api import AsyncClient
import streamlit as st

from utils import load_newest_files
from utils import load_vat
from utils import fetch_result_with_retry


RPC_STATE_KEY = "rpc_url"
Expand All @@ -33,12 +25,12 @@ def header():
def sidebar():
st.sidebar.header("Data Information")
try:
metadata = api("metadata", "", as_json=True)
metadata = fetch_result_with_retry(api, "metadata", "", as_json=True)
pickle_file = metadata["pickle_file"]
if pickle_file[-1] == "/":
pickle_file = pickle_file[:-1]
timestamp = pickle_file.split("-")[1:]
timestamp = datetime.strptime(" ".join(timestamp), "%Y %m %d %H %M %S")
timestamp = datetime.strptime(" ".join(timestamp[-6:]), "%Y %m %d %H %M %S")
time_ago = datetime.now() - timestamp
time_ago_str = humanize.precisedelta(
time_ago,
Expand Down
20 changes: 15 additions & 5 deletions src/page/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
import plotly.express as px
import streamlit as st

from utils import fetch_result_with_retry


def health_page():
health_distribution = api("health", "health_distribution")
largest_perp_positions = api("health", "largest_perp_positions")
most_levered_positions = api("health", "most_levered_perp_positions_above_1m")
largest_spot_borrows = api("health", "largest_spot_borrows")
most_levered_borrows = api("health", "most_levered_spot_borrows_above_1m")
health_distribution = fetch_result_with_retry(api, "health", "health_distribution")
largest_perp_positions = fetch_result_with_retry(
api, "health", "largest_perp_positions"
)
most_levered_positions = fetch_result_with_retry(
api, "health", "most_levered_perp_positions_above_1m"
)
largest_spot_borrows = fetch_result_with_retry(
api, "health", "largest_spot_borrows"
)
most_levered_borrows = fetch_result_with_retry(
api, "health", "most_levered_spot_borrows_above_1m"
)

fig = px.bar(
health_distribution,
Expand Down
23 changes: 19 additions & 4 deletions src/page/liquidation_curves.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from collections import defaultdict
import time

from lib.api import api
import numpy as np
import plotly.graph_objects as go
import streamlit as st

from utils import fetch_result_with_retry


def plot_liquidation_curves(liquidation_data):
liquidations_long = liquidation_data["liquidations_long"]
Expand Down Expand Up @@ -119,12 +122,24 @@ def liquidation_curves_page():
index=options.index(market_index),
)

# Update query parameters
st.query_params.update({"market_index": market_index})

liquidation_data = api(
"liquidation", "liquidation-curve", str(market_index), as_json=True
)
try:
liquidation_data = fetch_result_with_retry(
api, "liquidation", "liquidation-curve", str(market_index), as_json=True
)
if liquidation_data is None:
st.write("Fetching data for the first time...")
st.image(
"https://i.gifer.com/origin/8a/8a47f769c400b0b7d81a8f6f8e09a44a_w200.gif"
)
st.write("Check again in one minute!")
st.stop()

except Exception as e:
st.write(e)
st.stop()

(long_fig, short_fig) = plot_liquidation_curves(liquidation_data)

long_col, short_col = st.columns([1, 1])
Expand Down
11 changes: 11 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time
from typing import Optional

from driftpy.drift_client import DriftClient
Expand Down Expand Up @@ -92,3 +93,13 @@ async def load_vat(dc: DriftClient, pickle_map: dict[str, str]) -> Vat:
)

return vat


def fetch_result_with_retry(func, *args, **kwargs):
for _ in range(10):
result = func(*args, **kwargs)
if not ("result" in result and result["result"] == "miss"):
return result
time.sleep(0.5)
print(f"Fetching result with retry for {func.__name__}{args} did not succeed")
return None

0 comments on commit 03bfbf3

Please sign in to comment.