From 8adda5e0c6e2303f0aa2e1b681966d6b36d2ab39 Mon Sep 17 00:00:00 2001 From: terraputix Date: Mon, 2 Feb 2026 21:12:57 +0100 Subject: [PATCH 01/17] add prefetch button --- src/lib/components/time/time-selector.svelte | 122 +++++++++++++ src/lib/index.ts | 4 +- src/lib/prefetch.ts | 172 +++++++++++++++++++ 3 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 src/lib/prefetch.ts diff --git a/src/lib/components/time/time-selector.svelte b/src/lib/components/time/time-selector.svelte index 6006903b..2c9bb748 100644 --- a/src/lib/components/time/time-selector.svelte +++ b/src/lib/components/time/time-selector.svelte @@ -16,6 +16,7 @@ selectedDomain, variableSelectionOpen } from '$lib/stores/variables'; + import { domain as domainStore, variable as variableStore } from '$lib/stores/variables'; import * as Select from '$lib/components/ui/select'; @@ -26,6 +27,12 @@ MILLISECONDS_PER_HOUR, MILLISECONDS_PER_WEEK } from '$lib/constants'; + import { + type PrefetchMode, + getDateRangeForMode, + getPrefetchModeLabel, + prefetchData + } from '$lib/prefetch'; import { formatISOWithoutTimezone, formatLocalDate, @@ -338,6 +345,60 @@ } }; + // State to track prefetch progress and mode + let isPrefetching = $state(false); + let prefetchProgress = $state({ current: 0, total: 0 }); + let selectedPrefetchMode: PrefetchMode = $state('next24h'); + + const prefetchModes: { value: PrefetchMode; label: string }[] = [ + { value: 'next24h', label: 'Next 24h' }, + { value: 'prev24h', label: 'Prev 24h' }, + { value: 'completeModelRun', label: 'Full run' } + ]; + + // Prefetch data using the selected mode + const handlePrefetch = async () => { + if (!$metaJson || !$modelRun) { + toast.warning('No metadata available for prefetching'); + return; + } + + if (isPrefetching) { + toast.warning('Prefetch already in progress'); + return; + } + + isPrefetching = true; + prefetchProgress = { current: 0, total: 0 }; + + const modeLabel = getPrefetchModeLabel(selectedPrefetchMode); + toast.info(`Prefetching ${modeLabel}...`); + + const { startDate, endDate } = getDateRangeForMode(selectedPrefetchMode, $time, $metaJson); + + const result = await prefetchData( + { + startDate, + endDate, + metaJson: $metaJson, + modelRun: $modelRun, + domain: $domainStore, + variable: $variableStore + }, + (progress) => { + prefetchProgress = progress; + } + ); + + isPrefetching = false; + + if (result.success) { + toast.success(`Prefetched ${result.successCount}/${result.totalCount} time steps`); + } else { + toast.error(result.error || 'Prefetch failed'); + } + }; + // throttled versions of the navigation functions const throttledPreviousHour = throttle(previousHour, 150); const throttledNextHour = throttle(nextHour, 150); @@ -900,6 +961,67 @@ + + + + + { + if (v) { + selectedPrefetchMode = v as PrefetchMode; + } + }} + > + + {prefetchModes.find((m) => m.value === selectedPrefetchMode)?.label ?? 'Next 24h'} + + + {#each prefetchModes as mode (mode.value)} + + {mode.label} + + {/each} + + +