implement cron page (#7)

This commit is contained in:
UUBulb
2024-11-20 00:19:40 +08:00
committed by GitHub
parent 2bf2639080
commit e37f30d335
20 changed files with 555 additions and 28 deletions

237
src/routes/cron.tsx Normal file
View File

@@ -0,0 +1,237 @@
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 } from "react"
import { ActionButtonGroup } from "@/components/action-button-group"
import { HeaderButtonGroup } from "@/components/header-button-group"
import { Skeleton } from "@/components/ui/skeleton"
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<ModelCron[]>('/api/v1/cron', swrFetcher);
useEffect(() => {
if (error)
toast("Error", {
description: `Error fetching resource: ${error.message}.`,
})
}, [error])
const columns: ColumnDef<ModelCron>[] = [
{
id: "select",
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => 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 (
<div className="max-w-32 whitespace-normal break-words">
{s.name}
</div>
)
}
},
{
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 (
<div className="max-w-48 whitespace-normal break-words">
{s.command}
</div>
)
}
},
{
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 => {
switch (row.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
},
{
header: "Last Result",
accessorKey: "lastResult",
accessorFn: row => row.last_result ?? false,
},
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
const s = row.original
return (
<ActionButtonGroup className="flex gap-2" delete={{ fn: deleteCron, id: s.id, mutate: mutate }}>
<>
<IconButton variant="outline" icon="play" onClick={
async () => {
try {
await runCron(s.id);
} catch (e) {
console.log(e);
toast("Error executing task", {
description: "Please see the console for details.",
})
await mutate()
}
toast("Success", {
description: "The task triggered successfully.",
})
await mutate()
}} />
<CronCard mutate={mutate} data={s} />
</>
</ActionButtonGroup>
)
},
},
]
const table = useReactTable({
data: data ?? [],
columns,
getCoreRowModel: getCoreRowModel(),
})
const selectedRows = table.getSelectedRowModel().rows;
return (
<div className="px-8">
<div className="flex mt-6 mb-4">
<h1 className="flex-1 text-3xl font-bold tracking-tight">
Task
</h1>
<HeaderButtonGroup className="flex-2 flex ml-auto gap-2" delete={{
fn: deleteCron,
id: selectedRows.map(r => r.original.id),
mutate: mutate,
}}>
<CronCard mutate={mutate} />
</HeaderButtonGroup>
</div>
{isLoading ? (
<div className="flex flex-col items-center space-y-4">
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
</div>
) : (
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id} className="text-sm">
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id} className="text-xsm">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)}
</div >
)
}

View File

@@ -64,7 +64,7 @@ export default function NotificationGroupPage() {
}
},
{
header: "Notification methods (ID)",
header: "Notifiers (ID)",
accessorKey: "notifications",
accessorFn: row => row.notifications,
},

View File

@@ -106,7 +106,7 @@ export default function ServicePage() {
accessorFn: row => row.service.duration,
},
{
header: "Notification Group ID",
header: "Notifier Group ID",
accessorKey: "service.ngroup",
accessorFn: row => row.service.notification_group_id,
},