Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: cache image maturity results to improve page loading
Kyle Mendellee6eb1e52d31
diff --git a/src/lib/components/maturity-item.svelte b/src/lib/components/maturity-item.svelteindex 47b366399..c8c5c6294 100644--- a/src/lib/components/maturity-item.svelte+++ b/src/lib/components/maturity-item.svelte@@ -1,6 +1,6 @@<script lang="ts">import * as Tooltip from '$lib/components/ui/tooltip/index.js';- import { CircleCheck, CircleFadingArrowUp, CircleArrowUp } from '@lucide/svelte';+ import { CircleCheck, CircleFadingArrowUp, CircleArrowUp, Loader2 } from '@lucide/svelte';interface Props {maturity?:@@ -11,9 +11,10 @@date?: string;}| undefined;+ isLoadingInBackground?: boolean;}- let { maturity = undefined }: Props = $props();+ let { maturity = undefined, isLoadingInBackground = false }: Props = $props();</script>{#if maturity}@@ -67,6 +68,19 @@</Tooltip.Content></Tooltip.Root></Tooltip.Provider>+{:else if isLoadingInBackground}+ <Tooltip.Provider>+ <Tooltip.Root>+ <Tooltip.Trigger>+ <span class="inline-flex items-center justify-center align-middle mr-2 size-4">+ <Loader2 class="text-blue-400 size-4 animate-spin" />+ </span>+ </Tooltip.Trigger>+ <Tooltip.Content side="right" class="p-2 relative tooltip-with-arrow" align="center">+ <span class="text-xs">Checking maturity status...</span>+ </Tooltip.Content>+ </Tooltip.Root>+ </Tooltip.Provider>{:else}<Tooltip.Provider><Tooltip.Root>diff --git a/src/lib/services/docker/image-service.ts b/src/lib/services/docker/image-service.tsindex 4a28579fa..cedd42888 100644--- a/src/lib/services/docker/image-service.ts+++ b/src/lib/services/docker/image-service.ts@@ -6,6 +6,7 @@ import { parseImageNameForRegistry, areRegistriesEquivalent } from '$lib/utils/rimport { getSettings } from '$lib/services/settings-service';import { updateImageMaturity } from '$lib/stores/maturity-store';import { tryCatch } from '$lib/utils/try-catch';+import { maturityCache } from './maturity-cache-service';let maturityPollingInterval: NodeJS.Timeout | null = null;/**@@ -282,10 +283,16 @@ export async function pullImage(imageRef: string, platform?: string, authConfig?* and returns maturity information.*/export async function checkImageMaturity(imageId: string): Promise<import('$lib/types/docker/image.type').ImageMaturity | undefined> {+ // First check cache+ const cachedMaturity = maturityCache.get(imageId);+ if (cachedMaturity) {+ return cachedMaturity;+ }+const imageResult = await tryCatch(getImage(imageId));if (imageResult.error) {console.warn(`checkImageMaturity: Failed to get image details for ${imageId}:`, imageResult.error);- updateImageMaturity(imageId, undefined); // Ensure store is cleared if image details fail+ maturityCache.set(imageId, undefined);return undefined;}@@ -293,13 +300,13 @@ export async function checkImageMaturity(imageId: string): Promise<import('$lib/const repoTag = imageDetails.RepoTags?.[0];if (!repoTag || repoTag.includes('<none>')) {- updateImageMaturity(imageId, undefined);+ maturityCache.set(imageId, undefined);return undefined;}const lastColon = repoTag.lastIndexOf(':');if (lastColon === -1) {- updateImageMaturity(imageId, undefined);+ maturityCache.set(imageId, undefined);return undefined;}@@ -326,13 +333,13 @@ export async function checkImageMaturity(imageId: string): Promise<import('$lib/} else {console.error(`Error getting registry info for ${repository}:${currentTag}:`, registryInfoResult.error);}- updateImageMaturity(imageId, undefined); // Clear maturity on error+ maturityCache.set(imageId, undefined); // Clear maturity on errorreturn undefined;}const registryInfo = registryInfoResult.data;- updateImageMaturity(imageId, registryInfo);+ maturityCache.set(imageId, registryInfo);return registryInfo;}diff --git a/src/lib/services/docker/maturity-cache-service.ts b/src/lib/services/docker/maturity-cache-service.tsnew file mode 100644index 000000000..e8f2427b9--- /dev/null+++ b/src/lib/services/docker/maturity-cache-service.ts@@ -0,0 +1,94 @@+import type { ImageMaturity } from '$lib/types/docker/image.type';+import { maturityStore, updateImageMaturity } from '$lib/stores/maturity-store';+import { get } from 'svelte/store';++export class MaturityCache {+ private cache: Map<string, { data: ImageMaturity | undefined; timestamp: number }> = new Map();+ private maxEntries: number = 100; // Default limit+ private ttlMs: number = 30 * 60 * 1000; // 30 minutes default++ constructor(options?: { maxEntries?: number; ttlMinutes?: number }) {+ if (options?.maxEntries) this.maxEntries = options.maxEntries;+ if (options?.ttlMinutes) this.ttlMs = options.ttlMinutes * 60 * 1000;++ // Initialize from existing store+ this.initFromStore();+ }++ // Get a value from cache, return undefined if not found or expired+ get(imageId: string): ImageMaturity | undefined {+ const entry = this.cache.get(imageId);+ if (!entry) return undefined;++ // Check if the entry has expired+ if (Date.now() - entry.timestamp > this.ttlMs) {+ this.delete(imageId);+ return undefined;+ }++ // Update LRU status by removing and adding back+ this.cache.delete(imageId);+ this.cache.set(imageId, entry);++ return entry.data;+ }++ // Add or update a value in the cache+ set(imageId: string, data: ImageMaturity | undefined): void {+ // If at capacity, remove oldest entry (first in the Map)+ if (this.cache.size >= this.maxEntries) {+ const firstKey = this.cache.keys().next().value;+ if (typeof firstKey === 'string') {+ this.delete(firstKey);+ }+ }++ // Add new entry+ this.cache.set(imageId, {+ data,+ timestamp: Date.now()+ });++ // Update the store too+ updateImageMaturity(imageId, data);+ }++ // Remove an entry from cache+ delete(imageId: string): void {+ this.cache.delete(imageId);+ updateImageMaturity(imageId, undefined);+ }++ // Initialize cache from maturity store+ private initFromStore(): void {+ const storeData = get(maturityStore);+ const entries = Object.entries(storeData.maturityData);++ // Take only up to maxEntries+ entries.slice(0, this.maxEntries).forEach(([imageId, maturity]) => {+ this.cache.set(imageId, {+ data: maturity,+ timestamp: storeData.lastChecked?.getTime() || Date.now()+ });+ });+ }++ // Clear all items from cache+ clear(): void {+ this.cache.clear();+ }++ // Get cache stats+ getStats(): { size: number; maxSize: number } {+ return {+ size: this.cache.size,+ maxSize: this.maxEntries+ };+ }+}++// Create a singleton instance+export const maturityCache = new MaturityCache({+ maxEntries: 200, // Adjust based on your use case+ ttlMinutes: 60 // Cache entries expire after 1 hour+});diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.tsindex 8c04be2a7..eafe1ee30 100644--- a/src/routes/+page.server.ts+++ b/src/routes/+page.server.ts@@ -3,6 +3,7 @@ import { listContainers } from '$lib/services/docker/container-service';import { getDockerInfo } from '$lib/services/docker/core';import { isImageInUse, listImages, checkImageMaturity } from '$lib/services/docker/image-service';import { getSettings } from '$lib/services/settings-service';+import { maturityCache } from '$lib/services/docker/maturity-cache-service';import type { EnhancedImageInfo, ServiceContainer, ServiceImage } from '$lib/types/docker';type DockerInfoType = Awaited<ReturnType<typeof getDockerInfo>>;@@ -41,13 +42,18 @@ export const load: PageServerLoad = async (): Promise<DashboardData> => {images.map(async (image): Promise<EnhancedImageInfo> => {const inUse = await isImageInUse(image.id);- let maturity = undefined;- try {- if (image.repo !== '<none>' && image.tag !== '<none>') {- maturity = await checkImageMaturity(image.id);+ // Check maturity cache first before returning+ const cachedMaturity = maturityCache.get(image.id);++ let maturity = cachedMaturity;+ if (maturity === undefined) {+ try {+ if (image.repo !== '<none>' && image.tag !== '<none>') {+ maturity = await checkImageMaturity(image.id);+ }+ } catch (maturityError) {+ console.error(`Dashboard: Failed to check maturity for image ${image.id}:`, maturityError);}- } catch (maturityError) {- console.error(`Dashboard: Failed to check maturity for image ${image.id}:`, maturityError);}return {diff --git a/src/routes/+page.svelte b/src/routes/+page.svelteindex 64cd98f06..6f5ea5437 100644--- a/src/routes/+page.svelte+++ b/src/routes/+page.svelte@@ -21,11 +21,16 @@import type { EnhancedImageInfo } from '$lib/types/docker';import { openConfirmDialog } from '$lib/components/confirm-dialog';import MaturityItem from '$lib/components/maturity-item.svelte';+ import { onMount } from 'svelte';+ import { maturityStore } from '$lib/stores/maturity-store';+ import { maturityCache } from '$lib/services/docker/maturity-cache-service';+ import ImageAPIService from '$lib/services/api/image-api-service';let { data }: { data: PageData } = $props();const containerApi = new ContainerAPIService();const systemApi = new SystemAPIService();+ const imageApi = new ImageAPIService();let dashboardStates = $state({dockerInfo: data.dockerInfo,@@ -55,6 +60,11 @@dashboardStates.error = data.error;});+ onMount(async () => {+ // Asynchronously load maturity data for the top images after dashboard loads+ await loadTopImagesMaturity();+ });+async function refreshData() {if (isLoading.refreshing) return;isLoading.refreshing = true;@@ -124,6 +134,46 @@}});}++ async function loadTopImagesMaturity() {+ // Only process if there are images+ if (!dashboardStates.images || dashboardStates.images.length === 0) return;++ // Get the IDs of the largest 5 images (which are displayed in the dashboard)+ const topImageIds = [...dashboardStates.images]+ .sort((a, b) => (b.size || 0) - (a.size || 0))+ .slice(0, 5)+ .filter((img) => img.repo !== '<none>' && img.tag !== '<none>')+ .map((img) => img.id);++ if (topImageIds.length === 0) return;++ // Load maturity data in the background+ try {+ // Process in small batches to keep UI responsive+ const BATCH_SIZE = 2;+ for (let i = 0; i < topImageIds.length; i += BATCH_SIZE) {+ const batch = topImageIds.slice(i, i + BATCH_SIZE);+ await imageApi.checkMaturityBatch(batch);++ // Update the dashboardStates.images with the latest maturity info+ dashboardStates.images = dashboardStates.images.map((image) => {+ const storedMaturity = $maturityStore.maturityData[image.id];+ return {+ ...image,+ maturity: storedMaturity || image.maturity+ };+ });++ // Short delay between batches+ if (i + BATCH_SIZE < topImageIds.length) {+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+ }+ } catch (error) {+ console.error('Error loading maturity data for dashboard images:', error);+ }+ }</script><div class="space-y-8">@@ -405,7 +455,7 @@<Table.Cell><div class="flex items-center gap-2"><div class="flex items-center flex-1">- <MaturityItem maturity={item.maturity} />+ <MaturityItem maturity={item.maturity} isLoadingInBackground={!item.maturity} /><a class="font-medium hover:underline shrink truncate" href="/images/{item.id}/">{item.repo}</a>diff --git a/src/routes/images/+page.svelte b/src/routes/images/+page.svelteindex ce120783d..bcdabec4a 100644--- a/src/routes/images/+page.svelte+++ b/src/routes/images/+page.svelte@@ -3,7 +3,7 @@import type { EnhancedImageInfo } from '$lib/types/docker';import UniversalTable from '$lib/components/universal-table.svelte';import { Button } from '$lib/components/ui/button/index.js';- import { Download, AlertCircle, HardDrive, Trash2, Loader2, ChevronDown, CopyX, Ellipsis, ScanSearch, CircleFadingArrowUp, Funnel, CircleCheck, CircleArrowUp } from '@lucide/svelte';+ import { Download, AlertCircle, HardDrive, Trash2, Loader2, ChevronDown, CopyX, Ellipsis, ScanSearch, Funnel } from '@lucide/svelte';import * as Alert from '$lib/components/ui/alert/index.js';import * as Card from '$lib/components/ui/card/index.js';import { goto, invalidateAll } from '$app/navigation';@@ -19,8 +19,9 @@import { handleApiResultWithCallbacks } from '$lib/utils/api.util';import { tryCatch } from '$lib/utils/try-catch';import { settingsStore } from '$lib/stores/settings-store';- import * as Tooltip from '$lib/components/ui/tooltip/index.js';import MaturityItem from '$lib/components/maturity-item.svelte';+ import { onMount, onDestroy } from 'svelte';+ import { maturityStore } from '$lib/stores/maturity-store';let { data }: { data: PageData } = $props();let images = $state<EnhancedImageInfo[]>(data.images || []);@@ -32,9 +33,6 @@showUnused: true});- // Add with other derived values- const filteredImages = $derived(images.filter((img) => (imageFilters.showUsed && img.inUse) || (imageFilters.showUnused && !img.inUse)));-let isLoading = $state({pulling: false,removing: false,@@ -62,6 +60,51 @@const totalImages = $derived(images?.length || 0);const totalSize = $derived(images?.reduce((acc, img) => acc + (img.size || 0), 0) || 0);+ // let enhancedImages: EnhancedImageInfo[] = [];+ const enhancedImages = $derived(+ images.map((image) => {+ const storedMaturity = $maturityStore.maturityData[image.id];+ return {+ ...image,+ maturity: storedMaturity || image.maturity+ };+ })+ );++ const filteredImages = $derived(enhancedImages.filter((img) => (imageFilters.showUsed && img.inUse) || (imageFilters.showUnused && !img.inUse)) as EnhancedImageInfo[]);++ onMount(async () => {+ await loadMaturityData();+ });++ async function loadMaturityData() {+ // Only check visible images based on pagination+ const visibleImageIds = images+ .filter((img) => img.repo !== '<none>' && img.tag !== '<none>')+ .slice(0, 20) // Limit to visible images+ .map((img) => img.id);++ if (visibleImageIds.length === 0) return;++ isLoading.checking = true;+ try {+ // Process in smaller batches to avoid overwhelming the server+ const BATCH_SIZE = 5;+ for (let i = 0; i < visibleImageIds.length; i += BATCH_SIZE) {+ const batch = visibleImageIds.slice(i, i + BATCH_SIZE);+ await imageApi.checkMaturityBatch(batch);+ // Give UI time to update between batches+ if (i + BATCH_SIZE < visibleImageIds.length) {+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+ }+ } catch (error) {+ console.error('Error loading maturity data:', error);+ } finally {+ isLoading.checking = false;+ }+ }+async function handlePullImageSubmit(event: { imageRef: string; tag?: string; platform?: string; registryUrl?: string }) {const { imageRef, tag = 'latest', platform, registryUrl } = event;@@ -312,6 +355,43 @@$effect(() => {images = data.images;});++ let observer: IntersectionObserver | null = null;++ onMount(() => {+ // Lazily load maturity data for visible rows+ observer = new IntersectionObserver(+ (entries) => {+ entries.forEach((entry) => {+ if (entry.isIntersecting) {+ const imageId = entry.target.getAttribute('data-image-id');+ if (imageId) {+ loadImageMaturity(imageId);+ }+ }+ });+ },+ { rootMargin: '200px' }+ );++ setTimeout(() => {+ document.querySelectorAll('[data-image-id]').forEach((el) => {+ observer?.observe(el);+ });+ }, 100);+ });++ onDestroy(() => {+ observer?.disconnect();+ });++ async function loadImageMaturity(imageId: string) {+ try {+ await imageApi.checkMaturity(imageId);+ } catch (error) {+ console.error(`Error loading maturity for image ${imageId}:`, error);+ }+ }</script><div class="space-y-6">@@ -453,7 +533,7 @@bind:selectedIds>{#snippet rows({ item })}- <Table.Cell>+ <Table.Cell data-image-id={item.id}><div class="flex items-center gap-2"><div class="flex items-center flex-1"><MaturityItem maturity={item.maturity} />