Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 43 additions & 5 deletions apps/web/src/pages/data.astro
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,57 @@ const content = {
}
}

async function loadMetrics() {
const METRICS_CACHE_KEY = 'capgo-live-update-metrics-v1'
const METRICS_CACHE_TTL_MS = 5 * 60 * 1000

function readCachedMetrics(): Metrics | null {
try {
const raw = sessionStorage.getItem(METRICS_CACHE_KEY)
if (!raw) return null
const parsed = JSON.parse(raw) as { savedAt?: unknown; payload?: unknown }
if (typeof parsed.savedAt !== 'number' || Date.now() - parsed.savedAt > METRICS_CACHE_TTL_MS) return null
return normalizeMetrics(parsed.payload)
Comment on lines +315 to +316

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant file and inspect the metrics cache logic.
printf '\n== file list ==\n'
git ls-files 'apps/web/src/pages/data.astro' 'apps/web/src/**/config*' 'apps/web/src/**/*metrics*' | sed 's#^`#-` #'

printf '\n== relevant lines in apps/web/src/pages/data.astro ==\n'
nl -ba apps/web/src/pages/data.astro | sed -n '260,340p'

printf '\n== search for METRICS_CACHE_TTL_MS and savedAt usage ==\n'
rg -n "METRICS_CACHE_TTL_MS|savedAt|normalizeMetrics|cache" apps/web/src/pages/data.astro apps/web/src -g '!**/dist/**' -g '!**/build/**' || true

Repository: Cap-go/website

Length of output: 565


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== data.astro excerpt ==\n'
sed -n '280,340p' apps/web/src/pages/data.astro

printf '\n== savedAt / metrics cache references ==\n'
rg -n "savedAt|METRICS_CACHE_TTL_MS|normalizeMetrics|metrics cache|cache" apps/web/src/pages/data.astro apps/web/src -g '!**/dist/**' -g '!**/build/**' || true

Repository: Cap-go/website

Length of output: 50370


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '300,335p' apps/web/src/pages/data.astro

printf '\n--\n'

rg -n "savedAt|METRICS_CACHE_TTL_MS|normalizeMetrics" apps/web/src/pages/data.astro

Repository: Cap-go/website

Length of output: 2069


Reject future-dated cache entries. savedAt values ahead of Date.now() bypass this TTL check, so stale metrics can be treated as fresh when revalidation fails. Add a savedAt <= now guard before accepting the cache.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/web/src/pages/data.astro` around lines 315 - 316, Update the cache
validation around parsed.savedAt in the metrics cache parsing flow to capture
the current time and reject entries whose savedAt is later than that time, in
addition to the existing type and TTL checks. Only call normalizeMetrics for
non-future, non-expired entries.

} catch {
return null
}
}

function writeCachedMetrics(payload: unknown) {
try {
sessionStorage.setItem(METRICS_CACHE_KEY, JSON.stringify({ savedAt: Date.now(), payload }))
} catch {
// Ignore quota / private-mode failures.
}
}

async function fetchMetrics() {
if (!endpoint) throw new Error('Missing metrics endpoint')
const controller = new AbortController()
const timeoutId = window.setTimeout(() => controller.abort(), 10_000)
try {
if (!endpoint) throw new Error('Missing metrics endpoint')
const response = await fetch(endpoint, { headers: { Accept: 'application/json' }, signal: controller.signal })
const metrics = response.ok ? normalizeMetrics(await response.json()) : null
const payload = response.ok ? await response.json() : null
const metrics = normalizeMetrics(payload)
if (!metrics) throw new Error('Invalid metrics response')
writeCachedMetrics(payload)
return metrics
} finally {
window.clearTimeout(timeoutId)
}
}

async function loadMetrics() {
const cached = readCachedMetrics()
if (cached) {
renderMetrics(cached)
page?.setAttribute('aria-busy', 'false')
}
try {
const metrics = await fetchMetrics()
renderMetrics(metrics)
} catch {
renderUnavailable()
if (!cached) renderUnavailable()
} finally {
window.clearTimeout(timeoutId)
page?.setAttribute('aria-busy', 'false')
}
}
Expand Down
Loading