All files / components/Jobs JobLogTail.jsx

100% Statements 12/12
100% Branches 11/11
100% Functions 2/2
100% Lines 12/12

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                                    53x 53x     53x 48x       53x 1x 52x 32x 20x 7x   13x     53x                                                                  
import React, { useEffect, useRef } from "react";
import { Badge } from "react-bootstrap";
import { useJobLogTail } from "main/utils/useJobLogTail";
import {
  JOB_LOG_POLL_INTERVAL_SECONDS,
  isJobFinished,
} from "main/utils/jobLogUtils";
 
/**
 * Shows the log of a job of a course as it is written, like `tail -f`: new
 * lines are added at the bottom every few seconds, and the view scrolls to
 * follow them, until the job is over. See useJobLogTail.
 */
export default function JobLogTail({
  courseId,
  jobId,
  testIdPrefix = "JobLogTail",
}) {
  const { lines, status, error } = useJobLogTail(courseId, jobId);
  const logRef = useRef(null);
 
  // follow the end of the log as it grows
  useEffect(() => {
    logRef.current.scrollTop = logRef.current.scrollHeight;
  }, [lines]);
 
  let statusText;
  if (error) {
    statusText = "The log could not be loaded.";
  } else if (status === null) {
    statusText = "Loading the log...";
  } else if (isJobFinished(status)) {
    statusText = "The job is over, so the log will not change any more.";
  } else {
    statusText = `Checking for new lines every ${JOB_LOG_POLL_INTERVAL_SECONDS} seconds.`;
  }
 
  return (
    <div data-testid={testIdPrefix}>
      <p data-testid={`${testIdPrefix}-status`}>
        Job {jobId}
        {status !== null && (
          <>
            {" "}
            <Badge bg="secondary" data-testid={`${testIdPrefix}-status-badge`}>
              {status}
            </Badge>
          </>
        )}
        {" — "}
        <span data-testid={`${testIdPrefix}-status-text`}>{statusText}</span>
      </p>
      <pre
        ref={logRef}
        data-testid={`${testIdPrefix}-log`}
        style={{
          backgroundColor: "black",
          color: "white",
          fontFamily: "monospace",
          maxHeight: "60vh",
          overflowY: "auto",
          whiteSpace: "pre-wrap",
          padding: "0.75rem",
        }}
      >
        {lines.length === 0 ? "No log lines yet." : lines.join("\n")}
      </pre>
    </div>
  );
}