Skip to content

Commit

Permalink
feat: add hourly resolution to snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
banteg committed Mar 27, 2021
1 parent 129a4b2 commit f826a9d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scripts/historical_tvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def generate_snapshot_range(start, interval):
def main():
yearn = Yearn()
start = datetime(2020, 2, 12, tzinfo=timezone.utc) # first iearn deployment
interval = timedelta(days=1)
interval = timedelta(hours=1)
buffer = timedelta(minutes=5)
synced = False
for snapshot in generate_snapshot_range(start, interval):
Expand Down
40 changes: 31 additions & 9 deletions yearn/api.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
from collections import defaultdict

from cachetools.func import ttl_cache
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException

from yearn.entities import Snapshot, db_session, select

app = FastAPI(title="Yearn Exporter")
tree = lambda: defaultdict(tree)
ALLOWED_HOURLY_RESOLUTION = {1, 2, 4, 6, 8, 12, 24}


@ttl_cache(600)
def get_daily_tvl():
def get_aggregated_tvl_data(hourly_resolution):
with db_session:
return select(
(snap.block.snapshot.date(), sum(snap.assets)) for snap in Snapshot
(snap.block.snapshot, sum(snap.assets))
for snap in Snapshot
if int(snap.block.snapshot.hour) % hourly_resolution == 0
).order_by(1)[:]


@ttl_cache(600)
def get_daily_tvl_detailed():
def get_latest_tvl_data():
with db_session:
return select((s.block.snapshot, sum(s.assets)) for s in Snapshot).order_by(-1).first()


@ttl_cache(600)
def get_detailed_tvl_data(hourly_resolution):
data = tree()
with db_session:
for timestamp, product, name, assets in select(
(snap.block.snapshot.date(), snap.product, snap.name, snap.assets) for snap in Snapshot
(snap.block.snapshot, snap.product, snap.name, snap.assets)
for snap in Snapshot
if int(snap.block.snapshot.hour) % hourly_resolution == 0
).order_by(1):
if assets > 0:
data[timestamp][product][name] = assets
return data


@app.get("/v1/tvl", name="tvl")
def read_daily_tvl():
def read_daily_tvl(hourly_resolution: int = 24):
"""Daily historical TVL snapshot."""
return get_daily_tvl()
if hourly_resolution not in ALLOWED_HOURLY_RESOLUTION:
raise HTTPException(400, f"hourly_resolution must be {ALLOWED_HOURLY_RESOLUTION}")
return get_aggregated_tvl_data(hourly_resolution)


@app.get("/v1/tvl/latest", name="tvl latest")
def read_latest_tvl():
"""Latest hourly TVL snapshot."""
data = get_latest_tvl_data()
return {"date": data[0], "tvl": data[1]}


@app.get("/v1/tvl/detailed", name="tvl detailed")
def read_daily_tvl_detailed():
def read_daily_tvl_detailed(hourly_resolution: int = 24):
"""Detailed daily historical TVL snapshot broken down by product and contract."""
return get_daily_tvl_detailed()
if hourly_resolution not in ALLOWED_HOURLY_RESOLUTION:
raise HTTPException(400, f"hourly_resolution must be {ALLOWED_HOURLY_RESOLUTION}")
return get_detailed_tvl_data(hourly_resolution)

0 comments on commit f826a9d

Please sign in to comment.