Skip to content

Commit

Permalink
Update pages to use backend api"
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 2, 2024
1 parent 6d665d9 commit fd20217
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 572 deletions.
50 changes: 50 additions & 0 deletions src/lib/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Optional

import pandas as pd
import requests


BASE_URL = "http://localhost:8000"


def api(
section: str,
path: str,
path_extra_1: Optional[str] = None,
path_extra_2: Optional[
str
] = None, # TODO: this is pretty silly, but it works for now
as_json: bool = False,
params: Optional[dict] = None,
):
"""
Fetches data from the backend API. To find the corresponding
path, look at the `backend/api/` directory. It should be setup
so that the `section` is the name of the file, the `path` is the
function inside the file.
Args:
section (str): The section of the API to fetch from.
path (str): The path of the API to fetch from.
path_extra (Optional[str]): An optional extra path to append to the path.
as_json (bool): Whether to return the response as JSON.
Returns:
The response from the API.
"""
if path_extra_1:
path = f"{path}/{path_extra_1}"
if path_extra_2:
path = f"{path}/{path_extra_2}"
if params:
response = requests.get(f"{BASE_URL}/api/{section}/{path}", params=params)
else:
response = requests.get(f"{BASE_URL}/api/{section}/{path}")

if as_json:
return response.json()

try:
return pd.DataFrame(response.json())
except ValueError:
return response.json()
279 changes: 0 additions & 279 deletions src/lib/health_utils.py

This file was deleted.

20 changes: 17 additions & 3 deletions src/lib/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
"""

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

import streamlit as st
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 load_newest_files, load_vat

RPC_STATE_KEY = "rpc_url"
NETWORK_STATE_KEY = "network"
Expand Down Expand Up @@ -43,6 +48,15 @@ def sidebar():
f"Have VAT? {'✅' if st.session_state[VAT_STATE_KEY] is not None else 'Not Loaded'} "
)

metadata = api("metadata", "", as_json=True)
pickle_file = metadata["pickle_file"]
pickle_file = pickle_file.split("/")[-1]
timestamp = pickle_file.split("-")[1:]
timestamp = datetime.strptime(" ".join(timestamp), "%Y %m %d %H %M %S")
time_ago = datetime.now() - timestamp
time_ago_str = humanize.precisedelta(time_ago, minimum_unit="minutes")
st.sidebar.write(f"Last snapshot taken at: {timestamp} ({time_ago_str} ago)")


def needs_rpc_and_vat(page_callable: callable):
"""
Expand Down
Loading

0 comments on commit fd20217

Please sign in to comment.