|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import { useAtomValue } from "jotai"; |
| 4 | +import { DatabaseZapIcon, RefreshCwIcon, Trash2Icon } from "lucide-react"; |
| 5 | +import React, { useState } from "react"; |
| 6 | +import { useLocale } from "react-aria"; |
| 7 | +import { Spinner } from "@/components/icons/spinner"; |
| 8 | +import { Button } from "@/components/ui/button"; |
| 9 | +import { ConfirmationButton } from "@/components/ui/confirmation-button"; |
| 10 | +import { toast } from "@/components/ui/use-toast"; |
| 11 | +import { cacheInfoAtom } from "@/core/cache/requests"; |
| 12 | +import { useRequestClient } from "@/core/network/requests"; |
| 13 | +import { useAsyncData } from "@/hooks/useAsyncData"; |
| 14 | +import { cn } from "@/utils/cn"; |
| 15 | +import { formatBytes, formatTime } from "@/utils/formatting"; |
| 16 | +import { prettyNumber } from "@/utils/numbers"; |
| 17 | +import { PanelEmptyState } from "./empty-state"; |
| 18 | + |
| 19 | +const CachePanel = () => { |
| 20 | + const { clearCache, getCacheInfo } = useRequestClient(); |
| 21 | + const cacheInfo = useAtomValue(cacheInfoAtom); |
| 22 | + const [purging, setPurging] = useState(false); |
| 23 | + const { locale } = useLocale(); |
| 24 | + |
| 25 | + const { isPending, isFetching, refetch } = useAsyncData(async () => { |
| 26 | + await getCacheInfo(); |
| 27 | + // Artificially spin the icon if the request is really fast |
| 28 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 29 | + }, []); |
| 30 | + |
| 31 | + const handlePurge = async () => { |
| 32 | + try { |
| 33 | + setPurging(true); |
| 34 | + await clearCache(); |
| 35 | + toast({ |
| 36 | + title: "Cache purged", |
| 37 | + description: "All cached data has been cleared", |
| 38 | + }); |
| 39 | + // Request updated cache info after purge |
| 40 | + refetch(); |
| 41 | + } catch (err) { |
| 42 | + toast({ |
| 43 | + title: "Error", |
| 44 | + description: |
| 45 | + err instanceof Error ? err.message : "Failed to purge cache", |
| 46 | + variant: "danger", |
| 47 | + }); |
| 48 | + } finally { |
| 49 | + setPurging(false); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Show spinner only on initial load |
| 54 | + if (isPending && !cacheInfo) { |
| 55 | + return <Spinner size="medium" centered={true} />; |
| 56 | + } |
| 57 | + |
| 58 | + const refreshButton = ( |
| 59 | + <Button variant="outline" size="sm" onClick={refetch} disabled={isFetching}> |
| 60 | + {isFetching ? ( |
| 61 | + <Spinner size="small" className="w-4 h-4 mr-2" /> |
| 62 | + ) : ( |
| 63 | + <RefreshCwIcon className="w-4 h-4 mr-2" /> |
| 64 | + )} |
| 65 | + Refresh |
| 66 | + </Button> |
| 67 | + ); |
| 68 | + |
| 69 | + if (!cacheInfo) { |
| 70 | + return ( |
| 71 | + <PanelEmptyState |
| 72 | + title="No cache data" |
| 73 | + description="Cache information is not available." |
| 74 | + icon={<DatabaseZapIcon />} |
| 75 | + action={refreshButton} |
| 76 | + /> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const totalHits = cacheInfo.hits; |
| 81 | + const totalMisses = cacheInfo.misses; |
| 82 | + const totalTime = cacheInfo.time; |
| 83 | + const diskTotal = cacheInfo.disk_total; |
| 84 | + const diskToFree = cacheInfo.disk_to_free; |
| 85 | + |
| 86 | + const totalRequests = totalHits + totalMisses; |
| 87 | + const hitRate = totalRequests > 0 ? (totalHits / totalRequests) * 100 : 0; |
| 88 | + |
| 89 | + // Show empty state if no cache activity |
| 90 | + if (totalRequests === 0) { |
| 91 | + return ( |
| 92 | + <PanelEmptyState |
| 93 | + title="No cache activity" |
| 94 | + description="The cache has not been used yet. Cached functions will appear here once they are executed." |
| 95 | + icon={<DatabaseZapIcon />} |
| 96 | + action={refreshButton} |
| 97 | + /> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className="flex flex-col h-full overflow-auto"> |
| 103 | + <div className="flex flex-col gap-4 p-4 h-full"> |
| 104 | + {/* Header with Refresh Button */} |
| 105 | + <div className="flex items-center justify-end"> |
| 106 | + <Button |
| 107 | + variant="ghost" |
| 108 | + size="icon" |
| 109 | + className="h-6 w-6" |
| 110 | + onClick={refetch} |
| 111 | + disabled={isFetching} |
| 112 | + > |
| 113 | + <RefreshCwIcon |
| 114 | + className={cn( |
| 115 | + "h-4 w-4 text-muted-foreground hover:text-foreground", |
| 116 | + isFetching && "animate-[spin_0.5s]", |
| 117 | + )} |
| 118 | + /> |
| 119 | + </Button> |
| 120 | + </div> |
| 121 | + |
| 122 | + {/* Statistics Section */} |
| 123 | + <div className="space-y-3"> |
| 124 | + <h3 className="text-sm font-semibold text-foreground">Statistics</h3> |
| 125 | + <div className="grid grid-cols-2 gap-3"> |
| 126 | + <StatCard |
| 127 | + label="Time saved" |
| 128 | + value={formatTime(totalTime, locale)} |
| 129 | + description="Total execution time saved" |
| 130 | + /> |
| 131 | + <StatCard |
| 132 | + label="Hit rate" |
| 133 | + value={ |
| 134 | + totalRequests > 0 ? `${prettyNumber(hitRate, locale)}%` : "—" |
| 135 | + } |
| 136 | + description={`${prettyNumber(totalHits, locale)} hits / ${prettyNumber(totalRequests, locale)} total`} |
| 137 | + /> |
| 138 | + <StatCard |
| 139 | + label="Cache hits" |
| 140 | + value={prettyNumber(totalHits, locale)} |
| 141 | + description="Successful cache retrievals" |
| 142 | + /> |
| 143 | + <StatCard |
| 144 | + label="Cache misses" |
| 145 | + value={prettyNumber(totalMisses, locale)} |
| 146 | + description="Cache not found" |
| 147 | + /> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + |
| 151 | + {/* Storage Section */} |
| 152 | + {diskTotal > 0 && ( |
| 153 | + <div className="space-y-3 pt-2 border-t"> |
| 154 | + <h3 className="text-sm font-semibold text-foreground">Storage</h3> |
| 155 | + <div className="grid grid-cols-1 gap-3"> |
| 156 | + <StatCard |
| 157 | + label="Disk usage" |
| 158 | + value={formatBytes(diskTotal, locale)} |
| 159 | + description={ |
| 160 | + diskToFree > 0 |
| 161 | + ? `${formatBytes(diskToFree, locale)} can be freed` |
| 162 | + : "Cache storage on disk" |
| 163 | + } |
| 164 | + /> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + )} |
| 168 | + |
| 169 | + <div className="my-auto" /> |
| 170 | + |
| 171 | + {/* Actions Section */} |
| 172 | + <div className="pt-2 border-t"> |
| 173 | + <ConfirmationButton |
| 174 | + title="Purge cache?" |
| 175 | + description="This will permanently delete all cached data. This action cannot be undone." |
| 176 | + confirmText="Purge" |
| 177 | + destructive={true} |
| 178 | + onConfirm={handlePurge} |
| 179 | + > |
| 180 | + <Button |
| 181 | + variant="outlineDestructive" |
| 182 | + size="xs" |
| 183 | + disabled={purging} |
| 184 | + className="w-full" |
| 185 | + > |
| 186 | + {purging ? ( |
| 187 | + <Spinner size="small" className="w-3 h-3 mr-2" /> |
| 188 | + ) : ( |
| 189 | + <Trash2Icon className="w-3 h-3 mr-2" /> |
| 190 | + )} |
| 191 | + Purge Cache |
| 192 | + </Button> |
| 193 | + </ConfirmationButton> |
| 194 | + </div> |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + ); |
| 198 | +}; |
| 199 | + |
| 200 | +const StatCard: React.FC<{ |
| 201 | + label: string; |
| 202 | + value: string; |
| 203 | + description?: string; |
| 204 | +}> = ({ label, value, description }) => { |
| 205 | + return ( |
| 206 | + <div className="flex flex-col gap-1 p-3 rounded-lg border bg-card"> |
| 207 | + <span className="text-xs text-muted-foreground">{label}</span> |
| 208 | + <span className="text-lg font-semibold">{value}</span> |
| 209 | + {description && ( |
| 210 | + <span className="text-xs text-muted-foreground">{description}</span> |
| 211 | + )} |
| 212 | + </div> |
| 213 | + ); |
| 214 | +}; |
| 215 | + |
| 216 | +export default CachePanel; |
0 commit comments