import { useState, useEffect } from 'react' import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Progress } from "@/components/ui/progress" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer } from 'recharts' interface Task { id: string; status: "Processed" | "Processing" | "Queued" | "Exception"; createdAt: Date; title: string; description: string; expectedProgress: number; currentJobDescription: string; } const generateMockTasks = (): Task[] => { const statuses: Task['status'][] = ["Processed", "Processing", "Queued", "Exception"]; return Array.from({ length: 50 }, (_, i) => ({ id: `task-${i + 1}`, status: statuses[Math.floor(Math.random() * statuses.length)], createdAt: new Date(Date.now() - Math.random() * 10000000000), title: `Task ${i + 1}`, description: `This is a description for Task ${i + 1}`, expectedProgress: Math.random(), currentJobDescription: `Current job for Task ${i + 1}` })); }; export default function DynamicWorkQueue() { const [tasks, setTasks] = useState([]) useEffect(() => { setTasks(generateMockTasks()); const intervalId = setInterval(() => { setTasks(prevTasks => { let newTasks: Task[] = prevTasks; // if processed tasks are more than 10, remove the oldest one if (newTasks.filter(task => task.status === "Processed").length > 10) { newTasks = newTasks.filter(task => task.status !== "Processed"); } // update the progress of each task newTasks = newTasks.map(task => ({ ...task, expectedProgress: Math.min(1, task.expectedProgress + Math.random() * 0.2), status: task.expectedProgress >= 1 ? "Processed" : task.status })); // if there are no queued tasks, add a new one if (newTasks.filter(task => task.status === "Queued").length === 0) { newTasks.push({ id: `task-${newTasks.length + 1}`, status: "Queued", createdAt: new Date(), title: `Task ${newTasks.length + 1}`, description: `This is a description for Task ${newTasks.length + 1}`, expectedProgress: 0, currentJobDescription: `Current job for Task ${newTasks.length + 1}` }); } return newTasks; }); }, 5000); return () => clearInterval(intervalId); }, []) const updateTaskStatus = (taskId: string, newStatus: Task['status']) => { setTasks(prevTasks => prevTasks.map(task => task.id === taskId ? { ...task, status: newStatus } : task ) ); } const renderTaskList = (status: Task['status']) => { const filteredTasks = tasks.filter(task => task.status === status) return ( <> {filteredTasks.length === 0 ? (

No tasks

) : ( )} ) } const renderProcessedTaskSummary = () => { const processedTasks = tasks.filter(task => task.status === "Processed"); const totalProcessed = processedTasks.length; const averageProgress = processedTasks.reduce((sum, task) => sum + task.expectedProgress, 0) / totalProcessed || 0; const chartData = [ { name: 'Processed', value: totalProcessed }, { name: 'Remaining', value: tasks.length - totalProcessed }, ]; return (
Total Processed

{totalProcessed}

Average Progress

{(averageProgress * 100).toFixed(2)}%

Processed vs Remaining Tasks
) } return (
Dynamic Work Queue Queued ({tasks.filter(t => t.status === "Queued").length}) Processing ({tasks.filter(t => t.status === "Processing").length}) Processed ({tasks.filter(t => t.status === "Processed").length}) Exception ({tasks.filter(t => t.status === "Exception").length}) Summary {renderTaskList("Queued")} {renderTaskList("Processing")} {renderTaskList("Processed")} {renderTaskList("Exception")} {renderProcessedTaskSummary()}
) }