Skip to content

Commit 62bc25a

Browse files
Ryan McCombclaude
authored andcommitted
Speed up chart loading (smarter cache) and fix glitchy period buttons
- Chart cache: 60s TTL -> 5min with file-size check (only reprocess when new data arrives) - Period buttons: add debounce, loading state, instant visual feedback - No data file changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent abe3f4f commit 62bc25a

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

app.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def rdp_simplify(points, epsilon):
612612

613613

614614
# ===== CHART DATA CACHE =====
615-
_chart_cache = {'data': None, 'time': 0, 'key': None}
615+
_chart_cache = {'data': None, 'time': 0, 'key': None, 'file_size': 0}
616616

617617

618618
# ===== EMAIL ALERT FUNCTIONS =====
@@ -2100,9 +2100,15 @@ def get_snapshots_chart():
21002100
epsilon = float(request.args.get('epsilon', '0.5'))
21012101
cache_key = f'{period}:{epsilon}'
21022102

2103-
# 60-second cache
2103+
# Cache: serve cached result if same params AND file hasn't grown
21042104
now = _time.time()
2105-
if _chart_cache['key'] == cache_key and _chart_cache['data'] and (now - _chart_cache['time']) < 60:
2105+
try:
2106+
current_file_size = os.path.getsize(HISTORICAL_DATA_PATH)
2107+
except OSError:
2108+
current_file_size = 0
2109+
if (_chart_cache['key'] == cache_key and _chart_cache['data']
2110+
and _chart_cache['file_size'] == current_file_size
2111+
and (now - _chart_cache['time']) < 300):
21062112
return jsonify(_chart_cache['data'])
21072113

21082114
# Read all snapshots
@@ -2260,7 +2266,7 @@ def get_snapshots_chart():
22602266
}
22612267

22622268
# Cache and return
2263-
_chart_cache = {'data': result, 'time': now, 'key': cache_key}
2269+
_chart_cache = {'data': result, 'time': now, 'key': cache_key, 'file_size': current_file_size}
22642270

22652271
resp = jsonify(result)
22662272
resp.headers['Cache-Control'] = 'public, max-age=30'

templates/markets.html

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ <h4>Disclaimer</h4>
924924
background: var(--bg-surface-hover);
925925
}
926926

927+
.toggle-btn:active {
928+
transform: scale(0.96);
929+
}
930+
927931
.toggle-btn.active {
928932
background: var(--bg-surface);
929933
color: var(--text-primary);
@@ -2947,16 +2951,27 @@ <h4>Disclaimer</h4>
29472951

29482952
document.addEventListener('DOMContentLoaded', () => {
29492953
// Set up chart toggle buttons
2954+
var _chartLoading = false;
29502955
document.querySelectorAll('.toggle-btn').forEach(btn => {
29512956
btn.addEventListener('click', async (e) => {
2952-
// Update button states
2957+
if (_chartLoading) return; // debounce
2958+
var clicked = e.target.closest('.toggle-btn');
2959+
if (!clicked || clicked.classList.contains('active')) return;
2960+
2961+
// Update button states immediately
29532962
document.querySelectorAll('.toggle-btn').forEach(b => b.classList.remove('active'));
2954-
e.target.classList.add('active');
2963+
clicked.classList.add('active');
2964+
clicked.style.opacity = '0.6';
29552965

2956-
currentChartPeriod = e.target.dataset.period;
2966+
currentChartPeriod = clicked.dataset.period;
2967+
_chartLoading = true;
29572968

2958-
// Render chart with the selected period (handles both 30d and 1h)
2959-
await renderMarketTrendChart(candidatesData);
2969+
try {
2970+
await renderMarketTrendChart(candidatesData);
2971+
} finally {
2972+
_chartLoading = false;
2973+
clicked.style.opacity = '';
2974+
}
29602975
});
29612976
});
29622977

0 commit comments

Comments
 (0)