import { useEffect, useState } from "react" import { app } from "./api" import { AppSection } from "./AppSection" import { ONE_SECOND } from "@typed-assistant/utils/durations" export const Stats = () => { const [counter, setCounter] = useState(0) const [error, setError] = useState(null) const [lastUpdated, setLastUpdated] = useState(null) const [stats, setStats] = useState>["data"]>(null) useEffect(() => { app.stats.get().then((stats) => { setLastUpdated(Date.now()) if (stats.error) { setError(stats.error.message) return } setStats(stats.data) const timeout = setTimeout(() => { setCounter((c) => c + 1) }, 10 * ONE_SECOND) return () => clearTimeout(timeout) }) }, [counter]) return ( {error ? ( <>Error: {error} ) : stats ? ( <>
Memory:{" "} {stats.memory_usage ? `${bytesToMegaBytes(stats.memory_usage ?? 0)} / ${bytesToMegaBytes(stats.memory_limit ?? 0)}MB` : "Loading..."}
CPU: {stats.cpu_percent ? `${stats.cpu_percent}%` : "Loading..."}
Max memory usage: {bytesToMegaBytes(stats.max_memory_usage)}MB. Last updated:{" "} {lastUpdated ? new Date(lastUpdated).toLocaleTimeString() : "Never"}
) : (
Loading...
)}
) } const ProgressBar = ({ value }: { value: number }) => { return (
) } const bytesToMegaBytes = (bytes: number) => Math.round((bytes / 1024 / 1024) * 100) / 100