fix: log viewer

This commit is contained in:
bennykok 2024-01-31 21:21:22 +08:00
parent d4d7e98487
commit de641f0acf
3 changed files with 73 additions and 53 deletions

View File

@ -9,7 +9,10 @@ export type LogsType = {
timestamp: number; timestamp: number;
}[]; }[];
export function LogsViewer({ logs }: { logs: LogsType }) { export function LogsViewer({
logs,
hideTimestamp,
}: { logs: LogsType; hideTimestamp?: boolean }) {
const container = useRef<HTMLDivElement | null>(null); const container = useRef<HTMLDivElement | null>(null);
useEffect(() => { useEffect(() => {
@ -37,7 +40,7 @@ export function LogsViewer({ logs }: { logs: LogsType }) {
} }
container.current = ref; container.current = ref;
}} }}
className="flex flex-col text-xs p-2 overflow-y-scroll whitespace-break-spaces" className="h-full w-full flex flex-col text-xs p-2 overflow-y-scroll whitespace-break-spaces"
> >
{logs.map((x, i) => ( {logs.map((x, i) => (
<div <div
@ -48,10 +51,14 @@ export function LogsViewer({ logs }: { logs: LogsType }) {
navigator.clipboard.writeText(x.logs); navigator.clipboard.writeText(x.logs);
}} }}
> >
<span className="min-w-fit"> {!hideTimestamp && (
{new Date(x.timestamp).toLocaleString()} <>
<span className="w-[150px] flex-shrink-0">
{new Date(x.timestamp * 1000).toLocaleString()}
</span> </span>
<div className="h-full w-[1px] bg-stone-400 flex-shrink-0"></div> <div className="h-full w-[1px] bg-stone-400 flex-shrink-0"></div>
</>
)}
{/* Display timestamp */} {/* Display timestamp */}
<div>{x.logs}</div> <div>{x.logs}</div>
</div> </div>

View File

@ -16,7 +16,7 @@ import {
AlertDialogHeader, AlertDialogHeader,
AlertDialogTitle, AlertDialogTitle,
AlertDialogTrigger, AlertDialogTrigger,
} from "@/components/ui/alert-dialog" } from "@/components/ui/alert-dialog";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
export function MachineBuildLog({ export function MachineBuildLog({
@ -41,7 +41,7 @@ export function MachineBuildLog({
reconnectAttempts: 20, reconnectAttempts: 20,
reconnectInterval: 1000, reconnectInterval: 1000,
queryParams: query, queryParams: query,
} },
); );
const connectionStatus = getConnectionStatus(readyState); const connectionStatus = getConnectionStatus(readyState);
@ -57,59 +57,70 @@ export function MachineBuildLog({
setLogs((logs) => [...(logs ?? []), message.data]); setLogs((logs) => [...(logs ?? []), message.data]);
} else if (message?.event === "FINISHED") { } else if (message?.event === "FINISHED") {
setFinished(true); setFinished(true);
setStatus(message.data.status) setStatus(message.data.status);
} }
}, [lastMessage]); }, [lastMessage]);
const router = useRouter() const router = useRouter();
return ( return (
<div> <div>
{connectionStatus} {connectionStatus}
<LogsViewer logs={logs} /> <LogsViewer logs={logs} hideTimestamp />
<AlertDialog open={finished}> <AlertDialog open={finished}>
<AlertDialogContent> <AlertDialogContent>
{ {status == "succuss" ? (
status == "succuss" ? (
<> <>
<AlertDialogHeader> <AlertDialogHeader>
<AlertDialogTitle>Machine Built</AlertDialogTitle> <AlertDialogTitle>Machine Built</AlertDialogTitle>
<AlertDialogDescription> <AlertDialogDescription>
Your machine is built, you can now integrate your API, or directly run to check this machines. Your machine is built, you can now integrate your API, or
directly run to check this machines.
</AlertDialogDescription> </AlertDialogDescription>
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<AlertDialogAction onClick={() => { <AlertDialogAction
router.push("/workflows") onClick={() => {
}}>See Workflows</AlertDialogAction> router.push("/workflows");
<AlertDialogAction onClick={() => { }}
router.push("/machines") >
}}>See All Machines</AlertDialogAction> See Workflows
</AlertDialogFooter></> </AlertDialogAction>
<AlertDialogAction
onClick={() => {
router.push("/machines");
}}
>
See All Machines
</AlertDialogAction>
</AlertDialogFooter>
</>
) : ( ) : (
<> <>
<AlertDialogHeader> <AlertDialogHeader>
<AlertDialogTitle>Machine Failed</AlertDialogTitle> <AlertDialogTitle>Machine Failed</AlertDialogTitle>
<AlertDialogDescription> <AlertDialogDescription>
Something went wrong with the machine build, please check the log. Something went wrong with the machine build, please check the
Possible cause could be conflits with custom nodes, build got stuck, timeout, or too many custom nodes installed. log. Possible cause could be conflits with custom nodes, build
Please attempt a rebuild or remove some of the custom nodes. got stuck, timeout, or too many custom nodes installed. Please
attempt a rebuild or remove some of the custom nodes.
</AlertDialogDescription> </AlertDialogDescription>
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<AlertDialogCancel>See logs</AlertDialogCancel> <AlertDialogCancel>See logs</AlertDialogCancel>
<AlertDialogAction onClick={() => { <AlertDialogAction
router.push("/machines") onClick={() => {
}}>Back to machines</AlertDialogAction> router.push("/machines");
</AlertDialogFooter></> }}
) >
} Back to machines
</AlertDialogAction>
</AlertDialogFooter>
</>
)}
</AlertDialogContent> </AlertDialogContent>
</AlertDialog> </AlertDialog>
</div> </div>
); );
} }

View File

@ -36,7 +36,7 @@ type State = {
json: { json: {
event: string; event: string;
data: any; data: any;
} },
) => void; ) => void;
}; };
@ -82,7 +82,7 @@ function MachineWS({
const logs = useStore((x) => const logs = useStore((x) =>
x.logs x.logs
.filter((p) => p.machine_id === machine.id) .filter((p) => p.machine_id === machine.id)
.sort((a, b) => a.timestamp - b.timestamp) .sort((a, b) => a.timestamp - b.timestamp),
); );
const [sid, setSid] = useState(""); const [sid, setSid] = useState("");
@ -96,7 +96,7 @@ function MachineWS({
// queryParams: { // queryParams: {
// clientId: sid, // clientId: sid,
// }, // },
} },
); );
const connectionStatus = getConnectionStatus(readyState); const connectionStatus = getConnectionStatus(readyState);
@ -135,7 +135,9 @@ function MachineWS({
You can view your run&apos;s outputs here You can view your run&apos;s outputs here
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<LogsViewer logs={logs} /> <div className="h-[400px]">
<LogsViewer logs={logs} hideTimestamp />
</div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
); );