2020-11-29 07:20:54 -08:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
import clsx from "clsx";
|
|
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
|
|
|
|
import Table from "@material-ui/core/Table";
|
|
|
|
|
import TableBody from "@material-ui/core/TableBody";
|
|
|
|
|
import TableCell from "@material-ui/core/TableCell";
|
|
|
|
|
import TableContainer from "@material-ui/core/TableContainer";
|
|
|
|
|
import TableHead from "@material-ui/core/TableHead";
|
|
|
|
|
import TableRow from "@material-ui/core/TableRow";
|
2020-12-03 07:09:02 -08:00
|
|
|
import Alert from "@material-ui/lab/Alert";
|
|
|
|
|
import AlertTitle from "@material-ui/lab/AlertTitle";
|
2020-11-29 07:20:54 -08:00
|
|
|
import SyntaxHighlighter from "react-syntax-highlighter";
|
|
|
|
|
import syntaxHighlightStyle from "react-syntax-highlighter/dist/esm/styles/hljs/github";
|
2020-12-23 14:38:24 -08:00
|
|
|
import { SortDirection, SortableTableColumn } from "../types/table";
|
2020-11-29 07:20:54 -08:00
|
|
|
import TableSortLabel from "@material-ui/core/TableSortLabel";
|
2020-12-03 07:09:02 -08:00
|
|
|
import { SchedulerEntry } from "../api";
|
2020-12-21 06:51:07 -08:00
|
|
|
import { timeAgo, durationBefore } from "../utils";
|
2020-11-29 07:20:54 -08:00
|
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
|
table: {
|
|
|
|
|
minWidth: 650,
|
|
|
|
|
},
|
|
|
|
|
noBorder: {
|
|
|
|
|
border: "none",
|
|
|
|
|
},
|
|
|
|
|
fixedCell: {
|
|
|
|
|
position: "sticky",
|
|
|
|
|
zIndex: 1,
|
|
|
|
|
left: 0,
|
|
|
|
|
background: theme.palette.common.white,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
enum SortBy {
|
|
|
|
|
EntryId,
|
|
|
|
|
Spec,
|
|
|
|
|
Type,
|
|
|
|
|
Payload,
|
|
|
|
|
Options,
|
|
|
|
|
NextEnqueue,
|
|
|
|
|
PrevEnqueue,
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 14:38:24 -08:00
|
|
|
const colConfigs: SortableTableColumn<SortBy>[] = [
|
2020-11-29 07:20:54 -08:00
|
|
|
{
|
|
|
|
|
label: "Entry ID",
|
|
|
|
|
key: "entry_id",
|
|
|
|
|
sortBy: SortBy.EntryId,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Spec",
|
|
|
|
|
key: "spec",
|
|
|
|
|
sortBy: SortBy.Spec,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Type",
|
|
|
|
|
key: "type",
|
|
|
|
|
sortBy: SortBy.Type,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Payload",
|
2020-12-03 07:09:02 -08:00
|
|
|
key: "task_payload",
|
2020-11-29 07:20:54 -08:00
|
|
|
sortBy: SortBy.Payload,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Options",
|
|
|
|
|
key: "options",
|
|
|
|
|
sortBy: SortBy.Options,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Next Enqueue",
|
|
|
|
|
key: "next_enqueue",
|
|
|
|
|
sortBy: SortBy.NextEnqueue,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Prev Enqueue",
|
|
|
|
|
key: "prev_enqueue",
|
|
|
|
|
sortBy: SortBy.PrevEnqueue,
|
|
|
|
|
align: "left",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// sortEntries takes a array of entries and return a sorted array.
|
|
|
|
|
// It returns a new array and leave the original array untouched.
|
|
|
|
|
function sortEntries(
|
2020-12-03 07:09:02 -08:00
|
|
|
entries: SchedulerEntry[],
|
|
|
|
|
cmpFn: (first: SchedulerEntry, second: SchedulerEntry) => number
|
|
|
|
|
): SchedulerEntry[] {
|
2020-11-29 07:20:54 -08:00
|
|
|
let copy = [...entries];
|
|
|
|
|
copy.sort(cmpFn);
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 07:09:02 -08:00
|
|
|
interface Props {
|
|
|
|
|
entries: SchedulerEntry[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SchedulerEntriesTable(props: Props) {
|
2020-11-29 07:20:54 -08:00
|
|
|
const classes = useStyles();
|
|
|
|
|
const [sortBy, setSortBy] = useState<SortBy>(SortBy.EntryId);
|
|
|
|
|
const [sortDir, setSortDir] = useState<SortDirection>(SortDirection.Asc);
|
|
|
|
|
|
|
|
|
|
const createSortClickHandler = (sortKey: SortBy) => (e: React.MouseEvent) => {
|
|
|
|
|
if (sortKey === sortBy) {
|
|
|
|
|
// Toggle sort direction.
|
|
|
|
|
const nextSortDir =
|
|
|
|
|
sortDir === SortDirection.Asc ? SortDirection.Desc : SortDirection.Asc;
|
|
|
|
|
setSortDir(nextSortDir);
|
|
|
|
|
} else {
|
|
|
|
|
// Change the sort key.
|
|
|
|
|
setSortBy(sortKey);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-04 06:48:58 -08:00
|
|
|
const cmpFunc = (e1: SchedulerEntry, e2: SchedulerEntry): number => {
|
2020-11-29 07:20:54 -08:00
|
|
|
let isE1Smaller: boolean;
|
|
|
|
|
switch (sortBy) {
|
|
|
|
|
case SortBy.EntryId:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.id === e2.id) return 0;
|
|
|
|
|
isE1Smaller = e1.id < e2.id;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.Spec:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.spec === e2.spec) return 0;
|
|
|
|
|
isE1Smaller = e1.spec < e2.spec;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.Type:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.task_type === e2.task_type) return 0;
|
|
|
|
|
isE1Smaller = e1.task_type < e2.task_type;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.Payload:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.task_payload === e2.task_payload) return 0;
|
|
|
|
|
isE1Smaller = e1.task_payload < e2.task_payload;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.Options:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.options === e2.options) return 0;
|
|
|
|
|
isE1Smaller = e1.options < e2.options;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.NextEnqueue:
|
2020-12-04 06:48:58 -08:00
|
|
|
if (e1.next_enqueue_at === e2.next_enqueue_at) return 0;
|
|
|
|
|
isE1Smaller = e1.next_enqueue_at < e2.next_enqueue_at;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
case SortBy.PrevEnqueue:
|
2020-12-04 06:48:58 -08:00
|
|
|
const e1PrevEnqueueAt = e1.prev_enqueue_at || "";
|
|
|
|
|
const e2PrevEnqueueAt = e2.prev_enqueue_at || "";
|
|
|
|
|
if (e1PrevEnqueueAt === e2PrevEnqueueAt) return 0;
|
|
|
|
|
isE1Smaller = e1PrevEnqueueAt < e2PrevEnqueueAt;
|
2020-11-29 07:20:54 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// eslint-disable-next-line no-throw-literal
|
|
|
|
|
throw `Unexpected order by value: ${sortBy}`;
|
|
|
|
|
}
|
|
|
|
|
if (sortDir === SortDirection.Asc) {
|
|
|
|
|
return isE1Smaller ? -1 : 1;
|
|
|
|
|
} else {
|
|
|
|
|
return isE1Smaller ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-03 07:09:02 -08:00
|
|
|
if (props.entries.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<Alert severity="info">
|
|
|
|
|
<AlertTitle>Info</AlertTitle>
|
|
|
|
|
No entries found at this time.
|
|
|
|
|
</Alert>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 07:20:54 -08:00
|
|
|
return (
|
|
|
|
|
<TableContainer>
|
|
|
|
|
<Table className={classes.table} aria-label="simple table">
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
{colConfigs.map((cfg, i) => (
|
|
|
|
|
<TableCell
|
|
|
|
|
key={cfg.key}
|
|
|
|
|
align={cfg.align}
|
|
|
|
|
className={clsx(i === 0 && classes.fixedCell)}
|
|
|
|
|
>
|
|
|
|
|
<TableSortLabel
|
|
|
|
|
active={cfg.sortBy === sortBy}
|
|
|
|
|
direction={sortDir}
|
|
|
|
|
onClick={createSortClickHandler(cfg.sortBy)}
|
|
|
|
|
>
|
|
|
|
|
{cfg.label}
|
|
|
|
|
</TableSortLabel>
|
|
|
|
|
</TableCell>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
2020-12-03 07:09:02 -08:00
|
|
|
{sortEntries(props.entries, cmpFunc).map((entry, idx) => {
|
|
|
|
|
const isLastRow = idx === props.entries.length - 1;
|
2020-11-29 07:20:54 -08:00
|
|
|
return (
|
2020-12-03 07:09:02 -08:00
|
|
|
<TableRow key={entry.id}>
|
2020-11-29 07:20:54 -08:00
|
|
|
<TableCell
|
|
|
|
|
component="th"
|
|
|
|
|
scope="row"
|
|
|
|
|
className={clsx(isLastRow && classes.noBorder)}
|
|
|
|
|
>
|
2020-12-03 07:09:02 -08:00
|
|
|
{entry.id}
|
2020-11-29 07:20:54 -08:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
2020-12-03 07:09:02 -08:00
|
|
|
{entry.spec}
|
2020-11-29 07:20:54 -08:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
2020-12-03 07:09:02 -08:00
|
|
|
{entry.task_type}
|
2020-11-29 07:20:54 -08:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
|
|
|
|
<SyntaxHighlighter
|
|
|
|
|
language="json"
|
|
|
|
|
style={syntaxHighlightStyle}
|
|
|
|
|
>
|
2020-12-03 07:09:02 -08:00
|
|
|
{JSON.stringify(entry.task_payload)}
|
2020-11-29 07:20:54 -08:00
|
|
|
</SyntaxHighlighter>
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
|
|
|
|
<SyntaxHighlighter language="go" style={syntaxHighlightStyle}>
|
2020-12-03 07:09:02 -08:00
|
|
|
{entry.options.length > 0
|
|
|
|
|
? entry.options.join(", ")
|
|
|
|
|
: "No options"}
|
2020-11-29 07:20:54 -08:00
|
|
|
</SyntaxHighlighter>
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
2020-12-03 07:09:02 -08:00
|
|
|
{durationBefore(entry.next_enqueue_at)}
|
2020-11-29 07:20:54 -08:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell className={clsx(isLastRow && classes.noBorder)}>
|
2020-12-04 06:48:58 -08:00
|
|
|
{entry.prev_enqueue_at
|
|
|
|
|
? timeAgo(entry.prev_enqueue_at)
|
|
|
|
|
: "N/A"}
|
2020-11-29 07:20:54 -08:00
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|