All files / pages AdminJobsPage.js

100% Statements 56/56
100% Branches 6/6
100% Functions 17/17
100% Lines 49/49

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                            1x       26x           26x         26x 1x 1x           26x           26x             26x 1x 1x         26x         26x           26x           26x             26x 2x 1x 1x   1x 1x             26x         26x             26x           26x             26x 2x 1x 1x   1x 1x         26x           26x             26x 1x 1x         26x 1x               26x             26x 1x 1x     26x                                                       26x                     26x   102x                         26x   26x         156x                            
import React from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import JobsTable from "main/components/Jobs/JobsTable";
import Accordion from "react-bootstrap/Accordion";
import TestJobForm from "main/components/Jobs/TestJobForm";
import UpdateCowHealthForm from "main/components/Jobs/UpdateCowHealthForm";
import MilkCowsJobForm from "main/components/Jobs/MilkCowsJobForm";
import InstructorReportForm from "main/components/Jobs/InstructorReportForm";
import InstructorReportSpecificCommonsForm from "main/components/Jobs/InstructorReportSpecificCommonsForm";
import { toast } from "react-toastify";
 
import {useBackend, useBackendMutation } from "main/utils/useBackend";
import SetCowHealthForm from "main/components/Jobs/SetCowHealthForm";
 
const AdminJobsPage = () => {
 
  // *** Test job ***
 
  const objectToAxiosParamsTestJob = (data) => ({
    url: `/api/jobs/launch/testjob?fail=${data.fail}&sleepMs=${data.sleepMs}`,
    method: "POST",
  });
 
  // Stryker disable all
  const testJobMutation = useBackendMutation(objectToAxiosParamsTestJob, {}, [
    "/api/jobs/all",
  ]);
  // Stryker restore all
 
  const submitTestJob = async (data) => {
    toast("Submitted job: Test Job");
    testJobMutation.mutate(data);
  };
 
  
  // *** SetCowHealth job ***
 
  const objectToAxiosParamsSetCowHealthJob = (data) => ({
    url: `/api/jobs/launch/setcowhealth?commonsID=${data.selectedCommons}&health=${data.healthValue}`,
    method: "POST",
  });
 
  // Stryker disable all
  const SetCowHealthMutation = useBackendMutation(
    objectToAxiosParamsSetCowHealthJob,
    {},
    ["/api/jobs/all"]
  );
  // Stryker restore all
 
  const submitSetCowHealthJob = async (data) => {
    toast(`Submitted Job: Set Cow Health (Commons: ${data.selectedCommonsName}, Health: ${data.healthValue})`);
    SetCowHealthMutation.mutate(data);
  };
 
  // *** UpdateCowHealth job ***
 
  const objectToAxiosParamsUpdateCowHealthJob = () => ({
    url: `/api/jobs/launch/updatecowhealth`,
    method: "POST",
  });
 
  const objectToAxiosParamsUpdateCowHealthJobSingle = (data) => ({
    url: `/api/jobs/launch/updatecowhealthsinglecommons?commonsId=${data.selectedCommons}`,
    method: "POST",
  });
 
  // Stryker disable all
  const UpdateCowHealthMutation = useBackendMutation(
    objectToAxiosParamsUpdateCowHealthJob,
    {},
    ["/api/jobs/all"]
  );
 
  const UpdateCowHealthSingleMutation = useBackendMutation(
    objectToAxiosParamsUpdateCowHealthJobSingle,
    {},
    ["/api/jobs/all"]
  );
  // Stryker restore all
 
  const submitUpdateCowHealthJob = async (data) => {
    if (data.selectedCommonsName === "All Commons") {
      toast("Submitted Job: Update Cow Health");
    UpdateCowHealthMutation.mutate();
    } else {
    toast(`Submitted Job: Update Cow Health (Commons: ${data.selectedCommonsName})`);
    UpdateCowHealthSingleMutation.mutate(data);
    }
  };
 
 
  // *** MilkTheCows job ***
 
  const objectToAxiosParamsMilkTheCowsJob = () => ({
    url: `/api/jobs/launch/milkthecowjob`,
    method: "POST",
  });
 
  const objectToAxiosParamsMilkTheCowsJobSingle = (data) => ({
    url: `/api/jobs/launch/milkthecowjobsinglecommons?commonsId=${data.selectedCommons}`,
    method: "POST",
  });
 
 
  // Stryker disable all
  const MilkTheCowsMutation = useBackendMutation(
    objectToAxiosParamsMilkTheCowsJob,
    {},
    ["/api/jobs/all"]
  );
 
  const MilkTheCowsSingleMutation = useBackendMutation(
    objectToAxiosParamsMilkTheCowsJobSingle,
    {},
    ["/api/jobs/all"]
  );
  // Stryker restore all
 
  const submitMilkTheCowsJob = async (data) => {
    if (data.selectedCommonsName === "All Commons") {
      toast("Submitted Job: Milk The Cows!");
      MilkTheCowsMutation.mutate();
    } else {
    toast(`Submitted Job: Milk The Cows! (Commons: ${data.selectedCommonsName})`);
    MilkTheCowsSingleMutation.mutate(data);
    }
  };
  // *** Instructor Report job ***
 
  const objectToAxiosParamsInstructorReportJob = () => ({
    url: `/api/jobs/launch/instructorreport`,
    method: "POST",
  });
 
  // Stryker disable all
  const InstructorReportMutation = useBackendMutation(
    objectToAxiosParamsInstructorReportJob,
    {},
    ["/api/jobs/all"]
  );
  // Stryker restore all
 
  const submitInstructorReportJob = async () => {
    toast("Submitted Job: Instructor Report");
    InstructorReportMutation.mutate();
  }
 
  // *** Instructor Report (Specific Commons) job ***
 
  const objectToAxiosParamsInstructorReportSpecificCommonsJob = (data) => {
    return ({
      url: `/api/jobs/launch/instructorreportsinglecommons?commonsId=${data.selectedCommons}`,
      method: "POST",
    });
 
  };
  
// Stryker disable all
const InstructorReportSpecificCommonsMutation = useBackendMutation(
  objectToAxiosParamsInstructorReportSpecificCommonsJob,
  {},
  ["/api/jobs/all"]
);
// Stryker restore all
 
const submitInstructorReportSpecificCommonsJob = async (data) => {
  toast("Submitted Job: Instructor Report (Specific Commons)");
  InstructorReportSpecificCommonsMutation.mutate(data);
}
 
const jobLaunchers = [
  {
    name: "Test Job",
    form: <TestJobForm submitAction={submitTestJob} />,
  },
  {
    name: "Set Cow Health for a Specific Commons",
    form: <SetCowHealthForm submitAction={submitSetCowHealthJob} />,
  },
  {
    name: "Update Cow Health",
    form: <UpdateCowHealthForm submitAction={submitUpdateCowHealthJob} />,
  },
  {
    name: "Milk The Cows",
    form: <MilkCowsJobForm submitAction={submitMilkTheCowsJob} />,
  },
  {
    name: "Instructor Report",
    form: <InstructorReportForm submitAction={submitInstructorReportJob} />
  },
  {
    name: "Instructor Report (for specific commons)",
    form: <InstructorReportSpecificCommonsForm submitAction={submitInstructorReportSpecificCommonsJob} />
  },
];
 
// Stryker disable all
const { data: jobs } = useBackend(
  ["/api/jobs/all"],
  {
    method: "GET",
    url: "/api/jobs/all",
  },
  { content: [] }
);
// Stryker restore all
 
const jobsForTable = 
  Array.isArray(jobs) ?
  jobs.map((job) => {
    return {
      id: job.id,
      createdAt: job.createdAt,
      updatedAt: job.updatedAt,
      status: job.status,
      log: job.log,
    };
  }) : 
  // Stryker disable next-line ArrayDeclaration : no need to test what happens if [] is replaced with ["Stryker was here"]
  [];
 
// display in reverse chronological order
// Stryker disable next-line all : no need to test what happens if jobsForTable is reversed
jobsForTable.reverse();
 
return (
  <BasicLayout>
    <h2 className="p-3">Launch Jobs</h2>
    <Accordion>
      {jobLaunchers.map((jobLauncher, index) => (
        <Accordion.Item eventKey={index} key={index}>
          <Accordion.Header>{jobLauncher.name}</Accordion.Header>
          <Accordion.Body>{jobLauncher.form}</Accordion.Body>
        </Accordion.Item>
      ))}
    </Accordion>
 
    <h2 className="p-3">Job Status</h2>
    <JobsTable jobs={jobsForTable}/>
  </BasicLayout>
);
};
 
export default AdminJobsPage;