All files / components/Jobs JobsTable.jsx

100% Statements 18/18
100% Branches 7/7
100% Functions 11/11
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                5x                             253x         253x 2x 2x       253x         253x 2x     253x                               23x         23x         23x                     34x       2x                     34x                                       253x   253x    
import React from "react";
import OurTable from "main/components/OurTable";
import { formatTime } from "main/utils/dateUtils";
import { Button } from "react-bootstrap";
import { Link } from "react-router";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
 
const CANCELLABLE_STATUSES = ["queued", "running"];
 
/**
 * @param jobs the jobs to display
 * @param onCancelled called after a cancellation request succeeds
 * @param fullLogLink optional; a function (job) => path. When provided, every
 *   row's Log cell gets a "View full log" link below the preview. The preview
 *   in job.log is only the last few lines (lib-jobs caps it server-side), so
 *   the link is shown for every row, not just visibly long ones.
 */
export default function JobsTable({
  jobs,
  onCancelled = () => {},
  fullLogLink,
}) {
  const cellToAxiosParamsCancel = (cell) => ({
    url: `/api/jobs/${cell.row.original.id}/cancel`,
    method: "POST",
  });
 
  const cancelSuccess = () => {
    toast("Cancellation requested.");
    onCancelled();
  };
 
  // Stryker disable all : hard to test for query caching
  const cancelMutation = useBackendMutation(cellToAxiosParamsCancel, {
    onSuccess: cancelSuccess,
  });
  // Stryker restore all
 
  const cancelCallback = (cell) => {
    cancelMutation.mutate(cell);
  };
 
  const columns = [
    {
      header: "id",
      accessorKey: "id",
    },
    {
      header: "Job Name",
      accessorKey: "jobName",
    },
    {
      header: "User Email",
      accessorKey: "createdByEmail",
    },
    {
      header: "Course Id",
      accessorFn: (row) =>
        row.scopeType === "course" ? String(row.scopeId) : "",
      id: "courseId",
    },
    {
      header: "Created",
      accessorFn: (row) => formatTime(row.createdAt),
      id: "createdAt",
    },
    {
      header: "Updated",
      accessorFn: (row) => formatTime(row.updatedAt),
      id: "updatedAt",
    },
    {
      header: "Status",
      accessorKey: "status",
    },
    {
      header: "Cancel",
      id: "cancel",
      cell: ({ cell }) =>
        CANCELLABLE_STATUSES.includes(cell.row.original.status) ? (
          <Button
            variant="danger"
            size="sm"
            onClick={() => cancelCallback(cell)}
            data-testid={`JobsTable-cell-row-${cell.row.index}-col-cancel-button`}
          >
            Cancel
          </Button>
        ) : null,
    },
    {
      header: "Log",
      accessorKey: "log",
      cell: ({ cell }) => (
        <>
          <div
            style={{ maxWidth: 450, maxHeight: 100, overflowY: "auto" }}
            data-testid={`JobsTable-cell-row-${cell.row.index}-col-${cell.column.id}-div`}
          >
            <pre style={{ whiteSpace: "pre-wrap" }}>{cell.getValue()}</pre>
          </div>
          {fullLogLink && (
            <Link
              to={fullLogLink(cell.row.original)}
              data-testid={`JobsTable-cell-row-${cell.row.index}-col-${cell.column.id}-full-link`}
            >
              View full log
            </Link>
          )}
        </>
      ),
    },
  ];
 
  const testid = "JobsTable";
 
  return <OurTable data={jobs} columns={columns} testid={testid} />;
}