Skip to content

Commit 9cd7c1c

Browse files
committed
Improve performance and add caching
1 parent 0008f84 commit 9cd7c1c

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

src/apps/treasury/index.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import dash
22
from dash_extensions.enrich import html
3+
from flask_caching import Cache
4+
35
from src.apps.treasury.app import app
6+
from src.apps.treasury.util.constants import CACHE_TIMEOUT
47

58
CONTENT_STYLE = {
69
"position": "relative",
@@ -19,9 +22,24 @@
1922
"background-color": "#FFF",
2023
}
2124

22-
app.layout = html.Div([
23-
dash.page_container
24-
])
25+
cache = Cache(
26+
app.server,
27+
config={
28+
'CACHE_TYPE': 'filesystem',
29+
'CACHE_DIR': '/tmp/cache-directory',
30+
'CACHE_DEFAULT_TIMEOUT': CACHE_TIMEOUT
31+
}
32+
)
33+
34+
35+
@cache.memoize()
36+
def get_layout():
37+
return html.Div([
38+
dash.page_container
39+
])
40+
41+
42+
app.layout = get_layout
2543

2644
# For Gunicorn to reference
2745
server = app.server

src/apps/treasury/pages/hud.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@
1616

1717
dash.register_page(__name__, title="KlimaDAO Treasury Heads Up Display")
1818

19-
# TODO: add caching
2019

21-
# TODO: combine repetitive queries to `last_metric` into a single DF call and unpack instead
22-
# last_metric_df = sg.query_df([last_metric.marketCap, last_metric.treasuryMarketValue, ...])
20+
def query_last_metric():
21+
df = sg.query_df([
22+
last_metric.marketCap,
23+
last_metric.treasuryMarketValue,
24+
last_metric.daoBalanceUSDC, last_metric.treasuryBalanceUSDC,
25+
last_metric.treasuryMarketValue, last_metric.daoBalanceKLIMA,
26+
last_metric.klimaPrice, last_metric.totalSupply
27+
])
28+
29+
df = df.rename({
30+
c: c.replace('protocolMetrics_', '') for c in df.columns
31+
}, axis=1)
32+
33+
return df
34+
35+
36+
last_metric_df = query_last_metric()
37+
38+
mcap = last_metric_df.loc[0, 'marketCap']
39+
mval = last_metric_df.loc[0, 'treasuryMarketValue']
2340

2441
# Market Cap indicator
2542
metric_fig = go.Figure(
2643
go.Indicator(
2744
mode="number",
28-
value=sg.query([last_metric.marketCap]),
45+
value=mcap,
2946
number={"prefix": "$", "valueformat": ".2s", "font": {"family": "Poppins-ExtraBold"}},
3047
title={
3148
"text":
@@ -43,7 +60,7 @@
4360
# Total Treasury Market Value ($) indicator
4461
go.Indicator(
4562
mode="number",
46-
value=sg.query([last_metric.treasuryMarketValue]),
63+
value=mval,
4764
number={"prefix": "$", "valueformat": ".2s", "font": {"family": "Poppins-ExtraBold"}},
4865
title={
4966
"text":
@@ -81,28 +98,26 @@
8198
total_illiquid_spot = illiquid_assets[illiquid_assets.Is_Spot]["Dollars"].sum()
8299

83100
# Pull sum of DAO and Treasury USDC holdings for OpEx
84-
dao_usdc, treasury_usdc = sg.query([last_metric.daoBalanceUSDC, last_metric.treasuryBalanceUSDC])
101+
dao_usdc = last_metric_df.loc[0, 'daoBalanceUSDC']
102+
treasury_usdc = last_metric_df.loc[0, 'treasuryBalanceUSDC']
85103
total_usdc = dao_usdc + treasury_usdc
86104

87105
# Total treasury market value
88106
# TODO: check that treasuryMarketValue field includes all assets (raw carbon, all LPs, raw KLIMA)
89107
# TODO: add Solid World CRISP-C LP value
90108
# TODO: add offchain assets (added illiquid spot tonnes, may be other assets)
91-
(treasury_value, dao_klima, klima_price) = sg.query([
92-
last_metric.treasuryMarketValue, last_metric.daoBalanceKLIMA,
93-
last_metric.klimaPrice
94-
])
109+
treasury_value = last_metric_df.loc[0, 'treasuryMarketValue']
110+
dao_klima = last_metric_df.loc[0, 'daoBalanceKLIMA']
111+
klima_price = last_metric_df.loc[0, 'klimaPrice']
95112

96113
# Add DAO KLIMA holdings since it is not considered part of OpEx bucket
97114
total_treasury_value = (
98115
treasury_value + (dao_klima * klima_price) + total_illiquid_spot
99116
)
100117

101-
# TODO: make sure treasury KLIMA is included in treasuryMarketValue
102-
103118
# Separate out enough lowest price carbon tonnes from total treasury value
104119
# to back all KLIMA supply as Carbon Backing
105-
klima_supply = sg.query([last_metric.totalSupply])
120+
klima_supply = last_metric_df.loc[0, 'totalSupply']
106121

107122
# TODO: Replace hard-coded BCT with dynamic check for lowest priced tonne
108123
lowest_price_carbon_addr = BCT_ERC20_CONTRACT

src/apps/treasury/util/constants.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717
# Colors
1818
KLIMA_GREEN = '#00cc33'
19+
20+
CACHE_TIMEOUT = 60 * 60 # 1 hour

0 commit comments

Comments
 (0)