Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add run action to model table #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions frontend/src/components/ExtractionLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Table, TableColumnsType } from "antd";
import { useContext, useEffect, useState } from "react";
import { StopOutlined } from "@ant-design/icons";
import { AppContext } from "../context/AppContext";
import { ModelContext } from "../context/ModelContext";

type ExtractionLogType = {
id: string;
Expand All @@ -27,40 +28,68 @@ const COLUMNS: TableColumnsType<ExtractionLogType> = [
},
];

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

let interval: number;

const ExtractionLog = () => {
const { token } = useContext(AppContext);
const { run, setRun } = useContext(ModelContext);
const [extractionLog, setExtractionLog] = useState<ExtractionLogType[]>([]);
const [loading, setLoading] = useState(false);

useEffect(() => {
fetch(`https://localhost:5001/extraction-log`, {
method: "GET",
headers: new Headers({
Authorization: `Bearer ${token.accessToken}`,
}),
})
.then((response) => {
if (response.ok) {
return response.json();
}
throw response;
})
.then((data) => {
setExtractionLog(data);
})
.catch((error) => {
console.error(error);
alert("Error deleting.");
})
.finally(() => {
setLoading(false);
if (run !== undefined) {
setExtractionLog((prev) => {
const i = prev.length - 1;
prev[i] = { ...prev[i], status: "Running" };
return prev;
});
const f = async () => {
await sleep(5000);
setRun(undefined);
};
f();
}
}, [run]);

useEffect(() => {
// Polling for extraction-log.
interval = setInterval(
() =>
fetch(`https://localhost:5001/extraction-log`, {
method: "GET",
headers: new Headers({
Authorization: `Bearer ${token.accessToken}`,
}),
})
.then((response) => {
if (response.ok) {
return response.json();
}
throw response;
})
.then((data) => {
const next = data.map((o: any) => ({ ...o, status: "Successful" }));
setExtractionLog(next);
})
.catch((error) => {
console.error(error);
}),
1000
);
}, []);

const dataSource = extractionLog;
if (run !== undefined) {
const i = dataSource.length - 1;
dataSource[i] = { ...dataSource[i], status: "Running" };
}

return (
<Table
loading={loading}
dataSource={extractionLog}
dataSource={dataSource}
columns={COLUMNS}
pagination={false}
size="small"
Expand Down
38 changes: 36 additions & 2 deletions frontend/src/components/ModelTable.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useContext, useState } from "react";
import { Table, TableColumnsType } from "antd";
import { DeleteOutlined, EyeOutlined } from "@ant-design/icons";
import {
DeleteOutlined,
EyeOutlined,
PlayCircleOutlined,
} from "@ant-design/icons";
import { Model, ModelContext } from "../context/ModelContext";
import { AppContext } from "../context/AppContext";
import ForgeModelViewModal from "./ForgeModelViewModal";

const ModelTable = () => {
const { token } = useContext(AppContext);
const { models, setModels } = useContext(ModelContext);
const { models, setModels, setRun } = useContext(ModelContext);
const [viewModel, setViewModel] = useState<Model>();
const [loading, setLoading] = useState(false);

Expand Down Expand Up @@ -47,6 +51,36 @@ const ModelTable = () => {
}}
/>
<span style={{ padding: "0 2px" }} />
<PlayCircleOutlined
alt="Run"
onClick={() => {
setLoading(true);
fetch(
`https://localhost:5001/run?operation=${record.type}&modelId=${record.id}`,
{
method: "POST",
headers: new Headers({
Authorization: `Bearer ${token.accessToken}`,
}),
}
)
.then((response) => {
setRun(record.id);
if (response.ok) {
return response;
}
throw response;
})
.catch((error) => {
console.error(error);
alert("Error with run request.");
})
.finally(() => {
setLoading(false);
});
}}
/>
<span style={{ padding: "0 2px" }} />
<DeleteOutlined
alt="Delete Model"
onClick={() => {
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/context/ModelContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Context = {
setModelToAdd: Dispatch<SetStateAction<Model | undefined>>;
derivatives: Map<string, string>;
setDerivatives: Dispatch<SetStateAction<Map<string, string>>>;
run: string | undefined;
setRun: Dispatch<SetStateAction<string | undefined>>;
};

export const ModelContext = createContext<Context>({
Expand All @@ -51,6 +53,8 @@ export const ModelContext = createContext<Context>({
setModelToAdd: () => {},
derivatives: new Map(),
setDerivatives: () => {},
run: undefined,
setRun: () => {},
});

export const ModelContextProvider = ({ children }: PropsWithChildren) => {
Expand All @@ -60,6 +64,7 @@ export const ModelContextProvider = ({ children }: PropsWithChildren) => {
const [derivatives, setDerivatives] = useState<Map<string, string>>(
new Map()
);
const [run, setRun] = useState<string>();

useEffect(() => {
fetch("https://localhost:5001/models", {
Expand Down Expand Up @@ -108,6 +113,8 @@ export const ModelContextProvider = ({ children }: PropsWithChildren) => {
setModelToAdd,
derivatives,
setDerivatives,
run,
setRun,
}}
>
{children}
Expand Down