Spaces:
Running
Running
Khanna, Videh Rakesh Rakesh Claude Sonnet 5.5 commited on
Commit ·
51603e5
1
Parent(s): 66fc4a7
F&O: add F&O-only refresh button; start request timeouts after queue for charts and price polls
Browse filesAdds a 'Refresh F&O' button that re-quotes and re-forecasts only the watchlist's
contract cards. Contract price polls now run ahead of equity charts/AI so the
60s refresh is not starved, and the chart loader's 30s timeout starts when the
request leaves the queue instead of when it is created.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- static/app.js +27 -9
- static/style.css +1 -0
- templates/index.html +6 -3
static/app.js
CHANGED
|
@@ -50,7 +50,8 @@ function _fetchBackgroundJson(url, options = {}, priority = 0, isCurrent = () =>
|
|
| 50 |
// other background requests is never charged against the request's own deadline.
|
| 51 |
const res = await fetch(url, {
|
| 52 |
...fetchOptions,
|
| 53 |
-
signal: fetchOptions.signal
|
|
|
|
| 54 |
});
|
| 55 |
const data = await res.json();
|
| 56 |
return { res, data };
|
|
@@ -404,9 +405,10 @@ async function mountLwChart(containerId, ticker, interval) {
|
|
| 404 |
return;
|
| 405 |
}
|
| 406 |
pending = true;
|
|
|
|
|
|
|
| 407 |
const controller = new AbortController();
|
| 408 |
el._chartAbort = controller;
|
| 409 |
-
const deadline = setTimeout(() => controller.abort(), 30000);
|
| 410 |
try {
|
| 411 |
const { res, data } = await _fetchBackgroundJson(`/api/chart/${encodeURIComponent(ticker)}?interval=${iv}${force ? '&refresh=1' : ''}`, {
|
| 412 |
signal: controller.signal,
|
|
@@ -463,10 +465,9 @@ async function mountLwChart(containerId, ticker, interval) {
|
|
| 463 |
}
|
| 464 |
} catch (e) {
|
| 465 |
if (el._chartGeneration !== generation || !el.isConnected) return;
|
| 466 |
-
status.textContent =
|
| 467 |
status.classList.remove('hidden');
|
| 468 |
} finally {
|
| 469 |
-
clearTimeout(deadline);
|
| 470 |
pending = false;
|
| 471 |
}
|
| 472 |
}
|
|
@@ -2310,8 +2311,9 @@ function _foContractTargetCrossed(pick, price) {
|
|
| 2310 |
&& (price < forecast.target_price_lo || price > forecast.target_price_hi)))));
|
| 2311 |
}
|
| 2312 |
|
| 2313 |
-
async function _refreshWatchlistContracts(force = false) {
|
| 2314 |
-
|
|
|
|
| 2315 |
|| !document.getElementById('view-watchlist')?.classList.contains('active')) return;
|
| 2316 |
const cards = Array.from(document.querySelectorAll('#wl-list [data-contract-id]'));
|
| 2317 |
const loadSeq = _watchlistLoadSeq;
|
|
@@ -2330,10 +2332,10 @@ async function _refreshWatchlistContract(ticker, loadSeq, { quoteOnly = false, f
|
|
| 2330 |
_foContractRequests.set(ticker, loadSeq);
|
| 2331 |
try {
|
| 2332 |
const url = '/api/watchlist-pick/' + encodeURIComponent(ticker) + (quoteOnly ? '?quote_only=1' : force ? '?refresh=1' : '');
|
| 2333 |
-
// Contract requests are bounded server-side (~35s) and a blank
|
| 2334 |
-
// of the equity cards' charts/AI instead of starving behind them
|
| 2335 |
const { res: response, data } = await _fetchBackgroundJson(url, { cache: 'no-store', timeoutMs: 60000 },
|
| 2336 |
-
|
| 2337 |
if (loadSeq !== _watchlistLoadSeq) return;
|
| 2338 |
if (!response.ok || !data.pick) throw new Error(data.error || 'Contract quote unavailable');
|
| 2339 |
const previous = _predictionCache.get(ticker)?.pick;
|
|
@@ -2964,6 +2966,22 @@ document.getElementById('wl-refresh-btn')?.addEventListener('click', () => {
|
|
| 2964 |
loadWatchlist(true);
|
| 2965 |
});
|
| 2966 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2967 |
async function retryWatchlistCard(ticker, btnEl) {
|
| 2968 |
const card = document.querySelector(`#wl-list [data-ticker="${ticker}"]`);
|
| 2969 |
if (!card) return;
|
|
|
|
| 50 |
// other background requests is never charged against the request's own deadline.
|
| 51 |
const res = await fetch(url, {
|
| 52 |
...fetchOptions,
|
| 53 |
+
signal: fetchOptions.signal ? AbortSignal.any([fetchOptions.signal, AbortSignal.timeout(timeoutMs)])
|
| 54 |
+
: AbortSignal.timeout(timeoutMs),
|
| 55 |
});
|
| 56 |
const data = await res.json();
|
| 57 |
return { res, data };
|
|
|
|
| 405 |
return;
|
| 406 |
}
|
| 407 |
pending = true;
|
| 408 |
+
// The controller only cancels this request when the chart is remounted; the 30s timeout is applied by
|
| 409 |
+
// _fetchBackgroundJson once the request leaves the queue (charts are the lowest priority).
|
| 410 |
const controller = new AbortController();
|
| 411 |
el._chartAbort = controller;
|
|
|
|
| 412 |
try {
|
| 413 |
const { res, data } = await _fetchBackgroundJson(`/api/chart/${encodeURIComponent(ticker)}?interval=${iv}${force ? '&refresh=1' : ''}`, {
|
| 414 |
signal: controller.signal,
|
|
|
|
| 465 |
}
|
| 466 |
} catch (e) {
|
| 467 |
if (el._chartGeneration !== generation || !el.isConnected) return;
|
| 468 |
+
status.textContent = ['AbortError', 'TimeoutError'].includes(e.name) ? 'Chart request timed out. Select an interval to retry.' : e.message || 'Chart unavailable. Select an interval to retry.';
|
| 469 |
status.classList.remove('hidden');
|
| 470 |
} finally {
|
|
|
|
| 471 |
pending = false;
|
| 472 |
}
|
| 473 |
}
|
|
|
|
| 2311 |
&& (price < forecast.target_price_lo || price > forecast.target_price_hi)))));
|
| 2312 |
}
|
| 2313 |
|
| 2314 |
+
async function _refreshWatchlistContracts(force = false, { manual = false } = {}) {
|
| 2315 |
+
// A manual click also works outside market hours (the server then serves the last NSE snapshot).
|
| 2316 |
+
if (document.visibilityState !== 'visible' || (!manual && !_isMarketHoursIST())
|
| 2317 |
|| !document.getElementById('view-watchlist')?.classList.contains('active')) return;
|
| 2318 |
const cards = Array.from(document.querySelectorAll('#wl-list [data-contract-id]'));
|
| 2319 |
const loadSeq = _watchlistLoadSeq;
|
|
|
|
| 2332 |
_foContractRequests.set(ticker, loadSeq);
|
| 2333 |
try {
|
| 2334 |
const url = '/api/watchlist-pick/' + encodeURIComponent(ticker) + (quoteOnly ? '?quote_only=1' : force ? '?refresh=1' : '');
|
| 2335 |
+
// Contract requests (quote polls included, ~1s) are bounded server-side (~35s) and a blank or stale
|
| 2336 |
+
// card is useless, so they go ahead of the equity cards' charts/AI instead of starving behind them.
|
| 2337 |
const { res: response, data } = await _fetchBackgroundJson(url, { cache: 'no-store', timeoutMs: 60000 },
|
| 2338 |
+
0, () => loadSeq === _watchlistLoadSeq);
|
| 2339 |
if (loadSeq !== _watchlistLoadSeq) return;
|
| 2340 |
if (!response.ok || !data.pick) throw new Error(data.error || 'Contract quote unavailable');
|
| 2341 |
const previous = _predictionCache.get(ticker)?.pick;
|
|
|
|
| 2966 |
loadWatchlist(true);
|
| 2967 |
});
|
| 2968 |
|
| 2969 |
+
// F&O-only refresh: re-quotes and re-forecasts the watchlist's option/futures cards without reloading
|
| 2970 |
+
// every equity card (which would re-run their AI).
|
| 2971 |
+
document.getElementById('wl-refresh-fo-btn')?.addEventListener('click', async event => {
|
| 2972 |
+
const button = event.currentTarget;
|
| 2973 |
+
const label = button.textContent;
|
| 2974 |
+
button.disabled = true;
|
| 2975 |
+
button.textContent = 'Refreshing…';
|
| 2976 |
+
try {
|
| 2977 |
+
_foProviderRetries.clear();
|
| 2978 |
+
await _refreshWatchlistContracts(true, { manual: true });
|
| 2979 |
+
} finally {
|
| 2980 |
+
button.disabled = false;
|
| 2981 |
+
button.textContent = label;
|
| 2982 |
+
}
|
| 2983 |
+
});
|
| 2984 |
+
|
| 2985 |
async function retryWatchlistCard(ticker, btnEl) {
|
| 2986 |
const card = document.querySelector(`#wl-list [data-ticker="${ticker}"]`);
|
| 2987 |
if (!card) return;
|
static/style.css
CHANGED
|
@@ -165,6 +165,7 @@ body {
|
|
| 165 |
|
| 166 |
.view-header { margin-bottom: 24px; }
|
| 167 |
.view-header--row { display: flex; align-items: flex-start; justify-content: space-between; }
|
|
|
|
| 168 |
.view-title { font-size: 22px; font-weight: 700; letter-spacing: -.5px; }
|
| 169 |
.view-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 4px; max-width: 680px; }
|
| 170 |
|
|
|
|
| 165 |
|
| 166 |
.view-header { margin-bottom: 24px; }
|
| 167 |
.view-header--row { display: flex; align-items: flex-start; justify-content: space-between; }
|
| 168 |
+
.view-header-actions { display: flex; gap: 8px; flex-wrap: wrap; }
|
| 169 |
.view-title { font-size: 22px; font-weight: 700; letter-spacing: -.5px; }
|
| 170 |
.view-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 4px; max-width: 680px; }
|
| 171 |
|
templates/index.html
CHANGED
|
@@ -33,7 +33,7 @@
|
|
| 33 |
@keyframes apl-spin { to { transform: rotate(360deg); } }
|
| 34 |
.apl-hint { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 11px; color: #5a6278; }
|
| 35 |
</style>
|
| 36 |
-
<link rel="stylesheet" href="/static/style.css?v=20261103fo-
|
| 37 |
<script src="/static/vendor/lightweight-charts-4.2.0.js"></script>
|
| 38 |
</head>
|
| 39 |
<body>
|
|
@@ -171,7 +171,10 @@
|
|
| 171 |
<h1 class="view-title">Watchlist</h1>
|
| 172 |
<p class="view-subtitle">Stocks you're monitoring — Today / 1D predictions with entry, stop-loss, and target.</p>
|
| 173 |
</div>
|
| 174 |
-
<
|
|
|
|
|
|
|
|
|
|
| 175 |
</div>
|
| 176 |
|
| 177 |
<div class="toolbar">
|
|
@@ -541,6 +544,6 @@
|
|
| 541 |
</a>
|
| 542 |
</nav>
|
| 543 |
|
| 544 |
-
<script src="/static/app.js?v=20261103fo-
|
| 545 |
</body>
|
| 546 |
</html>
|
|
|
|
| 33 |
@keyframes apl-spin { to { transform: rotate(360deg); } }
|
| 34 |
.apl-hint { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 11px; color: #5a6278; }
|
| 35 |
</style>
|
| 36 |
+
<link rel="stylesheet" href="/static/style.css?v=20261103fo-refresh-btn">
|
| 37 |
<script src="/static/vendor/lightweight-charts-4.2.0.js"></script>
|
| 38 |
</head>
|
| 39 |
<body>
|
|
|
|
| 171 |
<h1 class="view-title">Watchlist</h1>
|
| 172 |
<p class="view-subtitle">Stocks you're monitoring — Today / 1D predictions with entry, stop-loss, and target.</p>
|
| 173 |
</div>
|
| 174 |
+
<div class="view-header-actions">
|
| 175 |
+
<button class="btn-ghost" id="wl-refresh-fo-btn" title="Re-quote and re-forecast only the F&O contracts">Refresh F&O</button>
|
| 176 |
+
<button class="btn-ghost" id="wl-refresh-btn">Refresh</button>
|
| 177 |
+
</div>
|
| 178 |
</div>
|
| 179 |
|
| 180 |
<div class="toolbar">
|
|
|
|
| 544 |
</a>
|
| 545 |
</nav>
|
| 546 |
|
| 547 |
+
<script src="/static/app.js?v=20261103fo-refresh-btn"></script>
|
| 548 |
</body>
|
| 549 |
</html>
|