From ea666ac9254c8ba4928258fd925943df48135ed6 Mon Sep 17 00:00:00 2001 From: kaganrua Date: Mon, 19 Aug 2024 21:30:56 -0400 Subject: [PATCH] refactoring done --- frontend/server/blockSerialization.js | 15 +- frontend/server/express.mjs | 18 +- frontend/src/App.jsx | 201 +++++++++--------- .../src/components/ui/AnvilLauncherStatus.jsx | 29 +++ .../ui/modal/AnvilConfigurationsModal.jsx | 107 +++------- .../components/ui/modal/KubecontextModal.jsx | 24 +-- .../src/components/ui/modal/StatusModal.jsx | 29 +++ 7 files changed, 199 insertions(+), 224 deletions(-) create mode 100644 frontend/src/components/ui/AnvilLauncherStatus.jsx create mode 100644 frontend/src/components/ui/modal/StatusModal.jsx diff --git a/frontend/server/blockSerialization.js b/frontend/server/blockSerialization.js index d697ed50..1f9d0cb3 100644 --- a/frontend/server/blockSerialization.js +++ b/frontend/server/blockSerialization.js @@ -57,20 +57,7 @@ export async function runTest(blockPath, blockKey) { throw new Error(`Could not find script for running tests: ${scriptPath}`); } - // return new Promise((resolve, reject) => { - // execFile( - // "python", - // [scriptPath, blockPath, blockKey], - // (error, stdout, stderr) => { - // if (error) { - // reject(error); - // } - // logger.debug(stdout); - // logger.error(stderr); - // resolve(); - // }, - // ); - // }); + return await runTestContainer(blockPath, blockKey) } diff --git a/frontend/server/express.mjs b/frontend/server/express.mjs index d40cb9c9..c341a980 100644 --- a/frontend/server/express.mjs +++ b/frontend/server/express.mjs @@ -17,11 +17,9 @@ let anvilProcess = null; function gracefullyStopAnvil() { if (anvilProcess !== null) { + console.log("KILLING ANVIL") anvilProcess.kill("SIGINT"); - const sleep = new Promise((resolve) => setTimeout(resolve, 5000)); - sleep().then((res) => { - console.log("schleepy shleep"); - }); + } } @@ -218,12 +216,7 @@ function startExpressServer() { if (electronApp.isPackaged) { agents = path.join(process.resourcesPath, "agents"); } - const scriptPath = path.join( - agents, - agentName, - "generate", - "computations.py", - ); + if (agentName === "gpt-4_python_compute") { try { const result = await computeAgent( @@ -369,7 +362,7 @@ function startExpressServer() { runAnvilPromise .then((response) => { - res.sendStatus(200); + res.status(200).send({success: response}); }) .catch((err) => { res @@ -395,7 +388,6 @@ function startExpressServer() { if (anvilProcess !== null) { anvilProcess.kill("SIGINT"); anvilProcess = null; - console.log("killed the process"); } const anvilDir = path.join(process.resourcesPath, "server2"); @@ -478,8 +470,6 @@ function startExpressServer() { reject(new Error(`Kubeservices not found: ${data.toString()}`)); } }); - - anvilProcess.on("close", (code) => {}); }); const runAnvilPromise = Promise.race([anvilTimeoutPromise, runAnvil]); diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index ead0b1b2..3b03d0fa 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -14,19 +14,20 @@ import SocketFetcher from "@/components/ui/SocketFetcher"; import "./styles/globals.scss"; import AnvilConfigurationsModal from "./components/ui/modal/AnvilConfigurationsModal"; -import ClosableModal from "./components/ui/modal/ClosableModal"; +import StatusModal from "./components/ui/modal/StatusModal"; import { useState, useEffect } from "react"; import axios from "axios"; import { useAtom } from "jotai"; -import { isPackaged } from "@/atoms/kubecontextAtom"; import { availableKubeContexts } from "@/atoms/kubecontextAtom"; -import { Loading } from "@carbon/react"; import { ping } from "./client/anvil"; import { activeConfigurationAtom } from "./atoms/anvilConfigurationsAtom"; +import AnvilLauncherStatus from "./components/ui/AnvilLauncherStatus"; + +const serverAddress = import.meta.env.VITE_EXPRESS; export default function App() { const [appIsPackaged, setIsPackaged] = useState(false); - const [availableKubeContextsAtom, setAvailableKubeContexts] = useAtom( + const [_, setAvailableKubeContexts] = useAtom( availableKubeContexts, ); const [configOpen, setConfigOpen] = useState(false); @@ -37,89 +38,100 @@ export default function App() { const [loading, setIsLoading] = useState(false); const [configuration] = useAtom(activeConfigurationAtom); + useEffect(() => { - const serverAddress = import.meta.env.VITE_EXPRESS; - const res = axios.get(`${serverAddress}/isPackaged`).then((response) => { - setIsPackaged(response.data); - if (response.data === true) { - ping(configuration) - .then((res) => { - if (!res) { - axios - .get(`${serverAddress}/get-kube-contexts`) - .then((res) => { - setAvailableKubeContexts(res.data); - - axios - .get(`${serverAddress}/get-anvil-config`) - .then((res) => { - const data = res.data; - if (data.has_config == true) { - const config = data.config; - const bucketPort = config.Local.BucketPort; - const driver = config.Local.Driver; - const serverPort = config.ServerPort; - const context = config.KubeContext; - if (config.IsLocal === true) { - setConfirmationIsOpen(true); - const configText = [ - "Are you sure you want to run anvil with the following configurations?", - "If you are using minikube driver, please make sure you've setted up your minikube cluster, and that it's running.", - `HOST: 127.0.0.1`, - `PORT: ${serverPort}`, - `Context: ${context}`, - `Driver: ${driver}`, - ]; - setConfirmationText(configText); - } - } else { - setConfigOpen(true); - setConfirmationIsOpen(false); - } - }) - .catch((err) => { - setErrText([ - "Your local anvil did not started as expected. Please select a config", - err.response?.data?.err, - err.response?.data?.kubeErr, - ]); - setConfigOpen(true); - }); - }) - .catch((err) => { - setErrText([ - "Cannot find kubectl or there's an error with kubectl command. Please try again or check if kubectl is in your path. You can ignore this message if you're using cloud anvil", - err.message, - ]); - setErrModalOpen(true); - }); + + async function initialLaunch() { + let isPackaged = false + + try { + const res = await axios.get(`${serverAddress}/isPackaged`) + isPackaged = res.data + setIsPackaged(res.data) + } catch(err) { + //do nothing + } + + if(isPackaged) { + const canPing = await ping(configuration) + if(!canPing) { + + try { + const kubeResponse = await axios.get(`${serverAddress}/get-kube-contexts`) + setAvailableKubeContexts(kubeResponse.data) + } catch(err) { + //once user set their cloud settings, this message won't display again. + setErrText([ + "Cannot find kubectl or there's an error with kubectl command. Please try again or check if kubectl is in your path or check if your Docker daemon is running. You can ignore this message if you'll use cloud anvil", + err.message, + ]); + setErrModalOpen(true); + } + + try { + const anvilRes = await axios.get(`${serverAddress}/get-anvil-config`) + const data = anvilRes.data + if(data.has_config) { + const config = data.config; + const bucketPort = config.Local.BucketPort; + const driver = config.Local.Driver; + const serverPort = config.ServerPort; + const context = config.KubeContext; + if (config.IsLocal === true) { + setConfirmationIsOpen(true); + const configText = [ + "Are you sure you want to run anvil locally with the following configurations?", + "If you are using minikube driver, please make sure you've setup up your minikube cluster, and that it's running.", + `HOST: 127.0.0.1`, + `PORT: ${serverPort}`, + `Context: ${context}`, + `Driver: ${driver}`, + ]; + setConfirmationText(configText); + } } else { - //do nothing, because you can ping - setConfigOpen(false); + setConfigOpen(true) + setConfirmationIsOpen(false) } - }) - .catch((err) => { - setConfigOpen(true); - }); - } else { - //do nothing, because either pip is the controller, or the user runs on dev, hence they're responsible for running anvil, setting kubectl context etc... + } catch(err) { + //once user set their cloud settings, this message won't display again. + setErrText([ + "Error occurred while trying to find your local Anvil configrations. Please try again to set your configurations and relaunch. If you want to use cloud settings, you can set your configurations to cloud instance, and ignore this message.", + err.response?.data?.err, + err.response?.data?.kubeErr, + ]); + setErrModalOpen(true) + } + + } } - }); + } + + initialLaunch() }, []); const confirmSettings = async () => { setIsLoading(true); - const serverAddress = import.meta.env.VITE_EXPRESS; try { const res = await axios.post(`${serverAddress}/launch-anvil-from-config`); setConfirmationIsOpen(false); setIsLoading(false); + setConfigOpen(false) } catch (err) { setErrText([err.message]); setErrModalOpen(true); setIsLoading(false); } }; + + const closeConfirmation = () => { + console.log("CHECK HERE") + if(!loading) { + setConfirmationIsOpen(false); + setConfigOpen(true); + } + } + return ( @@ -136,46 +148,23 @@ export default function App() { appIsPackaged={appIsPackaged} /> - {/* Confirmation */} - { - setConfirmationIsOpen(false); - setConfigOpen(true); - }} - open={confirmationOpen} - > -
- {confirmationText.map((error, i) => { - return

{error}

; - })} -
- -
- - {/* Error */} - { + { setErrModalOpen(false); setConfigOpen(true); }} - open={errModalOpen} - > -
- {errText.map((error, i) => { - return

{error}

; - })} -
-
+ errorOpen={errModalOpen} + errorMessage={errText} + /> + + diff --git a/frontend/src/components/ui/AnvilLauncherStatus.jsx b/frontend/src/components/ui/AnvilLauncherStatus.jsx new file mode 100644 index 00000000..57a312f8 --- /dev/null +++ b/frontend/src/components/ui/AnvilLauncherStatus.jsx @@ -0,0 +1,29 @@ +import StatusModal from "./modal/StatusModal"; + + +export default function AnvilLauncherStatus(props) { + + return (<> + + + + + ) + +} \ No newline at end of file diff --git a/frontend/src/components/ui/modal/AnvilConfigurationsModal.jsx b/frontend/src/components/ui/modal/AnvilConfigurationsModal.jsx index c0cba327..31fc3604 100644 --- a/frontend/src/components/ui/modal/AnvilConfigurationsModal.jsx +++ b/frontend/src/components/ui/modal/AnvilConfigurationsModal.jsx @@ -24,6 +24,9 @@ import { import { activeConfigurationAtom } from "@/atoms/anvilConfigurationsAtom"; import axios from "axios"; import { ping } from "@/client/anvil"; +import StatusModal from "./StatusModal"; +import AnvilLauncherStatus from "../AnvilLauncherStatus"; + const CONFIGURATION_TABLE_TITLE = "Anvil Configurations"; const NEW_CONFIGURATION_TITLE = "New Configuration"; @@ -172,65 +175,21 @@ export default function AnvilConfigurationsModal(props) { return ( <> - {props?.isInitial ? ( - - - Anvil Configurations + {...(props?.isInitial ? { open: props.open, onRequestClose: props.onRequestClose } : {})} - Set KubeContext - - - - {formOpen ? ( - - ) : ( - - )} - - -
- -
-
-
-
-
- ) : ( - Anvil Configurations + Set KubeContext @@ -259,37 +218,39 @@ export default function AnvilConfigurationsModal(props) { - )} + + {console.log("I AM TRYING TO CLOSE"); setConfirmationIsOpen(false) }} + confirmationOpen={confirmationOpen} + confirmationMessage={confirmationText} + loading={loading} + + errorHeader="Following errors occurred while launching anvil" + errorClose={() => setErrModalIsOpen(false)} + errorOpen={errModalOpen} + errorMessage={errMessage} + /> + {/* setConfirmationIsOpen(false)} + open={confirmationOpen} + message={confirmationText} + hasLoading={true} + loading={loading} /> + + - setConfirmationIsOpen(false)} - open={confirmationOpen} - > -
- {confirmationText.map((error, i) => { - return

{error}

; - })} -
- -
- setErrModalIsOpen(false)} + onRequestSubmit={() => setErrModalIsOpen(false)} open={errModalOpen} - > -
- {errMessage.map((error, i) => { - return

{error}

; - })} -
-
+ message={errMessage} /> */} + + ); } diff --git a/frontend/src/components/ui/modal/KubecontextModal.jsx b/frontend/src/components/ui/modal/KubecontextModal.jsx index 1d9787a7..d167c20c 100644 --- a/frontend/src/components/ui/modal/KubecontextModal.jsx +++ b/frontend/src/components/ui/modal/KubecontextModal.jsx @@ -10,31 +10,24 @@ import { TableRow, } from "@carbon/react"; import { useAtom } from "jotai"; -import { useImmerAtom } from "jotai-immer"; import { choosenKubeContexts, availableKubeContexts, - runningKubeContext, - kubeErrors, drivers, choosenDriver, } from "@/atoms/kubecontextAtom"; -import { useState, useEffect } from "react"; +import {useEffect } from "react"; import axios from "axios"; -export default function KubecontextModal({ - isPackaged, - initialLaunch, - closeFunc, -}) { +export default function KubecontextModal(props) { const [availableKubeContextsAtom, setAvailableKubeContexts] = useAtom( availableKubeContexts, ); - const [currentKubeContext, setCurrentKubeContext] = - useImmerAtom(choosenKubeContexts); + const [, setCurrentKubeContext] = + useAtom(choosenKubeContexts); const [availableDrivers] = useAtom(drivers); - const [userDriver, setChoosenDriver] = useAtom(choosenDriver); + const [, setChoosenDriver] = useAtom(choosenDriver); useEffect(() => { const serverAddress = import.meta.env.VITE_EXPRESS; @@ -49,9 +42,7 @@ export default function KubecontextModal({ const handleSelection = async (e) => { try { - setCurrentKubeContext((draft) => { - return e.selectedItem; - }); + setCurrentKubeContext(e.selectedItem) } catch (err) { console.log(err); } @@ -62,7 +53,7 @@ export default function KubecontextModal({ }; return ( - <> + @@ -88,6 +79,5 @@ export default function KubecontextModal({
- ); } diff --git a/frontend/src/components/ui/modal/StatusModal.jsx b/frontend/src/components/ui/modal/StatusModal.jsx new file mode 100644 index 00000000..1f153942 --- /dev/null +++ b/frontend/src/components/ui/modal/StatusModal.jsx @@ -0,0 +1,29 @@ +//This is a modal that's used for refactoring Confirmation and Error modals that we use throughout the modal while launching anvil locally + +import ClosableModal from "./ClosableModal"; +import { Loading } from "@carbon/react"; + + +export default function StatusModal(props) { + return ( +
+ {props?.message.map((error, i) => { + return

{error}

; + })} +
+ {props?.hasLoading === true ? : <>} +
) + +} + +