import { swrFetcher } from "@/api/api"; import { Checkbox } from "@/components/ui/checkbox"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { ModelCron } from "@/types"; import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"; import useSWR from "swr"; import { useEffect, useMemo } from "react"; import { ActionButtonGroup } from "@/components/action-button-group"; import { HeaderButtonGroup } from "@/components/header-button-group"; import { toast } from "sonner"; import { deleteCron, runCron } from "@/api/cron"; import { CronCard } from "@/components/cron"; import { cronTypes } from "@/types"; import { IconButton } from "@/components/xui/icon-button"; export default function CronPage() { const { data, mutate, error, isLoading } = useSWR("/api/v1/cron", swrFetcher); useEffect(() => { if (error) toast("Error", { description: `Error fetching resource: ${error.message}.`, }); }, [error]); const columns: ColumnDef[] = [ { id: "select", header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="Select row" /> ), enableSorting: false, enableHiding: false, }, { header: "ID", accessorKey: "id", accessorFn: (row) => row.id, }, { header: "Name", accessorKey: "name", cell: ({ row }) => { const s = row.original; return
{s.name}
; }, }, { header: "Task Type", accessorKey: "taskType", accessorFn: (row) => cronTypes[row.task_type] || "", }, { header: "Cron Expression", accessorKey: "scheduler", accessorFn: (row) => row.scheduler, }, { header: "Command", accessorKey: "command", cell: ({ row }) => { const s = row.original; return
{s.command}
; }, }, { header: "Notifier Group", accessorKey: "ngroup", accessorFn: (row) => row.notification_group_id, }, { header: "Send Success Notification", accessorKey: "pushSuccessful", accessorFn: (row) => row.push_successful ?? false, }, { header: "Coverage", accessorKey: "cover", accessorFn: (row) => row.cover, cell: ({ row }) => { const s = row.original; return (
{(() => { switch (s.cover) { case 0: { return Ignore All; } case 1: { return Cover All; } case 2: { return On alert; } } })()}
); }, }, { header: "Specific Servers", accessorKey: "servers", accessorFn: (row) => row.servers, }, { header: "Last Execution", accessorKey: "lastExecution", accessorFn: (row) => row.last_executed_at, cell: ({ row }) => { const s = row.original; return
{s.last_executed_at}
; }, }, { header: "Last Result", accessorKey: "lastResult", accessorFn: (row) => row.last_result ?? false, }, { id: "actions", header: "Actions", cell: ({ row }) => { const s = row.original; return ( <> { try { await runCron(s.id); } catch (e) { console.error(e); toast("Error executing task", { description: "Please see the console for details.", }); await mutate(); return; } toast("Success", { description: "The task triggered successfully.", }); await mutate(); }} /> ); }, }, ]; const dataCache = useMemo(() => { return data ?? []; }, [data]); const table = useReactTable({ data: dataCache, columns, getCoreRowModel: getCoreRowModel(), }); const selectedRows = table.getSelectedRowModel().rows; return (

Task

r.original.id), mutate: mutate, }} >
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} ); })} ))} {isLoading ? ( Loading ... ) : table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No results. )}
); }