import { useQuery } from "@tanstack/react-query"; import { Archive } from "lucide-react"; import { getRepositoryStatsOptions } from "~/client/api-client/@tanstack/react-query.gen"; import { ByteSize } from "~/client/components/bytes-size"; import { Card, CardContent, CardHeader, CardTitle } from "~/client/components/ui/card"; type Props = { repositoryShortId: string; }; const toSafeNumber = (value: number | undefined) => { if (typeof value !== "number" || !Number.isFinite(value)) { return 0; } return Math.max(0, value); }; export function CompressionStatsChart({ repositoryShortId }: Props) { const { data: stats, isLoading, error, } = useQuery({ ...getRepositoryStatsOptions({ path: { shortId: repositoryShortId } }), retry: false, }); const storedSize = toSafeNumber(stats?.total_size); const uncompressedSize = toSafeNumber(stats?.total_uncompressed_size); const savedSize = uncompressedSize > storedSize ? uncompressedSize - storedSize : 0; const compressionRatio = toSafeNumber(stats?.compression_ratio); const rawCompressionProgress = toSafeNumber(stats?.compression_progress); const compressionProgressPercent = Math.min(100, Math.max(0, rawCompressionProgress)); const spaceSavingPercent = toSafeNumber(stats?.compression_space_saving); const snapshotsCount = Math.round(toSafeNumber(stats?.snapshots_count)); const hasStats = !!stats && (storedSize > 0 || uncompressedSize > 0 || snapshotsCount > 0); if (isLoading) { return (

Loading compression statistics...

); } if (error) { return (

Failed to load compression statistics

{error.message}

); } if (!hasStats) { return (

No compression statistics available yet. Run a backup to populate repository stats.

); } return ( Compression Statistics
Stored Size
Uncompressed
Space Saved
{spaceSavingPercent.toFixed(1)}%
Ratio
{compressionRatio > 0 ? `${compressionRatio.toFixed(2)}x` : "—"}
Snapshots
{snapshotsCount.toLocaleString()} {compressionProgressPercent.toFixed(1)}% compressed
); }