From 04607cb4d869e3aaa503097f1b7aecc5f1e0abf6 Mon Sep 17 00:00:00 2001 From: mmelko Date: Thu, 14 Sep 2023 15:38:00 +0200 Subject: [PATCH] feat(Runtime): refresh Threads and Metrics periodically --- .../hawtio/src/plugins/runtime/Metrics.tsx | 16 +++++++++++----- .../src/plugins/runtime/SysProps.test.tsx | 2 +- .../hawtio/src/plugins/runtime/SysProps.tsx | 3 ++- .../hawtio/src/plugins/runtime/Threads.tsx | 18 +++++++++++------- .../src/plugins/runtime/runtime-service.ts | 1 + packages/hawtio/src/plugins/runtime/types.ts | 6 +++++- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/hawtio/src/plugins/runtime/Metrics.tsx b/packages/hawtio/src/plugins/runtime/Metrics.tsx index 63542290..12b575bc 100644 --- a/packages/hawtio/src/plugins/runtime/Metrics.tsx +++ b/packages/hawtio/src/plugins/runtime/Metrics.tsx @@ -1,6 +1,6 @@ import { Card, CardBody, CardHeader, Grid, GridItem, Title } from '@patternfly/react-core' import React, { useEffect, useState } from 'react' -import { getMetrics } from '@hawtiosrc/plugins/runtime/runtime-service' +import { getMetrics, REFRESH_INTERVAL } from '@hawtiosrc/plugins/runtime/runtime-service' import { Metric } from '@hawtiosrc/plugins/runtime/types' import { ChartBullet } from '@patternfly/react-charts' @@ -8,10 +8,16 @@ export const Metrics: React.FunctionComponent = () => { const [metrics, setMetrics] = useState([]) useEffect(() => { - getMetrics().then(m => { - console.log('received metrics:', m) - setMetrics(m) - }) + let timeoutHandle: NodeJS.Timeout + const readMetrics = async () => { + const metrics = await getMetrics() + setMetrics(metrics) + + timeoutHandle = setTimeout(readMetrics, REFRESH_INTERVAL) + } + + readMetrics() + return () => timeoutHandle && clearTimeout(timeoutHandle) }, []) return ( diff --git a/packages/hawtio/src/plugins/runtime/SysProps.test.tsx b/packages/hawtio/src/plugins/runtime/SysProps.test.tsx index 4e2e1bb5..4483caea 100644 --- a/packages/hawtio/src/plugins/runtime/SysProps.test.tsx +++ b/packages/hawtio/src/plugins/runtime/SysProps.test.tsx @@ -1,6 +1,6 @@ import { render, screen, waitFor, within } from '@testing-library/react' import { SysProps } from '@hawtiosrc/plugins/runtime/SysProps' -import { SystemProperty } from '@hawtiosrc/plugins/runtime/runtime-service' +import { SystemProperty } from './types' import userEvent from '@testing-library/user-event' function getMockedProperties(): SystemProperty[] { diff --git a/packages/hawtio/src/plugins/runtime/SysProps.tsx b/packages/hawtio/src/plugins/runtime/SysProps.tsx index cda420dd..c73c456f 100644 --- a/packages/hawtio/src/plugins/runtime/SysProps.tsx +++ b/packages/hawtio/src/plugins/runtime/SysProps.tsx @@ -21,8 +21,9 @@ import { } from '@patternfly/react-core' import { TableComposable, Tbody, Td, Th, Thead, ThProps, Tr } from '@patternfly/react-table' import { SearchIcon } from '@patternfly/react-icons' -import { getSystemProperties, SystemProperty } from '@hawtiosrc/plugins/runtime/runtime-service' +import { getSystemProperties } from './runtime-service' import { objectSorter } from '@hawtiosrc/util/objects' +import { SystemProperty } from './types' export const SysProps: React.FunctionComponent = () => { const [properties, setProperties] = useState<{ key: string; value: string }[]>([]) diff --git a/packages/hawtio/src/plugins/runtime/Threads.tsx b/packages/hawtio/src/plugins/runtime/Threads.tsx index 58f302d8..2c358c86 100644 --- a/packages/hawtio/src/plugins/runtime/Threads.tsx +++ b/packages/hawtio/src/plugins/runtime/Threads.tsx @@ -31,6 +31,7 @@ import { dumpThreads, enableThreadContentionMonitoring, isThreadContentionMonitoringEnabled, + REFRESH_INTERVAL, } from '@hawtiosrc/plugins/runtime/runtime-service' import { objectSorter } from '@hawtiosrc/util/objects' import { Thread } from '@hawtiosrc/plugins/runtime/types' @@ -85,13 +86,16 @@ export const Threads: React.FunctionComponent = () => { const [threadConnectionMonitoring, setThreadConnectionMonitoring] = useState(false) useEffect(() => { - getThreads().then((props: Thread[]) => { - setThreads(props) - setFilteredThreads(props) - }) - isThreadContentionMonitoringEnabled().then(enabled => { - setThreadConnectionMonitoring(enabled) - }) + let timeoutHandle: NodeJS.Timeout + const readThreads = async () => { + const threads = await getThreads() + setThreads(threads) + setFilteredThreads(threads) + setThreadConnectionMonitoring(await isThreadContentionMonitoringEnabled()) + timeoutHandle = setTimeout(readThreads, REFRESH_INTERVAL) + } + readThreads() + return () => timeoutHandle && clearTimeout(timeoutHandle) }, []) const onDeleteFilter = (filter: string) => { diff --git a/packages/hawtio/src/plugins/runtime/runtime-service.ts b/packages/hawtio/src/plugins/runtime/runtime-service.ts index 10c75f69..3b9403c7 100644 --- a/packages/hawtio/src/plugins/runtime/runtime-service.ts +++ b/packages/hawtio/src/plugins/runtime/runtime-service.ts @@ -1,6 +1,7 @@ import { jolokiaService } from '@hawtiosrc/plugins/shared' import { Metric, SystemProperty, Thread } from './types' +export const REFRESH_INTERVAL = 5000 function convertMsToDaysHours(ms: number): string { const seconds = Math.floor(ms / 1000) const minutes = Math.floor(seconds / 60) diff --git a/packages/hawtio/src/plugins/runtime/types.ts b/packages/hawtio/src/plugins/runtime/types.ts index c41038bd..c6ba72e0 100644 --- a/packages/hawtio/src/plugins/runtime/types.ts +++ b/packages/hawtio/src/plugins/runtime/types.ts @@ -1,4 +1,8 @@ -export type SystemProperty = { key: string; value: string } +export type SystemProperty = { + key: string + value: string +} + export type Metric = { type: 'JVM' | 'System' name: string