import { useFrigateStats } from "@/api/ws"; import { StatusBarMessagesContext, StatusMessage, } from "@/context/statusbar-provider"; import useStats from "@/hooks/use-stats"; import { FrigateStats } from "@/types/stats"; import { useContext, useEffect, useMemo } from "react"; import { FaCheck } from "react-icons/fa"; import { IoIosWarning } from "react-icons/io"; import { MdCircle } from "react-icons/md"; import { Link } from "react-router-dom"; import useSWR from "swr"; export default function Statusbar() { const { data: initialStats } = useSWR("stats", { revalidateOnFocus: false, }); const { payload: latestStats } = useFrigateStats(); const { messages, addMessage, clearMessages } = useContext( StatusBarMessagesContext, )!; const stats = useMemo(() => { if (latestStats) { return latestStats; } return initialStats; }, [initialStats, latestStats]); const cpuPercent = useMemo(() => { const systemCpu = stats?.cpu_usages["frigate.full_system"]?.cpu; if (!systemCpu || systemCpu == "0.0") { return null; } return parseInt(systemCpu); }, [stats]); const { potentialProblems } = useStats(stats); useEffect(() => { clearMessages("stats"); potentialProblems.forEach((problem) => { addMessage( "stats", problem.text, problem.color, undefined, problem.relevantLink, ); }); }, [potentialProblems, addMessage, clearMessages]); return (
{cpuPercent && (
CPU {cpuPercent}%
)} {Object.entries(stats?.gpu_usages || {}).map(([name, stats]) => { if (name == "error-gpu") { return; } let gpuTitle; switch (name) { case "amd-vaapi": gpuTitle = "AMD GPU"; break; case "intel-vaapi": case "intel-qsv": gpuTitle = "Intel GPU"; break; default: gpuTitle = name; break; } const gpu = parseInt(stats.gpu); return ( {" "}
{gpuTitle} {gpu}%
); })}
{Object.entries(messages).length === 0 ? (
System is healthy
) : ( Object.entries(messages).map(([key, messageArray]) => (
{messageArray.map(({ id, text, color, link }: StatusMessage) => { const message = (
{text}
); if (link) { return ( {message} ); } else { return message; } })}
)) )}
); }