From f30b55a9437eb50f9704313004c3d773c342f607 Mon Sep 17 00:00:00 2001 From: Kamoltat Sirivadhna Date: Tue, 30 Jan 2024 14:50:56 -0500 Subject: [PATCH] src/pages/Schedule: Connect pulpito-ng with teuthology-api for scheduling suites Use axios post request to for run, dry-run and force-priority Signed-off-by: Kamoltat Sirivadhna --- src/lib/teuthologyAPI.ts | 36 ++++++++++++++++++++++++++++++------ src/pages/Schedule/index.jsx | 29 +++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/lib/teuthologyAPI.ts b/src/lib/teuthologyAPI.ts index 19b09ba..528680e 100644 --- a/src/lib/teuthologyAPI.ts +++ b/src/lib/teuthologyAPI.ts @@ -3,28 +3,51 @@ import { useQuery } from "@tanstack/react-query"; import { Cookies } from "react-cookie"; import type { UseQueryResult } from "@tanstack/react-query"; -const TEUTHOLOGY_API_SERVER = +const TEUTHOLOGY_API_SERVER = import.meta.env.VITE_TEUTHOLOGY_API || ""; const GH_USER_COOKIE = "GH_USER"; -function getURL(relativeURL: URL|string): string { - if ( ! TEUTHOLOGY_API_SERVER ) return ""; +function getURL(relativeURL: URL | string): string { + if (!TEUTHOLOGY_API_SERVER) return ""; return new URL(relativeURL, TEUTHOLOGY_API_SERVER).toString(); } function doLogin() { const url = getURL("/login/"); - if ( url ) window.location.href = url; + if (url) window.location.href = url; } function doLogout() { const cookies = new Cookies(); cookies.remove(GH_USER_COOKIE); - + const url = getURL("/logout/"); window.location.href = url; } +function doSchedule(commandValue: any, dryRun = false) { + console.log("doSchedule"); + console.log(commandValue); + let url; + if (dryRun) { + url = getURL("/suite?dry_run=true"); + } else { + url = getURL("/suite?dry_run=false"); + } + if (commandValue['--user'] != useUserData().get("username")) { + console.log("Error: --user doesn't match username of current logged in account"); + return false; + } + axios.post(url, commandValue, { + withCredentials: true, + headers: { "Content-Type": "application/json" }, + }).then((resp) => { + console.log(resp); + }, (error) => { + console.log(error); + }); +} + function useSession(): UseQueryResult { const url = getURL("/"); const query = useQuery({ @@ -59,6 +82,7 @@ function useUserData(): Map { export { doLogin, doLogout, + doSchedule, useSession, - useUserData + useUserData, } diff --git a/src/pages/Schedule/index.jsx b/src/pages/Schedule/index.jsx index a237f0b..e17bc6e 100644 --- a/src/pages/Schedule/index.jsx +++ b/src/pages/Schedule/index.jsx @@ -21,6 +21,7 @@ import MenuItem from '@mui/material/MenuItem'; import Checkbox from '@mui/material/Checkbox'; import Tooltip from '@mui/material/Tooltip'; import InfoIcon from '@mui/icons-material/Info'; +import { useUserData, doSchedule } from '../../lib/teuthologyAPI'; export default function Schedule() { const keyOptions = @@ -83,20 +84,44 @@ export default function Schedule() { const [rowData, setRowData] = useLocalStorage("rowData", []); const [rowIndex, setRowIndex] = useLocalStorage("rowIndex", -1); const [commandBarValue, setCommandBarValue] = useState([]); + const userData = useUserData(); + let commandValue = {}; useEffect(() => { setCommandBarValue(rowData); }, [rowData]) + function getCommandValue() { + let retCommandValue = {}; + commandBarValue.map((data) => { + if (data.checked) { + retCommandValue[data.key] = data.value; + } + }) + let username = userData.get("username"); + if (!username) { + console.log("User is not logged in"); + return {}; + } else { + retCommandValue['--user'] = userData.get("username"); + } + return retCommandValue; + } + const handleRun = () => { - return false; + let commandValue = getCommandValue(); + doSchedule(commandValue); }; const handleDryRun = () => { - return false; + let commandValue = getCommandValue(); + doSchedule(commandValue, true); }; const handleForcePriority = () => { + let commandValue = getCommandValue(); + commandValue['--force-priority'] = true; + doSchedule(commandValue); return false; };