asynqmon/ui/src/components/RetryTasksTable.tsx

433 lines
14 KiB
TypeScript
Raw Normal View History

2020-11-29 15:04:24 -08:00
import React, { useCallback, useState } from "react";
2020-11-24 06:54:00 -08:00
import { connect, ConnectedProps } from "react-redux";
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";
import TableFooter from "@material-ui/core/TableFooter";
import TablePagination from "@material-ui/core/TablePagination";
import Paper from "@material-ui/core/Paper";
import Box from "@material-ui/core/Box";
2020-12-23 14:38:24 -08:00
import Tooltip from "@material-ui/core/Tooltip";
2020-12-18 06:48:00 -08:00
import Checkbox from "@material-ui/core/Checkbox";
2020-11-24 06:54:00 -08:00
import Collapse from "@material-ui/core/Collapse";
import IconButton from "@material-ui/core/IconButton";
import PlayArrowIcon from "@material-ui/icons/PlayArrow";
import DeleteIcon from "@material-ui/icons/Delete";
import ArchiveIcon from "@material-ui/icons/Archive";
2020-12-23 14:38:24 -08:00
import MoreHorizIcon from "@material-ui/icons/MoreHoriz";
2020-11-24 06:54:00 -08:00
import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp";
import KeyboardArrowDownIcon from "@material-ui/icons/KeyboardArrowDown";
import Typography from "@material-ui/core/Typography";
import Alert from "@material-ui/lab/Alert";
import AlertTitle from "@material-ui/lab/AlertTitle";
import SyntaxHighlighter from "react-syntax-highlighter";
import syntaxHighlightStyle from "react-syntax-highlighter/dist/esm/styles/hljs/github";
import {
batchDeleteRetryTasksAsync,
batchRunRetryTasksAsync,
batchKillRetryTasksAsync,
deleteAllRetryTasksAsync,
runAllRetryTasksAsync,
killAllRetryTasksAsync,
listRetryTasksAsync,
deleteRetryTaskAsync,
runRetryTaskAsync,
killRetryTaskAsync,
} from "../actions/tasksActions";
2020-11-24 06:54:00 -08:00
import { AppState } from "../store";
import TablePaginationActions, {
defaultPageSize,
rowsPerPageOptions,
} from "./TablePaginationActions";
2020-12-18 12:48:05 -08:00
import TableActions from "./TableActions";
2020-12-21 06:51:07 -08:00
import { durationBefore, uuidPrefix } from "../utils";
2020-11-29 15:04:24 -08:00
import { usePolling } from "../hooks";
import { RetryTaskExtended } from "../reducers/tasksReducer";
2020-12-23 14:38:24 -08:00
import clsx from "clsx";
import { TableColumn } from "../types/table";
2020-11-24 06:54:00 -08:00
const useStyles = makeStyles({
table: {
minWidth: 650,
},
});
function mapStateToProps(state: AppState) {
return {
loading: state.tasks.retryTasks.loading,
tasks: state.tasks.retryTasks.data,
2020-12-18 12:48:05 -08:00
batchActionPending: state.tasks.retryTasks.batchActionPending,
allActionPending: state.tasks.retryTasks.allActionPending,
2020-11-24 06:54:00 -08:00
pollInterval: state.settings.pollInterval,
};
}
const mapDispatchToProps = {
batchDeleteRetryTasksAsync,
batchRunRetryTasksAsync,
batchKillRetryTasksAsync,
deleteAllRetryTasksAsync,
runAllRetryTasksAsync,
killAllRetryTasksAsync,
listRetryTasksAsync,
deleteRetryTaskAsync,
runRetryTaskAsync,
killRetryTaskAsync,
};
2020-11-24 06:54:00 -08:00
const connector = connect(mapStateToProps, mapDispatchToProps);
type ReduxProps = ConnectedProps<typeof connector>;
interface Props {
queue: string; // name of the queue.
totalTaskCount: number; // totoal number of scheduled tasks.
}
function RetryTasksTable(props: Props & ReduxProps) {
const { pollInterval, listRetryTasksAsync, queue } = props;
const classes = useStyles();
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(defaultPageSize);
2020-12-18 06:48:00 -08:00
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
2020-12-23 14:38:24 -08:00
const [activeTaskId, setActiveTaskId] = useState<string>("");
2020-11-24 06:54:00 -08:00
const handleChangePage = (
event: React.MouseEvent<HTMLButtonElement> | null,
newPage: number
) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
setPageSize(parseInt(event.target.value, 10));
setPage(0);
};
2020-12-18 06:48:00 -08:00
const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.checked) {
const newSelected = props.tasks.map((t) => t.key);
setSelectedKeys(newSelected);
} else {
setSelectedKeys([]);
}
};
const handleRunAllClick = () => {
props.runAllRetryTasksAsync(queue);
};
const handleDeleteAllClick = () => {
props.deleteAllRetryTasksAsync(queue);
};
const handleKillAllClick = () => {
props.killAllRetryTasksAsync(queue);
};
const handleBatchRunClick = () => {
props
.batchRunRetryTasksAsync(queue, selectedKeys)
.then(() => setSelectedKeys([]));
};
const handleBatchDeleteClick = () => {
props
.batchDeleteRetryTasksAsync(queue, selectedKeys)
.then(() => setSelectedKeys([]));
};
const handleBatchKillClick = () => {
props
.batchKillRetryTasksAsync(queue, selectedKeys)
.then(() => setSelectedKeys([]));
};
2020-11-29 15:04:24 -08:00
const fetchData = useCallback(() => {
2020-11-24 06:54:00 -08:00
const pageOpts = { page: page + 1, size: pageSize };
listRetryTasksAsync(queue, pageOpts);
2020-11-29 15:04:24 -08:00
}, [page, pageSize, queue, listRetryTasksAsync]);
usePolling(fetchData, pollInterval);
2020-11-24 06:54:00 -08:00
if (props.tasks.length === 0) {
return (
<Alert severity="info">
<AlertTitle>Info</AlertTitle>
No retry tasks at this time.
</Alert>
);
}
2020-12-23 14:38:24 -08:00
const columns: TableColumn[] = [
{ key: "icon", label: "", align: "left" },
{ key: "id", label: "ID", align: "left" },
{ key: "type", label: "Type", align: "left" },
{ key: "retry_in", label: "Retry In", align: "left" },
{ key: "last_error", label: "Last Error", align: "left" },
{ key: "retried", label: "Retried", align: "left" },
{ key: "max_retry", label: "Max Retry", align: "left" },
{ key: "actions", label: "Actions", align: "center" },
2020-11-24 06:54:00 -08:00
];
2020-12-18 06:48:00 -08:00
const rowCount = props.tasks.length;
const numSelected = selectedKeys.length;
2020-11-24 06:54:00 -08:00
return (
2020-12-18 12:48:05 -08:00
<div>
<TableActions
showIconButtons={numSelected > 0}
iconButtonActions={[
{
tooltip: "Delete",
icon: <DeleteIcon />,
onClick: handleBatchDeleteClick,
disabled: props.batchActionPending,
},
{
tooltip: "Kill",
icon: <ArchiveIcon />,
onClick: handleBatchKillClick,
disabled: props.batchActionPending,
},
{
tooltip: "Run",
icon: <PlayArrowIcon />,
onClick: handleBatchRunClick,
disabled: props.batchActionPending,
},
]}
menuItemActions={[
{
label: "Delete All",
onClick: handleDeleteAllClick,
disabled: props.allActionPending,
},
{
label: "Kill All",
onClick: handleKillAllClick,
disabled: props.allActionPending,
},
{
label: "Run All",
onClick: handleRunAllClick,
disabled: props.allActionPending,
},
]}
2020-12-18 12:48:05 -08:00
/>
<TableContainer component={Paper}>
<Table
stickyHeader={true}
className={classes.table}
aria-label="retry tasks table"
size="small"
>
<TableHead>
<TableRow>
<TableCell padding="checkbox">
<Checkbox
indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount}
onChange={handleSelectAllClick}
inputProps={{
"aria-label": "select all tasks shown in the table",
}}
/>
</TableCell>
{columns.map((col) => (
2020-12-23 14:38:24 -08:00
<TableCell key={col.label} align={col.align}>
{col.label}
</TableCell>
2020-12-18 12:48:05 -08:00
))}
</TableRow>
</TableHead>
<TableBody>
{props.tasks.map((task) => (
<Row
key={task.id}
task={task}
allActionPending={props.allActionPending}
2020-12-18 12:48:05 -08:00
isSelected={selectedKeys.includes(task.key)}
onSelectChange={(checked: boolean) => {
if (checked) {
setSelectedKeys(selectedKeys.concat(task.key));
} else {
setSelectedKeys(
selectedKeys.filter((key) => key !== task.key)
);
}
}}
onRunClick={() => {
props.runRetryTaskAsync(task.queue, task.key);
}}
2020-12-18 12:48:05 -08:00
onDeleteClick={() => {
props.deleteRetryTaskAsync(task.queue, task.key);
2020-12-18 06:48:00 -08:00
}}
onKillClick={() => {
props.killRetryTaskAsync(task.queue, task.key);
}}
2020-12-23 14:38:24 -08:00
onActionCellEnter={() => setActiveTaskId(task.id)}
onActionCellLeave={() => setActiveTaskId("")}
showActions={activeTaskId === task.id}
2020-12-18 06:48:00 -08:00
/>
2020-11-24 06:54:00 -08:00
))}
2020-12-18 12:48:05 -08:00
</TableBody>
<TableFooter>
<TableRow>
<TablePagination
rowsPerPageOptions={rowsPerPageOptions}
colSpan={columns.length + 1}
count={props.totalTaskCount}
rowsPerPage={pageSize}
page={page}
SelectProps={{
inputProps: { "aria-label": "rows per page" },
native: true,
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
ActionsComponent={TablePaginationActions}
/>
</TableRow>
</TableFooter>
</Table>
</TableContainer>
</div>
2020-11-24 06:54:00 -08:00
);
}
const useRowStyles = makeStyles({
root: {
"& > *": {
borderBottom: "unset",
},
},
2020-12-23 14:38:24 -08:00
actionCell: {
width: "140px",
},
activeActionCell: {
display: "flex",
justifyContent: "space-between",
},
2020-11-24 06:54:00 -08:00
});
interface RowProps {
task: RetryTaskExtended;
2020-12-18 06:48:00 -08:00
isSelected: boolean;
onSelectChange: (checked: boolean) => void;
onDeleteClick: () => void;
onRunClick: () => void;
onKillClick: () => void;
allActionPending: boolean;
2020-12-23 14:38:24 -08:00
showActions: boolean;
onActionCellEnter: () => void;
onActionCellLeave: () => void;
}
function Row(props: RowProps) {
2020-11-24 06:54:00 -08:00
const { task } = props;
const [open, setOpen] = React.useState(false);
const classes = useRowStyles();
return (
<React.Fragment>
2020-12-20 17:26:22 -08:00
<TableRow
key={task.id}
className={classes.root}
selected={props.isSelected}
>
2020-12-18 06:48:00 -08:00
<TableCell padding="checkbox">
<Checkbox
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
props.onSelectChange(event.target.checked)
}
checked={props.isSelected}
/>
</TableCell>
2020-11-24 06:54:00 -08:00
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => setOpen(!open)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
<TableCell component="th" scope="row">
2020-12-21 06:51:07 -08:00
{uuidPrefix(task.id)}
2020-11-24 06:54:00 -08:00
</TableCell>
<TableCell>{task.type}</TableCell>
<TableCell>{durationBefore(task.next_process_at)}</TableCell>
<TableCell>{task.error_message}</TableCell>
<TableCell>{task.retried}</TableCell>
<TableCell>{task.max_retry}</TableCell>
2020-12-23 14:38:24 -08:00
<TableCell
align="center"
className={clsx(
classes.actionCell,
props.showActions && classes.activeActionCell
)}
onMouseEnter={props.onActionCellEnter}
onMouseLeave={props.onActionCellLeave}
>
{props.showActions ? (
<React.Fragment>
<Tooltip title="Delete">
<IconButton
onClick={props.onDeleteClick}
disabled={task.requestPending || props.allActionPending}
size="small"
>
<DeleteIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip title="Kill">
<IconButton
onClick={props.onKillClick}
disabled={task.requestPending || props.allActionPending}
size="small"
>
<ArchiveIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip title="Run">
<IconButton
onClick={props.onRunClick}
disabled={task.requestPending || props.allActionPending}
size="small"
>
<PlayArrowIcon fontSize="small" />
</IconButton>
</Tooltip>
</React.Fragment>
) : (
<IconButton size="small" onClick={props.onActionCellEnter}>
<MoreHorizIcon fontSize="small" />
</IconButton>
)}
2020-11-24 06:54:00 -08:00
</TableCell>
</TableRow>
2020-12-20 17:26:22 -08:00
<TableRow selected={props.isSelected}>
2020-12-18 06:48:00 -08:00
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={10}>
2020-11-24 06:54:00 -08:00
<Collapse in={open} timeout="auto" unmountOnExit>
<Box margin={1}>
<Typography variant="h6" gutterBottom component="div">
Payload
</Typography>
<SyntaxHighlighter language="json" style={syntaxHighlightStyle}>
{JSON.stringify(task.payload, null, 2)}
</SyntaxHighlighter>
</Box>
</Collapse>
</TableCell>
</TableRow>
</React.Fragment>
);
}
export default connector(RetryTasksTable);