From 18db6daf0a65e8ea86bdcf6fe8b8c301a4ffda9f Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Mon, 18 Jan 2021 10:49:00 -0800 Subject: [PATCH] feat(web): layout & auto-update debug page --- web/snowpack.config.js | 1 + web/src/App.jsx | 4 +- web/src/Debug.jsx | 97 +++++++++++++++++++++++++++++++++--- web/src/components/Table.jsx | 30 +++++------ 4 files changed, 108 insertions(+), 24 deletions(-) diff --git a/web/snowpack.config.js b/web/snowpack.config.js index 613c91f32..9f915e233 100644 --- a/web/snowpack.config.js +++ b/web/snowpack.config.js @@ -21,6 +21,7 @@ module.exports = { }, ], ], + routes: [{ match: 'routes', src: '.*', dest: '/index.html' }], packageOptions: { sourcemap: false, }, diff --git a/web/src/App.jsx b/web/src/App.jsx index 2c06f8db8..fffaf3375 100644 --- a/web/src/App.jsx +++ b/web/src/App.jsx @@ -24,9 +24,9 @@ export default function App() {
) : ( -
+
-
+
diff --git a/web/src/Debug.jsx b/web/src/Debug.jsx index e536fe463..f80fe461d 100644 --- a/web/src/Debug.jsx +++ b/web/src/Debug.jsx @@ -1,16 +1,97 @@ import { h } from 'preact'; -import { ApiHost } from './context'; -import { useContext, useEffect, useState } from 'preact/hooks'; +import Heading from './components/Heading'; +import Link from './components/Link'; +import { ApiHost, Config } from './context'; +import { Table, Tbody, Thead, Tr, Th, Td } from './components/Table'; +import { useCallback, useContext, useEffect, useState } from 'preact/hooks'; export default function Debug() { const apiHost = useContext(ApiHost); - const [config, setConfig] = useState({}); + const config = useContext(Config); + const [stats, setStats] = useState({}); + const [timeoutId, setTimeoutId] = useState(null); - useEffect(async () => { - const response = await fetch(`${apiHost}/api/stats`); - const data = response.ok ? await response.json() : {}; - setConfig(data); + const fetchStats = useCallback(async () => { + const statsResponse = await fetch(`${apiHost}/api/stats`); + const stats = statsResponse.ok ? await statsResponse.json() : {}; + setStats(stats); + setTimeoutId(setTimeout(fetchStats, 1000)); + }, [setStats]); + + useEffect(() => { + fetchStats(); }, []); - return
{JSON.stringify(config, null, 2)}
; + useEffect(() => { + return () => { + clearTimeout(timeoutId); + }; + }, [timeoutId]); + + const { detectors, detection_fps, service, ...cameras } = stats; + if (!service) { + return 'loading…'; + } + + const detectorNames = Object.keys(detectors); + const detectorDataKeys = Object.keys(detectors[detectorNames[0]]); + + const cameraNames = Object.keys(cameras); + const cameraDataKeys = Object.keys(cameras[cameraNames[0]]); + + return ( +
+ + Debug {service.version} + + + + + + {detectorDataKeys.map((name) => ( + + ))} + + + + {detectorNames.map((detector, i) => ( + + + {detectorDataKeys.map((name) => ( + + ))} + + ))} + +
detector{name.replace('_', ' ')}
{detector}{detectors[detector][name]}
+ + + + + + {cameraDataKeys.map((name) => ( + + ))} + + + + {cameraNames.map((camera, i) => ( + + + {cameraDataKeys.map((name) => ( + + ))} + + ))} + +
camera{name.replace('_', ' ')}
+ {camera} + {cameras[camera][name]}
+ + Config +
+        {JSON.stringify(config, null, 2)}
+      
+
+ ); } diff --git a/web/src/components/Table.jsx b/web/src/components/Table.jsx index 0088c2dfe..d3667a6a1 100644 --- a/web/src/components/Table.jsx +++ b/web/src/components/Table.jsx @@ -1,29 +1,31 @@ import { h } from 'preact'; -export function Table({ children }) { - return {children}
; +export function Table({ children, className }) { + return ( + {children}
+ ); } -export function Thead({ children }) { - return {children}; +export function Thead({ children, className }) { + return {children}; } -export function Tbody({ children }) { - return {children}; +export function Tbody({ children, className }) { + return {children}; } -export function Tfoot({ children }) { - return {children}; +export function Tfoot({ children, className }) { + return {children}; } -export function Tr({ children, index }) { - return {children}; +export function Tr({ children, className, index }) { + return {children}; } -export function Th({ children }) { - return {children}; +export function Th({ children, className }) { + return {children}; } -export function Td({ children }) { - return {children}; +export function Td({ children, className }) { + return {children}; }