Skip to content

Commit

Permalink
Optionally show & disable kill-run btn
Browse files Browse the repository at this point in the history
new features:
1. Disable kill-run button for finished runs
2. Hide kill-run button for users who don't own the run

Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Jan 8, 2024
1 parent 43a31c0 commit 2f683a1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/components/KillButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import { CircularProgress } from "@mui/material";

import { KillRun } from "../../lib/teuthologyAPI.d";
import { KillRunPayload } from "../../lib/teuthologyAPI.d";
import { useSession } from "../../lib/teuthologyAPI";
import Alert from "../Alert";


type KillButtonProps = {
mutation: UseMutationResult;
text: string;
payload: KillRun;
payload: KillRunPayload;
disabled?: boolean;
};


export default function KillButton(props: KillButtonProps) {
const mutation: UseMutationResult = props.mutation;

const sessionQuery = useSession();
const loggedUser = sessionQuery.data?.session.username;

if (loggedUser?.toLowerCase() != props.payload["--user"].toLowerCase()) {
// logged user and owner of the job should be equal (case insensitive)
return null
}

return (
<div>
Expand All @@ -26,7 +34,7 @@ export default function KillButton(props: KillButtonProps) {
color="error"
size="large"
onClick={() => mutation.mutate(props.payload)}
disabled={(mutation.isLoading)}
disabled={(props.disabled || mutation.isLoading)}
>
{props.text}
</Button>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/paddles.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Job = {
suite: string;
branch: string;
scheduled: string;
owner: string;
};

export type RunResults = {
Expand All @@ -39,6 +40,7 @@ export type Run = {
results: RunResults;
user: string;
machine_type: string;
status: string;
};

export type Node = {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/teuthologyAPI.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

export type KillRun = {
export type Session = {
session: {
id: int,
username: string
}
}

export type KillRunPayload = {
"--run": string,
"--owner": string,
"--machine-type": string,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/teuthologyAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import { useQuery, useMutation } from "@tanstack/react-query";
import type { UseQueryResult, UseMutationResult } from "@tanstack/react-query";
import { Cookies } from "react-cookie";
import { Session } from "./teuthologyAPI.d"

const TEUTHOLOGY_API_SERVER =
import.meta.env.VITE_TEUTHOLOGY_API || "";
Expand All @@ -25,9 +26,9 @@ function doLogout() {
window.location.href = url;
}

function useSession(): UseQueryResult {
function useSession(): UseQueryResult<Session> {
const url = getURL("/");
const query = useQuery({
const query = useQuery<Session, Error>({
queryKey: ['ping-api', { url }],
queryFn: () => (
axios.get(url, {
Expand Down
14 changes: 10 additions & 4 deletions src/pages/Run/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useRunKill } from "../../lib/teuthologyAPI";
import JobList from "../../components/JobList";
import Link from "../../components/Link";
import KillButton from "../../components/KillButton";
import { KillRun } from '../../lib/teuthologyAPI.d';
import { KillRunPayload } from '../../lib/teuthologyAPI.d';

const PREFIX = "index";

Expand Down Expand Up @@ -68,9 +68,10 @@ export default function Run() {
const date = query.data?.scheduled
? format(new Date(query.data.scheduled), "yyyy-MM-dd")
: null;
const killPayload: KillRun = {
const run_owner = data?.jobs[0].owner || "";
const killPayload: KillRunPayload = {
"--run": data?.name || "",
"--owner": data?.user || "",
"--owner": run_owner,
"--machine-type": data?.machine_type || "",
"--user": data?.user || "",
}
Expand All @@ -94,7 +95,12 @@ export default function Run() {
date
</FilterLink>
</div>
<KillButton mutation={killMutation} text="Kill run" payload={killPayload} />
<KillButton
mutation={killMutation}
text="Kill run"
payload={killPayload}
disabled={(data?.status.includes("finished"))}
/>
<ButtonGroup style={{ display: "flex", justifyContent: "center" }}>
<Button
onClick={() => {
Expand Down

0 comments on commit 2f683a1

Please sign in to comment.