import Checkbox from "@material-ui/core/Checkbox"; import IconButton from "@material-ui/core/IconButton"; import Paper from "@material-ui/core/Paper"; 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 TableFooter from "@material-ui/core/TableFooter"; import TableHead from "@material-ui/core/TableHead"; import TablePagination from "@material-ui/core/TablePagination"; import TableRow from "@material-ui/core/TableRow"; import Tooltip from "@material-ui/core/Tooltip"; import ArchiveIcon from "@material-ui/icons/Archive"; import DeleteIcon from "@material-ui/icons/Delete"; import FileCopyOutlinedIcon from '@material-ui/icons/FileCopyOutlined'; import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; import PlayArrowIcon from "@material-ui/icons/PlayArrow"; import Alert from "@material-ui/lab/Alert"; import AlertTitle from "@material-ui/lab/AlertTitle"; import React, { useCallback, useState } from "react"; import { connect, ConnectedProps } from "react-redux"; import { useHistory } from "react-router-dom"; import { taskRowsPerPageChange } from "../actions/settingsActions"; import { archiveAllRetryTasksAsync, archiveRetryTaskAsync, batchArchiveRetryTasksAsync, batchDeleteRetryTasksAsync, batchRunRetryTasksAsync, deleteAllRetryTasksAsync, deleteRetryTaskAsync, listRetryTasksAsync, runAllRetryTasksAsync, runRetryTaskAsync } from "../actions/tasksActions"; import { usePolling } from "../hooks"; import { taskDetailsPath } from "../paths"; import { TaskInfoExtended } from "../reducers/tasksReducer"; import { AppState } from "../store"; import { TableColumn } from "../types/table"; import { durationBefore, prettifyPayload, uuidPrefix } from "../utils"; import SyntaxHighlighter from "./SyntaxHighlighter"; import TableActions from "./TableActions"; import TablePaginationActions, { rowsPerPageOptions } from "./TablePaginationActions"; const useStyles = makeStyles((theme) => ({ table: { minWidth: 650, }, stickyHeaderCell: { background: theme.palette.background.paper, }, alert: { borderTopLeftRadius: 0, borderTopRightRadius: 0, }, pagination: { border: "none", }, })); function mapStateToProps(state: AppState) { return { loading: state.tasks.retryTasks.loading, error: state.tasks.retryTasks.error, tasks: state.tasks.retryTasks.data, batchActionPending: state.tasks.retryTasks.batchActionPending, allActionPending: state.tasks.retryTasks.allActionPending, pollInterval: state.settings.pollInterval, pageSize: state.settings.taskRowsPerPage, }; } const mapDispatchToProps = { batchDeleteRetryTasksAsync, batchRunRetryTasksAsync, batchArchiveRetryTasksAsync, deleteAllRetryTasksAsync, runAllRetryTasksAsync, archiveAllRetryTasksAsync, listRetryTasksAsync, deleteRetryTaskAsync, runRetryTaskAsync, archiveRetryTaskAsync, taskRowsPerPageChange, }; const connector = connect(mapStateToProps, mapDispatchToProps); type ReduxProps = ConnectedProps; interface Props { queue: string; // name of the queue. totalTaskCount: number; // totoal number of scheduled tasks. } function RetryTasksTable(props: Props & ReduxProps) { const { pollInterval, listRetryTasksAsync, queue, pageSize } = props; const classes = useStyles(); const [page, setPage] = useState(0); const [selectedIds, setSelectedIds] = useState([]); const [activeTaskId, setActiveTaskId] = useState(""); const handlePageChange = ( event: React.MouseEvent | null, newPage: number ) => { setPage(newPage); }; const handleRowsPerPageChange = ( event: React.ChangeEvent ) => { props.taskRowsPerPageChange(parseInt(event.target.value, 10)); setPage(0); }; const handleSelectAllClick = (event: React.ChangeEvent) => { if (event.target.checked) { const newSelected = props.tasks.map((t) => t.id); setSelectedIds(newSelected); } else { setSelectedIds([]); } }; const handleRunAllClick = () => { props.runAllRetryTasksAsync(queue); }; const handleDeleteAllClick = () => { props.deleteAllRetryTasksAsync(queue); }; const handleArchiveAllClick = () => { props.archiveAllRetryTasksAsync(queue); }; const handleBatchRunClick = () => { props .batchRunRetryTasksAsync(queue, selectedIds) .then(() => setSelectedIds([])); }; const handleBatchDeleteClick = () => { props .batchDeleteRetryTasksAsync(queue, selectedIds) .then(() => setSelectedIds([])); }; const handleBatchArchiveClick = () => { props .batchArchiveRetryTasksAsync(queue, selectedIds) .then(() => setSelectedIds([])); }; const fetchData = useCallback(() => { const pageOpts = { page: page + 1, size: pageSize }; listRetryTasksAsync(queue, pageOpts); }, [page, pageSize, queue, listRetryTasksAsync]); usePolling(fetchData, pollInterval); if (props.error.length > 0) { return ( Error {props.error} ); } if (props.tasks.length === 0) { return ( Info No retry tasks at this time. ); } const columns: TableColumn[] = [ { key: "id", label: "ID", align: "left" }, { key: "type", label: "Type", align: "left" }, { key: "payload", label: "Payload", align: "left" }, { key: "retry_in", label: "Retry In", align: "left" }, { key: "last_error", label: "Last Error", align: "left" }, { key: "retried", label: "Retried", align: "right" }, { key: "max_retry", label: "Max Retry", align: "right" }, { key: "actions", label: "Actions", align: "center" }, ]; const rowCount = props.tasks.length; const numSelected = selectedIds.length; return (
0} iconButtonActions={[ { tooltip: "Delete", icon: , onClick: handleBatchDeleteClick, disabled: props.batchActionPending, }, { tooltip: "Archive", icon: , onClick: handleBatchArchiveClick, disabled: props.batchActionPending, }, { tooltip: "Run", icon: , onClick: handleBatchRunClick, disabled: props.batchActionPending, }, ]} menuItemActions={[ { label: "Delete All", onClick: handleDeleteAllClick, disabled: props.allActionPending, }, { label: "Archive All", onClick: handleArchiveAllClick, disabled: props.allActionPending, }, { label: "Run All", onClick: handleRunAllClick, disabled: props.allActionPending, }, ]} /> 0 && numSelected < rowCount} checked={rowCount > 0 && numSelected === rowCount} onChange={handleSelectAllClick} inputProps={{ "aria-label": "select all tasks shown in the table", }} /> {columns.map((col) => ( {col.label} ))} {props.tasks.map((task) => ( { if (checked) { setSelectedIds(selectedIds.concat(task.id)); } else { setSelectedIds(selectedIds.filter((id) => id !== task.id)); } }} onRunClick={() => { props.runRetryTaskAsync(task.queue, task.id); }} onDeleteClick={() => { props.deleteRetryTaskAsync(task.queue, task.id); }} onArchiveClick={() => { props.archiveRetryTaskAsync(task.queue, task.id); }} onActionCellEnter={() => setActiveTaskId(task.id)} onActionCellLeave={() => setActiveTaskId("")} showActions={activeTaskId === task.id} /> ))}
); } const useRowStyles = makeStyles((theme) => ({ root: { cursor: "pointer", "&:hover": { boxShadow: theme.shadows[2], }, "&:hover $copyButton": { display: "inline-block" }, "&:hover .MuiTableCell-root": { borderBottomColor: theme.palette.background.paper, }, }, actionCell: { width: "140px", }, actionButton: { marginLeft: 3, marginRight: 3, }, idCell: { width: "200px", }, copyButton: { display: "none" }, IdGroup: { display: "flex", alignItems: "center", } })); interface RowProps { task: TaskInfoExtended; isSelected: boolean; onSelectChange: (checked: boolean) => void; onDeleteClick: () => void; onRunClick: () => void; onArchiveClick: () => void; allActionPending: boolean; showActions: boolean; onActionCellEnter: () => void; onActionCellLeave: () => void; } function Row(props: RowProps) { const { task } = props; const classes = useRowStyles(); const history = useHistory(); return ( history.push(taskDetailsPath(task.queue, task.id))} > e.stopPropagation()}> ) => props.onSelectChange(event.target.checked) } checked={props.isSelected} />
{uuidPrefix(task.id)} { e.stopPropagation() navigator.clipboard.writeText(task.id) }} size="small" className={classes.copyButton} >
{task.type} {prettifyPayload(task.payload)} {durationBefore(task.next_process_at)} {task.error_message} {task.retried} {task.max_retry} e.stopPropagation()} > {props.showActions ? ( ) : ( )}
); } export default connector(RetryTasksTable);