From 302172c53764048f34fd168591a5ac44af919179 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 12 Feb 2025 14:30:05 -0500 Subject: [PATCH] Add refresh button to settings page status tables, fix duration on active scripts --- .../components/settings/DataManagement.tsx | 97 ++++++++++++++++++- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/inventory/src/components/settings/DataManagement.tsx b/inventory/src/components/settings/DataManagement.tsx index 42f7c07..52d6e5a 100644 --- a/inventory/src/components/settings/DataManagement.tsx +++ b/inventory/src/components/settings/DataManagement.tsx @@ -24,7 +24,7 @@ import { AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; -import { Loader2, X, RefreshCw, AlertTriangle } from "lucide-react"; +import { Loader2, X, RefreshCw, AlertTriangle, RefreshCcw, Hourglass } from "lucide-react"; import config from "../../config"; import { toast } from "sonner"; import { Table, TableBody, TableCell, TableRow, TableHeader, TableHead } from "@/components/ui/table"; @@ -101,7 +101,26 @@ export function DataManagement() { }; // Helper to format duration with seconds - const formatDurationWithSeconds = (minutes: number) => { + const formatDurationWithSeconds = (minutes: number, isRunning: boolean = false, startTime?: string) => { + if (isRunning && startTime) { + const elapsedMinutes = (new Date().getTime() - new Date(startTime).getTime()) / (1000 * 60); + const hours = Math.floor(elapsedMinutes / 60); + const remainingMinutes = Math.floor(elapsedMinutes % 60); + const seconds = Math.round((elapsedMinutes % 1) * 60); + + const parts = []; + if (hours > 0) parts.push(`${hours}h`); + if (remainingMinutes > 0) parts.push(`${remainingMinutes}m`); + if (seconds > 0) parts.push(`${seconds}s`); + + return ( + + {parts.join(" ")} + + + ); + } + if (minutes < 1 / 60) return "Less than a second"; const hours = Math.floor(minutes / 60); @@ -365,6 +384,60 @@ export function DataManagement() { } }; + const refreshTableStatus = async () => { + try { + const response = await fetch(`${config.apiUrl}/csv/status/tables`); + const data = await response.json(); + setTableStatus(data); + } catch (error) { + toast.error("Failed to refresh table status"); + } + }; + + const refreshModuleStatus = async () => { + try { + const response = await fetch(`${config.apiUrl}/csv/status/modules`); + const data = await response.json(); + setModuleStatus(data); + } catch (error) { + toast.error("Failed to refresh module status"); + } + }; + + const refreshImportHistory = async () => { + try { + const response = await fetch(`${config.apiUrl}/csv/history/import`); + const data = await response.json(); + setImportHistory(data); + } catch (error) { + toast.error("Failed to refresh import history"); + } + }; + + const refreshCalculateHistory = async () => { + try { + const response = await fetch(`${config.apiUrl}/csv/history/calculate`); + const data = await response.json(); + setCalculateHistory(data); + } catch (error) { + toast.error("Failed to refresh calculate history"); + } + }; + + const refreshAllData = async () => { + try { + await Promise.all([ + refreshTableStatus(), + refreshModuleStatus(), + refreshImportHistory(), + refreshCalculateHistory() + ]); + toast.success("All data refreshed"); + } catch (error) { + toast.error("Failed to refresh some data"); + } + }; + useEffect(() => { fetchHistory(); }, []); @@ -511,7 +584,17 @@ export function DataManagement() { {/* History Section */}
-

History & Status

+
+

History & Status

+ +
{/* Table Status */} @@ -595,7 +678,9 @@ export function DataManagement() { {formatDurationWithSeconds( - record.duration_minutes + record.duration_minutes, + record.status === "running", + record.start_time )} {formatDurationWithSeconds( - record.duration_minutes + record.duration_minutes, + record.status === "running", + record.start_time )}