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

[Enhancement]: Logs are not fetched in cloud #1706

Merged
merged 19 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
33324d5
added button to cancel operation and show logs
bekossy May 25, 2024
7ed8845
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jun 7, 2024
267ddc6
refactor(frontend): improve loading in playground
bekossy Jun 11, 2024
8811cbc
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jun 12, 2024
026ad12
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jun 14, 2024
dc05337
perf(frontend): code cleanup and review
bekossy Jun 14, 2024
cac79c9
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jun 22, 2024
8ced5ae
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jun 29, 2024
858d018
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jul 6, 2024
eb4ff70
minor fix
bekossy Jul 9, 2024
5ccddfa
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jul 9, 2024
e923b42
fix(frontend): fetch variant logs
bekossy Jul 9, 2024
55aaab8
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jul 10, 2024
e0ded6e
fix(frontend): fetch variant logs in oss
bekossy Jul 10, 2024
000fac8
improve ui for loading logs state
bekossy Jul 11, 2024
f3e70cb
fix(frontend): enable refetch parameters if promise is resolved
bekossy Jul 11, 2024
a2067c9
fix(frontend): minor improvements
bekossy Jul 11, 2024
6016e1b
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jul 15, 2024
6fc529a
Merge branch 'main' into cloud-issue-369/-enhancement-logs-are-not-fe…
bekossy Jul 16, 2024
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
112 changes: 83 additions & 29 deletions agenta-web/src/components/Playground/ViewNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useEffect, useRef} from "react"
import {Col, Row, Divider, Button, Tooltip, Spin, notification, Typography} from "antd"
import {Col, Row, Divider, Button, Tooltip, notification, Typography} from "antd"
import TestView from "./Views/TestView"
import ParametersView from "./Views/ParametersView"
import {useVariant} from "@/lib/hooks/useVariant"
Expand All @@ -8,7 +8,6 @@ import {useRouter} from "next/router"
import {useState} from "react"
import axios from "axios"
import {createUseStyles} from "react-jss"

import {fetchAppContainerURL, waitForAppToStart} from "@/services/api"
import {useAppsData} from "@/contexts/app.context"
import {isDemo} from "@/lib/helpers/utils"
Expand Down Expand Up @@ -70,17 +69,24 @@ const ViewNavigation: React.FC<Props> = ({
historyStatus,
setPromptOptParams,
setHistoryStatus,
getVariantLogs,
isLogsLoading,
variantErrorLogs,
setIsLogsLoading,
onClickShowLogs,
} = useVariant(appId, variant)

const [retrying, setRetrying] = useState(false)
const [isParamsCollapsed, setIsParamsCollapsed] = useState("1")
const [containerURI, setContainerURI] = useState("")
const [variantErrorLogs, setVariantErrorLogs] = useState("")
const [restarting, setRestarting] = useState<boolean>(false)
const {currentApp} = useAppsData()
const retriedOnce = useRef(false)
const netWorkError = (error as any)?.code === "ERR_NETWORK"
const [isDrawerOpen, setIsDrawerOpen] = useState(false)
const stopperRef = useRef<Function | null>(null)
const [isDelayed, setIsDelayed] = useState(false)
const hasError = netWorkError || (isDemo() ? netWorkError : isError)

let prevKey = ""
const showNotification = (config: Parameters<typeof notification.open>[0]) => {
Expand All @@ -90,41 +96,89 @@ const ViewNavigation: React.FC<Props> = ({
}

useEffect(() => {
if (netWorkError) {
if (hasError) {
retriedOnce.current = true
setRetrying(true)
waitForAppToStart({appId, variant, timeout: isDemo() ? 40000 : 6000})
.then(() => {
refetch()
const startApp = async () => {
const {stopper, promise} = await waitForAppToStart({
appId,
variant,
timeout: isDemo() ? 40000 : 10000,
})
.catch(() => {
showNotification({
type: "error",
message: "Variant unreachable",
description: `Unable to connect to the variant.`,
stopperRef.current = stopper

promise
.then(() => {
if (!onClickShowLogs.current) {
refetch()
}
})
})
.finally(() => {
setRetrying(false)
})
}
.catch(() => {
getVariantLogs()

if (isError) {
const getLogs = async () => {
const logs = await fetchVariantLogs(variant.variantId)
setVariantErrorLogs(logs)
showNotification({
type: "error",
message: "Variant unreachable",
description: `Unable to connect to the variant.`,
})
})
.finally(() => {
setRetrying(false)
setIsDelayed(false)
})
}
getLogs()
startApp()
}
}, [netWorkError, isError, variant.variantId])

useEffect(() => {
if (retrying) {
const timeout = setTimeout(
() => {
setIsDelayed(true)
},
isDemo() ? 15000 : 5000,
)
return () => clearTimeout(timeout)
}
}, [retrying])

const handleStopPolling = () => {
setIsLogsLoading(true)
if (stopperRef.current) {
onClickShowLogs.current = true
stopperRef.current()
getVariantLogs()
}
}

if (isLoading)
return <ResultComponent status="info" title="Loading variants..." spinner={true} />

if (isLogsLoading && isError)
return <ResultComponent status="info" title="Fetching variants logs..." spinner={true} />

if (retrying || (!retriedOnce.current && netWorkError)) {
return (
<ResultComponent
status={"info"}
title="Waiting for the variant to start"
spinner={retrying}
/>
<>
<div className="grid place-items-center">
<ResultComponent
status={"info"}
title="Waiting for the variant to start"
subtitle={isDelayed ? "This is taking longer than expected" : ""}
spinner={retrying}
/>
{isDelayed && (
<Button
loading={isLogsLoading}
onClick={() => handleStopPolling()}
type="primary"
>
Show Logs
</Button>
)}
</div>
</>
)
}

Expand Down Expand Up @@ -245,7 +299,7 @@ const ViewNavigation: React.FC<Props> = ({
}

return (
<Spin spinning={isLoading}>
<>
<Row gutter={[{xs: 8, sm: 16, md: 24, lg: 32}, 20]}>
<Col span={24}>
<ParametersView
Expand Down Expand Up @@ -286,7 +340,7 @@ const ViewNavigation: React.FC<Props> = ({
/>
</Col>
</Row>
</Spin>
</>
)
}

Expand Down
27 changes: 23 additions & 4 deletions agenta-web/src/lib/hooks/useVariant.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {useState, useEffect} from "react"
import {useState, useEffect, useRef} from "react"
import {Variant, Parameter} from "@/lib/Types"
import {getAllVariantParameters, updateInputParams} from "@/lib/helpers/variantHelper"
import {PERMISSION_ERR_MSG} from "../helpers/axiosConfig"
import {createNewVariant, updateVariantParams} from "@/services/playground/api"
import {createNewVariant, fetchVariantLogs, updateVariantParams} from "@/services/playground/api"

/**
* Hook for using the variant.
Expand All @@ -21,6 +21,21 @@ export function useVariant(appId: string, variant: Variant) {
const [error, setError] = useState<Error | null>(null)
const [isParamSaveLoading, setIsParamSaveLoading] = useState(false)
const [isChatVariant, setIsChatVariant] = useState<boolean | null>(null)
const [isLogsLoading, setIsLogsLoading] = useState(false)
const [variantErrorLogs, setVariantErrorLogs] = useState("")
const onClickShowLogs = useRef(false)

const getVariantLogs = async () => {
try {
setIsLogsLoading(true)
const logs = await fetchVariantLogs(variant.variantId)
setVariantErrorLogs(logs)
} catch (error) {
console.error(error)
} finally {
setIsLogsLoading(false)
}
}

const fetchParameters = async () => {
setIsLoading(true)
Expand All @@ -35,7 +50,7 @@ export function useVariant(appId: string, variant: Variant) {
setInputParams(inputs)
setURIPath(URIPath)
setIsChatVariant(isChatVariant)
setHistoryStatus({loading: false, error: true})
setHistoryStatus({loading: false, error: false})
} catch (error: any) {
if (error.message !== PERMISSION_ERR_MSG) {
console.log(error)
Expand All @@ -45,7 +60,6 @@ export function useVariant(appId: string, variant: Variant) {
}
} finally {
setIsLoading(false)
setHistoryStatus({loading: false, error: false})
}
}

Expand Down Expand Up @@ -112,6 +126,11 @@ export function useVariant(appId: string, variant: Variant) {
historyStatus,
setPromptOptParams,
setHistoryStatus,
getVariantLogs,
isLogsLoading,
variantErrorLogs,
setIsLogsLoading,
onClickShowLogs,
}
}

Expand Down
10 changes: 8 additions & 2 deletions agenta-web/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ export const waitForAppToStart = async ({
variant?: Variant
timeout?: number
interval?: number
}) => {
}): Promise<{
stopper: () => void
promise: Promise<void>
}> => {
const _variant = variant || (await fetchVariants(appId, true))[0]
if (_variant) {
const {stopper, promise} = shortPoll(
Expand All @@ -218,6 +221,9 @@ export const waitForAppToStart = async ({
).then(() => stopper()),
{delayMs: interval, timeoutMs: timeout},
)
await promise

return {stopper, promise}
} else {
return {stopper: () => {}, promise: Promise.reject(new Error("Variant not found"))}
}
}
3 changes: 2 additions & 1 deletion agenta-web/src/services/app-selector/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export const createAndStartTemplate = async ({

onStatusChange?.("starting_app", "", app?.data?.app_id)
try {
await waitForAppToStart({appId: app?.data?.app_id, timeout})
const {promise} = await waitForAppToStart({appId: app?.data?.app_id, timeout})
await promise
} catch (error: any) {
if (error.message === "timeout") {
onStatusChange?.("timeout", "", app?.data?.app_id)
Expand Down