Skip to content

Commit

Permalink
Merge pull request #1611 from Agenta-AI/improvement-playground-error-…
Browse files Browse the repository at this point in the history
…message

[Enhancement]: Improve playground error message
  • Loading branch information
aakrem authored May 8, 2024
2 parents de74902 + ebea11d commit f115323
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
15 changes: 15 additions & 0 deletions agenta-backend/agenta_backend/routers/variants_router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import inspect
import logging
from typing import Any, Optional, Union, List

Expand All @@ -24,11 +25,13 @@
Image_ as Image,
AppVariantResponse_ as AppVariantResponse,
)
from agenta_backend.cloud.services import logs_manager
else:
from agenta_backend.models.api.api_models import (
Image,
AppVariantResponse,
)
from agenta_backend.services import logs_manager

from agenta_backend.models.api.api_models import (
URI,
Expand Down Expand Up @@ -312,6 +315,18 @@ async def start_variant(
return url


@router.get("/{variant_id}/logs/", operation_id="retrieve_variant_logs")
async def retrieve_variant_logs(variant_id: str, request: Request):
try:
app_variant = await db_manager.fetch_app_variant_by_id(variant_id)
deployment = await db_manager.get_deployment_by_appid(str(app_variant.app.id))
logs_result = await logs_manager.retrieve_logs(deployment.container_id)
return logs_result
except Exception as exc:
logger.exception(f"An error occurred: {str(exc)}")
raise HTTPException(500, {"message": str(exc)})


@router.get(
"/{variant_id}/",
operation_id="get_variant",
Expand Down
18 changes: 18 additions & 0 deletions agenta-backend/agenta_backend/services/logs_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import aiodocker


async def retrieve_logs(container_id: str) -> str:
"""
Retrieves and returns the last 10 lines of logs (both stdout and stderr)
for a specified Docker container.
Args:
container_id (str): The docker container identifier
Returns:
the last 10 lines of logs
"""

async with aiodocker.Docker() as client:
container = await client.containers.get(container_id)
logs = await container.log(stdout=True, stderr=True)
outputs = logs[::-1][:10]
return "".join(outputs)
59 changes: 39 additions & 20 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} from "antd"
import {Col, Row, Divider, Button, Tooltip, Spin, notification, Typography} from "antd"
import TestView from "./Views/TestView"
import ParametersView from "./Views/ParametersView"
import {useVariant} from "@/lib/hooks/useVariant"
Expand All @@ -9,6 +9,7 @@ import {useState} from "react"
import axios from "axios"
import {createUseStyles} from "react-jss"
import {
fetchVariantLogs,
getAppContainerURL,
removeVariant,
restartAppVariantContainer,
Expand All @@ -18,6 +19,8 @@ import {useAppsData} from "@/contexts/app.context"
import {isDemo} from "@/lib/helpers/utils"
import ResultComponent from "../ResultComponent/ResultComponent"

const {Text} = Typography

interface Props {
variant: Variant
handlePersistVariant: (variantName: string) => void
Expand All @@ -36,6 +39,9 @@ const useStyles = createUseStyles({
restartBtnMargin: {
marginRight: "10px",
},
errorLogs: {
whiteSpace: "pre-wrap",
},
})

const ViewNavigation: React.FC<Props> = ({
Expand Down Expand Up @@ -69,6 +75,7 @@ const ViewNavigation: React.FC<Props> = ({
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)
Expand Down Expand Up @@ -101,7 +108,15 @@ const ViewNavigation: React.FC<Props> = ({
setRetrying(false)
})
}
}, [netWorkError])

if (isError) {
const getLogs = async () => {
const logs = await fetchVariantLogs(variant.variantId)
setVariantErrorLogs(logs)
}
getLogs()
}
}, [netWorkError, isError, variant.variantId])

if (retrying || (!retriedOnce.current && netWorkError)) {
return (
Expand All @@ -115,7 +130,8 @@ const ViewNavigation: React.FC<Props> = ({

if (isError) {
let variantDesignator = variant.templateVariantName
let imageName = `agentaai/${(currentApp?.app_name || "").toLowerCase()}_`
let appName = currentApp?.app_name || ""
let imageName = `agentaai/${appName.toLowerCase()}_`

if (!variantDesignator || variantDesignator === "") {
variantDesignator = variant.variantName
Expand Down Expand Up @@ -165,31 +181,34 @@ const ViewNavigation: React.FC<Props> = ({
<p>
Error connecting to the variant {variant.variantName}.{" "}
{(axios.isAxiosError(error) && error.response?.status === 404 && (
<span>Container is not running.</span>
<span>
Container is not running. <b>See logs below:</b>
</span>
)) || <span>{error.message}</span>}
</p>
<p>To debug this issue, please follow the steps below:</p>
<ul>
<li>
Verify whether the API is up by checking if {apiAddress} is
accessible.
</li>
<li>
Check if the Docker container for the variant {variantDesignator} is
running. The image should be called {imageName}.
</li>
<div>
<Text code className={classes.errorLogs}>
{variantErrorLogs}
</Text>
</div>
</ul>
<p>
{" "}
In case the docker container is not running. Please check the logs from
docker to understand the issue. Most of the time it is a missing
requirements. Also, please attempt restarting it (using cli or docker
desktop)
Verify API accessibility at{" "}
<a href={apiAddress} target="_blank">
{apiAddress}
</a>
</p>
<p>
{" "}
If the issue persists please file an issue in github here:
https://github.com/Agenta-AI/agenta/issues/new?title=Issue%20in%20ViewNavigation.tsx
If the issue persists please file an issue in github
<a
href="https://github.com/Agenta-AI/agenta/issues/new?title=Issue%20in%20ViewNavigation.tsx"
target="_blank"
>
{" "}
here
</a>
</p>

<Button
Expand Down
7 changes: 7 additions & 0 deletions agenta-web/src/lib/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export async function fetchVariants(
return []
}

export const fetchVariantLogs = async (variantId: string, ignoreAxiosError: boolean = false) => {
const response = await axios.get(`${getAgentaApiUrl()}/api/variants/${variantId}/logs`, {
_ignoreError: ignoreAxiosError,
} as any)
return response.data
}

export function restartAppVariantContainer(variantId: string) {
return axios.post(`${getAgentaApiUrl()}/api/containers/restart_container/`, {
variant_id: variantId,
Expand Down

0 comments on commit f115323

Please sign in to comment.