All files / pages/Jobs JobLogPage.jsx

100% Statements 19/19
100% Branches 16/16
100% Functions 3/3
100% Lines 19/19

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                                            24x 24x   24x   24x 24x   24x       24x                         24x       24x     24x 2x 22x 11x 11x 2x   9x             24x                 3x             2x                      
import React from "react";
import { useLocation, useNavigate, useParams } from "react-router";
import { Button } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend } from "main/utils/useBackend";
 
/**
 * Shows the full log of a single job.
 *
 * The Jobs tables (admin-global and per-course) only receive a short tail
 * preview of each log from the backend (lib-jobs populates Job.log with the
 * last few lines only), so this page is where the complete log lives.
 *
 * It serves two routes and decides what to fetch from the route params:
 *  - /admin/jobs/logs/:id
 *      -> GET /api/jobs/logs/{id} (lib-jobs endpoint, ROLE_ADMIN only)
 *  - /instructor/courses/:courseId/jobs/:jobId/logs and
 *    /staff/courses/:courseId/jobs/:jobId/logs
 *      -> GET /api/jobs/course/logs?courseId=&jobId= (app endpoint, guarded by
 *         course manage permissions, so non-admin instructors/staff can use it)
 */
export default function JobLogPage() {
  const { id, courseId, jobId } = useParams();
  const navigate = useNavigate();
  // staff have their own address for this page, and go back to the staff view of the course
  const isStaffView = useLocation().pathname.startsWith("/staff/");
 
  const isCourseScoped = courseId !== undefined;
  const effectiveJobId = isCourseScoped ? jobId : id;
 
  const queryKey = isCourseScoped
    ? ["/api/jobs/course/logs", courseId, jobId]
    : [`/api/jobs/logs/${id}`];
 
  const axiosParameters = isCourseScoped
    ? {
        // Stryker disable next-line StringLiteral: axios default is GET
        method: "GET",
        url: "/api/jobs/course/logs",
        params: { courseId, jobId },
      }
    : {
        // Stryker disable next-line StringLiteral: axios default is GET
        method: "GET",
        url: `/api/jobs/logs/${id}`,
      };
 
  const backPath = isCourseScoped
    ? `/${isStaffView ? "staff" : "instructor"}/courses/${courseId}`
    : "/admin/jobs";
 
  const { data: log, isError, refetch } = useBackend(queryKey, axiosParameters);
 
  let content;
  if (isError) {
    content = <p data-testid="JobLogPage-error">Error loading log.</p>;
  } else if (log === undefined) {
    content = <p data-testid="JobLogPage-loading">Loading...</p>;
  } else if (log === "") {
    content = <p data-testid="JobLogPage-empty">No log lines yet.</p>;
  } else {
    content = (
      <pre data-testid="JobLogPage-log" style={{ whiteSpace: "pre-wrap" }}>
        {log}
      </pre>
    );
  }
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h2 data-testid="JobLogPage-heading">
          Job Log for Job {effectiveJobId}
        </h2>
        <div className="mb-3">
          <Button
            variant="secondary"
            onClick={() => navigate(backPath)}
            data-testid="JobLogPage-back"
          >
            Back
          </Button>
          <Button
            className="ms-2"
            onClick={() => refetch()}
            data-testid="JobLogPage-refresh"
          >
            Refresh
          </Button>
        </div>
        {content}
      </div>
    </BasicLayout>
  );
}