From 1ba36656758815b9003815bac120794e0c8b3a9b Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Sat, 2 Dec 2023 02:23:19 +0530 Subject: [PATCH] Add component Alert and KillButton 1. Move Alert and kill-run Button from Run Page to `Alert` and `KillButton` component respectively 2. lib/teuthologyAPI: add useRunKill (which uses `useMutation` to send POST req at /kill to t-api) Signed-off-by: Vallari Agrawal --- src/components/Alert/index.tsx | 29 ++++++++++++++ src/components/KillButton/index.tsx | 50 ++++++++++++++++++++++++ src/lib/paddles.d.ts | 2 + src/lib/teuthologyAPI.ts | 21 ++++++++-- src/pages/Run/index.tsx | 60 ++--------------------------- 5 files changed, 103 insertions(+), 59 deletions(-) create mode 100644 src/components/Alert/index.tsx create mode 100644 src/components/KillButton/index.tsx diff --git a/src/components/Alert/index.tsx b/src/components/Alert/index.tsx new file mode 100644 index 0000000..bb8f86c --- /dev/null +++ b/src/components/Alert/index.tsx @@ -0,0 +1,29 @@ +import Snackbar from "@mui/material/Snackbar"; +import Alert from "@mui/material/Alert"; +import { useState } from "react"; + +type AlertProps = { + severity: "success" | "error", + message: string, +}; + +export default function AlertComponent(props: AlertProps) { + const [isOpen, setIsOpen] = useState(true); + const handleClose = ( + event?: React.SyntheticEvent | Event, + reason?: string + ) => { + if (reason === "clickaway") { + return; + } + setIsOpen(false); + }; + + return ( + + + {props.message} + + + ); +} \ No newline at end of file diff --git a/src/components/KillButton/index.tsx b/src/components/KillButton/index.tsx new file mode 100644 index 0000000..d7ea898 --- /dev/null +++ b/src/components/KillButton/index.tsx @@ -0,0 +1,50 @@ +import type { UseMutationResult } from "@tanstack/react-query"; +import Button from "@mui/material/Button"; +import Box from "@mui/material/Box"; +import { CircularProgress } from "@mui/material"; + +import { Run } from "../../lib/paddles.d" +import Alert from "../Alert"; + + +type KillButtonProps = { + mutation: UseMutationResult; + text: string; + runDetails?: Run; +}; + + +// TODO: make this generic for killing runs and job +export default function KillButton(props: KillButtonProps) { + const mutation: UseMutationResult = props.mutation; + + const killPayload = { + "--run": props.runDetails?.name, + "--owner": props.runDetails?.user, + "--machine-type": props.runDetails?.machine_type, + "--user": props.runDetails?.user, + } + + return ( +
+
+ + {(mutation.isLoading) ? ( + + + + ) : null} +
+ { (mutation.isError) ? : null } + { (mutation.isSuccess) ? : null } +
+ ); +}; diff --git a/src/lib/paddles.d.ts b/src/lib/paddles.d.ts index afad206..f180600 100644 --- a/src/lib/paddles.d.ts +++ b/src/lib/paddles.d.ts @@ -37,6 +37,8 @@ export type Run = { jobs: Job[]; scheduled: string; results: RunResults; + user: string; + machine_type: string; }; export type Node = { diff --git a/src/lib/teuthologyAPI.ts b/src/lib/teuthologyAPI.ts index 19b09ba..45a3a13 100644 --- a/src/lib/teuthologyAPI.ts +++ b/src/lib/teuthologyAPI.ts @@ -1,7 +1,7 @@ import axios from "axios"; -import { useQuery } from "@tanstack/react-query"; +import { useQuery, useMutation } from "@tanstack/react-query"; +import type { UseQueryResult, UseMutationResult } from "@tanstack/react-query"; import { Cookies } from "react-cookie"; -import type { UseQueryResult } from "@tanstack/react-query"; const TEUTHOLOGY_API_SERVER = import.meta.env.VITE_TEUTHOLOGY_API || ""; @@ -56,9 +56,24 @@ function useUserData(): Map { return new Map(); } +function useRunKill(): UseMutationResult { + const url = getURL("/kill?dry_run=false&logs=true"); + const mutation: UseMutationResult = useMutation({ + mutationKey: ['run-kill', { url }], + mutationFn: (payload) => ( + axios.post(url, payload, { + withCredentials: true + }) + ), + retry: 0, + }); + return mutation; +} + export { doLogin, doLogout, useSession, - useUserData + useUserData, + useRunKill, } diff --git a/src/pages/Run/index.tsx b/src/pages/Run/index.tsx index 104b27e..916e9f8 100644 --- a/src/pages/Run/index.tsx +++ b/src/pages/Run/index.tsx @@ -2,22 +2,19 @@ import { PropsWithChildren } from 'react' import { useQueryParams, StringParam, NumberParam } from "use-query-params"; import { styled } from "@mui/material/styles"; import { useParams } from "react-router-dom"; -import { useState } from "react"; import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; import ButtonGroup from "@mui/material/ButtonGroup"; -import Alert from "@mui/material/Alert"; -import Box from "@mui/material/Box"; -import CircularProgress from "@mui/material/CircularProgress"; -import Snackbar from "@mui/material/Snackbar"; import { format } from "date-fns"; import { Helmet } from "react-helmet"; import type { Run, RunParams } from "../../lib/paddles.d"; import { useRun } from "../../lib/paddles"; +import { useRunKill } from "../../lib/teuthologyAPI"; import JobList from "../../components/JobList"; import Link from "../../components/Link"; +import KillButton from "../../components/KillButton"; const PREFIX = "index"; @@ -53,29 +50,8 @@ export default function Run() { page: NumberParam, pageSize: NumberParam, }); - const [kill, setKill] = useState(false); - const [success, setSuccess] = useState(false); - const [error, setError] = useState(false); - const killRun = async () => { - setKill(true); - // Using a mock API endpoint for testing - const response = await fetch("https://reqres.in/api/users/2?delay=3"); - const status = response.status; - if (status === 200) setSuccess(true); - else setError(true); - setKill(false); - }; - const handleClose = ( - event?: React.SyntheticEvent | Event, - reason?: string - ) => { - if (reason === "clickaway") { - return; - } - setSuccess(false); - setError(false); - }; const { name } = useParams(); + const killMutation = useRunKill(); const query = useRun(name === undefined ? "" : name); if (query === null) return 404; if (query.isError) return null; @@ -111,35 +87,7 @@ export default function Run() { date -
- - {kill ? ( - - - - ) : null} -
- - - Run killed successfully - - - - - Unable to kill run - - +