2024-07-11 03:08:52 -07:00
|
|
|
import { getClient } from "@/api/AxiosClient";
|
2024-09-24 11:54:20 -07:00
|
|
|
import {
|
|
|
|
|
Status,
|
|
|
|
|
TaskApiResponse,
|
|
|
|
|
WorkflowRunStatusApiResponse,
|
|
|
|
|
} from "@/api/types";
|
2024-07-11 03:08:52 -07:00
|
|
|
import { StatusBadge } from "@/components/StatusBadge";
|
2024-09-05 18:40:58 +03:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import {
|
|
|
|
|
Pagination,
|
|
|
|
|
PaginationContent,
|
|
|
|
|
PaginationItem,
|
|
|
|
|
PaginationLink,
|
|
|
|
|
PaginationNext,
|
|
|
|
|
PaginationPrevious,
|
|
|
|
|
} from "@/components/ui/pagination";
|
2024-07-11 03:08:52 -07:00
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableRow,
|
|
|
|
|
} from "@/components/ui/table";
|
|
|
|
|
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
|
|
|
|
|
import { basicTimeFormat } from "@/util/timeFormat";
|
2024-07-25 07:35:47 -07:00
|
|
|
import { cn } from "@/util/utils";
|
2024-09-24 11:54:20 -07:00
|
|
|
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
2024-09-05 18:40:58 +03:00
|
|
|
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
|
|
|
|
|
import { TaskActions } from "../tasks/list/TaskActions";
|
|
|
|
|
import { TaskListSkeletonRows } from "../tasks/list/TaskListSkeletonRows";
|
2024-09-30 11:36:24 -07:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import { statusIsNotFinalized, statusIsRunningOrQueued } from "../tasks/types";
|
|
|
|
|
import { envCredential } from "@/util/env";
|
|
|
|
|
import { toast } from "@/components/ui/use-toast";
|
|
|
|
|
|
|
|
|
|
type StreamMessage = {
|
|
|
|
|
task_id: string;
|
|
|
|
|
status: string;
|
|
|
|
|
screenshot?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let socket: WebSocket | null = null;
|
|
|
|
|
|
|
|
|
|
const wssBaseUrl = import.meta.env.VITE_WSS_BASE_URL;
|
2024-07-11 03:08:52 -07:00
|
|
|
|
|
|
|
|
function WorkflowRun() {
|
2024-07-25 07:35:47 -07:00
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
|
const page = searchParams.get("page") ? Number(searchParams.get("page")) : 1;
|
2024-07-11 03:08:52 -07:00
|
|
|
const { workflowRunId, workflowPermanentId } = useParams();
|
|
|
|
|
const credentialGetter = useCredentialGetter();
|
2024-09-30 11:36:24 -07:00
|
|
|
const [streamImgSrc, setStreamImgSrc] = useState<string>("");
|
2024-07-11 03:08:52 -07:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { data: workflowRun, isLoading: workflowRunIsLoading } =
|
|
|
|
|
useQuery<WorkflowRunStatusApiResponse>({
|
|
|
|
|
queryKey: ["workflowRun", workflowPermanentId, workflowRunId],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const client = await getClient(credentialGetter);
|
|
|
|
|
return client
|
|
|
|
|
.get(`/workflows/${workflowPermanentId}/runs/${workflowRunId}`)
|
|
|
|
|
.then((response) => response.data);
|
|
|
|
|
},
|
2024-09-24 11:54:20 -07:00
|
|
|
refetchInterval: (query) => {
|
2024-09-30 11:36:24 -07:00
|
|
|
if (!query.state.data) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (statusIsNotFinalized(query.state.data)) {
|
2024-09-24 11:54:20 -07:00
|
|
|
return 5000;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
placeholderData: keepPreviousData,
|
2024-07-11 03:08:52 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { data: workflowTasks, isLoading: workflowTasksIsLoading } = useQuery<
|
|
|
|
|
Array<TaskApiResponse>
|
|
|
|
|
>({
|
2024-07-25 07:35:47 -07:00
|
|
|
queryKey: ["workflowTasks", workflowRunId, page],
|
2024-07-11 03:08:52 -07:00
|
|
|
queryFn: async () => {
|
|
|
|
|
const client = await getClient(credentialGetter);
|
2024-07-25 07:35:47 -07:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.append("page", String(page));
|
2024-07-11 03:08:52 -07:00
|
|
|
return client
|
2024-07-25 07:35:47 -07:00
|
|
|
.get(`/tasks?workflow_run_id=${workflowRunId}`, { params })
|
2024-07-11 03:08:52 -07:00
|
|
|
.then((response) => response.data);
|
|
|
|
|
},
|
2024-09-24 11:54:20 -07:00
|
|
|
refetchInterval: () => {
|
|
|
|
|
if (workflowRun?.status === Status.Running) {
|
|
|
|
|
return 5000;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
placeholderData: keepPreviousData,
|
2024-09-24 12:16:01 -07:00
|
|
|
refetchOnMount: workflowRun?.status === Status.Running,
|
2024-07-11 03:08:52 -07:00
|
|
|
});
|
|
|
|
|
|
2024-09-30 11:36:24 -07:00
|
|
|
const workflowRunIsRunningOrQueued =
|
|
|
|
|
workflowRun && statusIsRunningOrQueued(workflowRun);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!workflowRunIsRunningOrQueued) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
|
// Create WebSocket connection.
|
|
|
|
|
let credential = null;
|
|
|
|
|
if (credentialGetter) {
|
|
|
|
|
const token = await credentialGetter();
|
|
|
|
|
credential = `?token=Bearer ${token}`;
|
|
|
|
|
} else {
|
|
|
|
|
credential = `?apikey=${envCredential}`;
|
|
|
|
|
}
|
|
|
|
|
if (socket) {
|
|
|
|
|
socket.close();
|
|
|
|
|
}
|
|
|
|
|
socket = new WebSocket(
|
|
|
|
|
`${wssBaseUrl}/stream/workflow_runs/${workflowRunId}${credential}`,
|
|
|
|
|
);
|
|
|
|
|
// Listen for messages
|
|
|
|
|
socket.addEventListener("message", (event) => {
|
|
|
|
|
try {
|
|
|
|
|
const message: StreamMessage = JSON.parse(event.data);
|
|
|
|
|
if (message.screenshot) {
|
|
|
|
|
setStreamImgSrc(message.screenshot);
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
message.status === "completed" ||
|
|
|
|
|
message.status === "failed" ||
|
|
|
|
|
message.status === "terminated"
|
|
|
|
|
) {
|
|
|
|
|
socket?.close();
|
|
|
|
|
if (
|
|
|
|
|
message.status === "failed" ||
|
|
|
|
|
message.status === "terminated"
|
|
|
|
|
) {
|
|
|
|
|
toast({
|
|
|
|
|
title: "Run Failed",
|
|
|
|
|
description: "The workflow run has failed.",
|
|
|
|
|
variant: "destructive",
|
|
|
|
|
});
|
|
|
|
|
} else if (message.status === "completed") {
|
|
|
|
|
toast({
|
|
|
|
|
title: "Run Completed",
|
|
|
|
|
description: "The workflow run has been completed.",
|
|
|
|
|
variant: "success",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Failed to parse message", e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.addEventListener("close", () => {
|
|
|
|
|
socket = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
run();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (socket) {
|
|
|
|
|
socket.close();
|
|
|
|
|
socket = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [credentialGetter, workflowRunId, workflowRunIsRunningOrQueued]);
|
|
|
|
|
|
|
|
|
|
function getStream() {
|
|
|
|
|
if (workflowRun?.status === Status.Created) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-full w-full flex-col items-center justify-center gap-8 bg-slate-900 py-8 text-lg">
|
|
|
|
|
<span>Workflow has been created.</span>
|
|
|
|
|
<span>Stream will start when the workflow is running.</span>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (workflowRun?.status === Status.Queued) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-full w-full flex-col items-center justify-center gap-8 bg-slate-900 py-8 text-lg">
|
|
|
|
|
<span>Your workflow run is queued.</span>
|
|
|
|
|
<span>Stream will start when the workflow is running.</span>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workflowRun?.status === Status.Running && streamImgSrc.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-full w-full items-center justify-center bg-slate-900 py-8 text-lg">
|
|
|
|
|
Starting the stream...
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workflowRun?.status === Status.Running && streamImgSrc.length > 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="h-full w-full">
|
|
|
|
|
<img src={`data:image/png;base64,${streamImgSrc}`} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 03:08:52 -07:00
|
|
|
function handleNavigate(event: React.MouseEvent, id: string) {
|
|
|
|
|
if (event.ctrlKey || event.metaKey) {
|
|
|
|
|
window.open(
|
|
|
|
|
window.location.origin + `/tasks/${id}/actions`,
|
|
|
|
|
"_blank",
|
|
|
|
|
"noopener,noreferrer",
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2024-07-11 09:02:22 -07:00
|
|
|
navigate(`/tasks/${id}/actions`);
|
2024-07-11 03:08:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parameters = workflowRun?.parameters ?? {};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-8">
|
2024-09-05 16:58:38 +03:00
|
|
|
<header className="flex justify-between">
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
<h1 className="text-lg font-semibold">{workflowRunId}</h1>
|
|
|
|
|
{workflowRunIsLoading ? (
|
|
|
|
|
<Skeleton className="h-8 w-28" />
|
|
|
|
|
) : workflowRun ? (
|
|
|
|
|
<StatusBadge status={workflowRun?.status} />
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
2024-09-05 18:40:58 +03:00
|
|
|
navigate(`/workflows/${workflowPermanentId}/run`, {
|
|
|
|
|
state: { data: parameters },
|
|
|
|
|
});
|
2024-09-05 16:58:38 +03:00
|
|
|
}}
|
|
|
|
|
variant="secondary"
|
|
|
|
|
>
|
|
|
|
|
Rerun Workflow
|
|
|
|
|
</Button>
|
2024-07-11 03:08:52 -07:00
|
|
|
</header>
|
2024-09-30 11:36:24 -07:00
|
|
|
{getStream()}
|
2024-07-11 03:08:52 -07:00
|
|
|
<div className="space-y-4">
|
|
|
|
|
<header>
|
|
|
|
|
<h2 className="text-lg font-semibold">Tasks</h2>
|
|
|
|
|
</header>
|
|
|
|
|
<div className="rounded-md border">
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHead className="w-1/4">ID</TableHead>
|
|
|
|
|
<TableHead className="w-1/4">URL</TableHead>
|
|
|
|
|
<TableHead className="w-1/6">Status</TableHead>
|
|
|
|
|
<TableHead className="w-1/4">Created At</TableHead>
|
|
|
|
|
<TableHead className="w-1/12" />
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{workflowTasksIsLoading ? (
|
|
|
|
|
<TaskListSkeletonRows />
|
|
|
|
|
) : workflowTasks?.length === 0 ? (
|
|
|
|
|
<TableRow>
|
2024-07-25 07:35:47 -07:00
|
|
|
<TableCell colSpan={5}>No tasks</TableCell>
|
2024-07-11 03:08:52 -07:00
|
|
|
</TableRow>
|
|
|
|
|
) : (
|
|
|
|
|
workflowTasks?.map((task) => {
|
|
|
|
|
return (
|
|
|
|
|
<TableRow key={task.task_id}>
|
|
|
|
|
<TableCell
|
|
|
|
|
className="w-1/4 cursor-pointer"
|
|
|
|
|
onClick={(event) => handleNavigate(event, task.task_id)}
|
|
|
|
|
>
|
|
|
|
|
{task.task_id}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell
|
2024-07-11 21:29:47 +03:00
|
|
|
className="w-1/4 max-w-64 cursor-pointer overflow-hidden overflow-ellipsis whitespace-nowrap"
|
2024-07-11 03:08:52 -07:00
|
|
|
onClick={(event) => handleNavigate(event, task.task_id)}
|
|
|
|
|
>
|
|
|
|
|
{task.request.url}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell
|
|
|
|
|
className="w-1/6 cursor-pointer"
|
|
|
|
|
onClick={(event) => handleNavigate(event, task.task_id)}
|
|
|
|
|
>
|
|
|
|
|
<StatusBadge status={task.status} />
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell
|
|
|
|
|
className="w-1/4 cursor-pointer"
|
|
|
|
|
onClick={(event) => handleNavigate(event, task.task_id)}
|
|
|
|
|
>
|
|
|
|
|
{basicTimeFormat(task.created_at)}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className="w-1/12">
|
|
|
|
|
<TaskActions task={task} />
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
2024-07-25 07:35:47 -07:00
|
|
|
<Pagination className="pt-2">
|
|
|
|
|
<PaginationContent>
|
|
|
|
|
<PaginationItem>
|
|
|
|
|
<PaginationPrevious
|
|
|
|
|
className={cn({ "cursor-not-allowed": page === 1 })}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (page === 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.set("page", String(Math.max(1, page - 1)));
|
|
|
|
|
setSearchParams(params, { replace: true });
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</PaginationItem>
|
|
|
|
|
<PaginationItem>
|
|
|
|
|
<PaginationLink>{page}</PaginationLink>
|
|
|
|
|
</PaginationItem>
|
|
|
|
|
<PaginationItem>
|
|
|
|
|
<PaginationNext
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.set("page", String(page + 1));
|
|
|
|
|
setSearchParams(params, { replace: true });
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</PaginationItem>
|
|
|
|
|
</PaginationContent>
|
|
|
|
|
</Pagination>
|
2024-07-11 03:08:52 -07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<header>
|
|
|
|
|
<h2 className="text-lg font-semibold">Parameters</h2>
|
|
|
|
|
</header>
|
|
|
|
|
{Object.entries(parameters).map(([key, value]) => {
|
|
|
|
|
return (
|
|
|
|
|
<div key={key} className="flex flex-col gap-2">
|
|
|
|
|
<Label>{key}</Label>
|
|
|
|
|
{typeof value === "string" ? (
|
|
|
|
|
<Input value={value} readOnly />
|
|
|
|
|
) : (
|
|
|
|
|
<Input value={JSON.stringify(value)} readOnly />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { WorkflowRun };
|