page.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. "use client";
  2. import {
  3. ArrowPathIcon,
  4. ChartBarIcon,
  5. FolderIcon,
  6. TrashIcon,
  7. } from "@heroicons/react/24/outline";
  8. import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
  9. import { useEffect, useState } from "react";
  10. import toast from "react-hot-toast";
  11. import { del, get, post } from "../../lib/api";
  12. import LoadingCard from "../components/Loading";
  13. import { useAppContext } from "../providers/AppContext";
  14. interface IndexStats {
  15. totalDuplicates: number;
  16. duplicatesByDataset: Array<{
  17. dataset: string;
  18. hash: string;
  19. file_size: number;
  20. file_count: number;
  21. files: string[];
  22. }>;
  23. }
  24. interface IndexCount {
  25. count: number;
  26. }
  27. export default function IndexManagementPage() {
  28. const queryClient = useQueryClient();
  29. const { datasets, datasetsConfig } = useAppContext();
  30. const [selectedDataset, setSelectedDataset] = useState<string>("");
  31. const [destinationPath, setDestinationPath] = useState<string>("");
  32. const [batchSize, setBatchSize] = useState<number>(100);
  33. const datasetNames = datasets
  34. ? datasets.map((p: string) => p.split("/").pop()).filter(Boolean)
  35. : [];
  36. // Auto-populate destination path from dataset configuration when a dataset is selected
  37. // We mimic the backend collector: prefer top-level destination, otherwise any nested config with a destination key
  38. useEffect(() => {
  39. if (!selectedDataset || !datasetsConfig) return;
  40. const cfg = datasetsConfig[selectedDataset];
  41. if (!cfg) return;
  42. const tryFindDestination = (obj: any): string | undefined => {
  43. if (!obj || typeof obj !== "object") return undefined;
  44. if (typeof obj.destination === "string" && obj.destination.trim()) {
  45. return obj.destination as string;
  46. }
  47. for (const value of Object.values(obj)) {
  48. if (
  49. value &&
  50. typeof value === "object" &&
  51. typeof value.destination === "string"
  52. ) {
  53. if (value.destination.trim()) return value.destination as string;
  54. }
  55. }
  56. return undefined;
  57. };
  58. const destination = tryFindDestination(cfg);
  59. if (destination && destination !== destinationPath) {
  60. setDestinationPath(destination);
  61. }
  62. }, [selectedDataset, datasetsConfig, destinationPath]);
  63. // Get index count for selected dataset
  64. const {
  65. data: indexCount,
  66. isLoading: isLoadingCount,
  67. refetch: refetchCount,
  68. } = useQuery<IndexCount>({
  69. queryKey: ["index-count", selectedDataset],
  70. queryFn: async () =>
  71. selectedDataset
  72. ? get("/maintenance/index/count", { dataset: selectedDataset })
  73. : { count: 0 },
  74. enabled: !!selectedDataset,
  75. });
  76. // Get duplicate stats
  77. const {
  78. data: stats,
  79. isLoading: isLoadingStats,
  80. refetch: refetchStats,
  81. } = useQuery<IndexStats>({
  82. queryKey: ["index-stats", selectedDataset],
  83. queryFn: async () => {
  84. const params = selectedDataset ? { dataset: selectedDataset } : undefined;
  85. return get("/maintenance/index/stats", params);
  86. },
  87. });
  88. // Index destination mutation
  89. const indexMutation = useMutation({
  90. mutationFn: async ({
  91. dataset,
  92. destination,
  93. reindex,
  94. }: {
  95. dataset: string;
  96. destination: string;
  97. reindex: boolean;
  98. }) =>
  99. post("/maintenance/index/destination", {
  100. dataset,
  101. destination,
  102. reindex,
  103. batchSize,
  104. }),
  105. onSuccess: (data) => {
  106. toast.success(
  107. `✅ Indexed: ${data.indexed}, Skipped: ${data.skipped}, Errors: ${data.errors}`
  108. );
  109. refetchCount();
  110. refetchStats();
  111. },
  112. onError: (err: any) => {
  113. console.error(err);
  114. toast.error("Failed to index destination");
  115. },
  116. });
  117. // Clear index mutation
  118. const clearMutation = useMutation({
  119. mutationFn: async (dataset: string) => del(`/maintenance/index/${dataset}`),
  120. onSuccess: (data) => {
  121. toast.success(`🗑️ Cleared ${data.cleared} index entries`);
  122. refetchCount();
  123. refetchStats();
  124. },
  125. onError: (err: any) => {
  126. console.error(err);
  127. toast.error("Failed to clear index");
  128. },
  129. });
  130. const handleIndex = (reindex: boolean) => {
  131. if (!selectedDataset) {
  132. toast.error("Please select a dataset");
  133. return;
  134. }
  135. if (!destinationPath) {
  136. toast.error("Please enter a destination path");
  137. return;
  138. }
  139. indexMutation.mutate({
  140. dataset: selectedDataset,
  141. destination: destinationPath,
  142. reindex,
  143. });
  144. };
  145. const handleClear = () => {
  146. if (!selectedDataset) {
  147. toast.error("Please select a dataset");
  148. return;
  149. }
  150. if (confirm(`Clear all index entries for ${selectedDataset}?`)) {
  151. clearMutation.mutate(selectedDataset);
  152. }
  153. };
  154. const formatBytes = (bytes: number) => {
  155. if (!bytes) return "0 B";
  156. const sizes = ["B", "KB", "MB", "GB", "TB"];
  157. const i = Math.floor(Math.log(bytes) / Math.log(1024));
  158. return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
  159. };
  160. return (
  161. <div className="space-y-6">
  162. <div>
  163. <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">
  164. Index Management
  165. </h1>
  166. <p className="mt-1 text-sm text-gray-600 dark:text-gray-400">
  167. Index destination files for fast duplicate detection
  168. </p>
  169. </div>
  170. {/* Index Controls */}
  171. <div className="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
  172. <h2 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-4">
  173. Index Destination
  174. </h2>
  175. <div className="space-y-4">
  176. <div>
  177. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
  178. Dataset
  179. </label>
  180. <select
  181. value={selectedDataset}
  182. onChange={(e) => setSelectedDataset(e.target.value)}
  183. className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100"
  184. >
  185. <option value="">Select a dataset...</option>
  186. {datasetNames.map((name) => (
  187. <option key={name} value={name}>
  188. {name}
  189. </option>
  190. ))}
  191. </select>
  192. </div>
  193. <div>
  194. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
  195. Destination Path
  196. </label>
  197. <input
  198. type="text"
  199. value={destinationPath}
  200. onChange={(e) => setDestinationPath(e.target.value)}
  201. placeholder="/path/to/destination"
  202. className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100"
  203. />
  204. </div>
  205. <div>
  206. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
  207. Batch Size
  208. </label>
  209. <input
  210. type="number"
  211. value={batchSize}
  212. onChange={(e) => setBatchSize(parseInt(e.target.value))}
  213. min="10"
  214. max="1000"
  215. className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-gray-100"
  216. />
  217. <p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
  218. Number of files to process at once
  219. </p>
  220. </div>
  221. <div className="flex gap-2">
  222. <button
  223. onClick={() => handleIndex(false)}
  224. disabled={indexMutation.isPending}
  225. className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
  226. >
  227. <FolderIcon className="h-5 w-5 mr-2" />
  228. Index
  229. </button>
  230. <button
  231. onClick={() => handleIndex(true)}
  232. disabled={indexMutation.isPending}
  233. className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-orange-600 hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500 disabled:opacity-50"
  234. >
  235. <ArrowPathIcon className="h-5 w-5 mr-2" />
  236. Re-index
  237. </button>
  238. <button
  239. onClick={handleClear}
  240. disabled={clearMutation.isPending || !selectedDataset}
  241. className="inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 text-sm font-medium rounded-md shadow-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
  242. >
  243. <TrashIcon className="h-5 w-5 mr-2" />
  244. Clear Index
  245. </button>
  246. </div>
  247. </div>
  248. </div>
  249. {/* Index Stats */}
  250. {selectedDataset && (
  251. <div className="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
  252. <h2 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-4 flex items-center">
  253. <ChartBarIcon className="h-5 w-5 mr-2" />
  254. Index Statistics
  255. </h2>
  256. {isLoadingCount ? (
  257. <LoadingCard message="Loading stats..." />
  258. ) : (
  259. <div className="space-y-4">
  260. <div className="flex justify-between items-center p-4 bg-gray-50 dark:bg-gray-700 rounded-lg">
  261. <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
  262. Indexed Files
  263. </span>
  264. <span className="text-2xl font-bold text-blue-600 dark:text-blue-400">
  265. {indexCount?.count || 0}
  266. </span>
  267. </div>
  268. </div>
  269. )}
  270. </div>
  271. )}
  272. {/* Duplicate Stats */}
  273. <div className="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
  274. <h2 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-4">
  275. Duplicate Statistics
  276. </h2>
  277. {isLoadingStats ? (
  278. <LoadingCard message="Loading duplicate stats..." />
  279. ) : stats && stats.totalDuplicates > 0 ? (
  280. <div className="space-y-4">
  281. <div className="flex justify-between items-center p-4 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg border border-yellow-200 dark:border-yellow-800">
  282. <span className="text-sm font-medium text-yellow-800 dark:text-yellow-300">
  283. Total Duplicate Groups
  284. </span>
  285. <span className="text-2xl font-bold text-yellow-600 dark:text-yellow-400">
  286. {stats.totalDuplicates}
  287. </span>
  288. </div>
  289. <div className="space-y-3 max-h-96 overflow-y-auto">
  290. {stats.duplicatesByDataset.slice(0, 10).map((dup, idx) => (
  291. <div
  292. key={idx}
  293. className="p-4 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600"
  294. >
  295. <div className="flex justify-between items-start mb-2">
  296. <span className="text-sm font-medium text-gray-900 dark:text-gray-100">
  297. [{dup.dataset}] {dup.file_count} files
  298. </span>
  299. <span className="text-sm text-gray-500 dark:text-gray-400">
  300. {formatBytes(dup.file_size)}
  301. </span>
  302. </div>
  303. <div className="text-xs text-gray-500 dark:text-gray-400 font-mono mb-2">
  304. Hash: {dup.hash.substring(0, 32)}...
  305. </div>
  306. <div className="space-y-1">
  307. {dup.files.map((file, fileIdx) => (
  308. <div
  309. key={fileIdx}
  310. className="text-xs text-gray-600 dark:text-gray-400 truncate"
  311. title={file}
  312. >
  313. • {file}
  314. </div>
  315. ))}
  316. </div>
  317. </div>
  318. ))}
  319. </div>
  320. {stats.duplicatesByDataset.length > 10 && (
  321. <p className="text-sm text-gray-500 dark:text-gray-400 text-center">
  322. ... and {stats.duplicatesByDataset.length - 10} more duplicate
  323. groups
  324. </p>
  325. )}
  326. </div>
  327. ) : (
  328. <div className="text-center py-8 text-gray-500 dark:text-gray-400">
  329. <p>No duplicates found in indexed files</p>
  330. </div>
  331. )}
  332. </div>
  333. </div>
  334. );
  335. }