Skip to content

Commit

Permalink
refactor(runtime): make class from runtime-service
Browse files Browse the repository at this point in the history
refactor threads and metrics to use jolokia scheduler instead of custom timeout
use Record instead of Map in Metrics
  • Loading branch information
mmelko authored and tadayosi committed Sep 20, 2023
1 parent 609cc83 commit 47a68e1
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 148 deletions.
36 changes: 22 additions & 14 deletions packages/hawtio/src/plugins/runtime/Metrics.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { Card, CardBody, CardHeader, Grid, GridItem, Title } from '@patternfly/react-core'
import React, { useEffect, useState } from 'react'
import { loadMetrics, REFRESH_INTERVAL } from '@hawtiosrc/plugins/runtime/runtime-service'
import { Metric } from '@hawtiosrc/plugins/runtime/types'
import { runtimeService } from './runtime-service'
import { Metric } from './types'
import { ChartBullet } from '@patternfly/react-charts'

export const Metrics: React.FunctionComponent = () => {
const [metrics, setMetrics] = useState<Metric[]>([])
const [metrics, setMetrics] = useState<Record<string, Metric>>({})

useEffect(() => {
let timeoutHandle: NodeJS.Timeout
const readMetrics = async () => {
const metrics = await loadMetrics()
setMetrics(metrics)
const registerMetricsRequests = () => {
let metricsRecord: Record<string, Metric> = {}
runtimeService.registerMetrics(metric => {
metricsRecord = { ...metricsRecord, [metric.name]: metric }
setMetrics(metricsRecord)
})
}

timeoutHandle = setTimeout(readMetrics, REFRESH_INTERVAL)
const readMetrics = async () => {
const metricsList = await runtimeService.loadMetrics()
let metricsRecord: Record<string, Metric> = {}
metricsList.forEach(metric => (metricsRecord = { ...metricsRecord, [metric.name]: metric }))
setMetrics(metricsRecord)
}

readMetrics()
return () => timeoutHandle && clearTimeout(timeoutHandle)
registerMetricsRequests()
return () => runtimeService.unregisterAll()
}, [])

return (
Expand All @@ -28,7 +36,7 @@ export const Metrics: React.FunctionComponent = () => {
<Title headingLevel='h2'>System</Title>
</CardHeader>
<CardBody>
{metrics
{Object.values(metrics)
.filter(m => m.type === 'System')
.map((metric, index) => {
return (
Expand Down Expand Up @@ -63,15 +71,15 @@ export const Metrics: React.FunctionComponent = () => {
</CardHeader>

<CardBody>
{metrics
{Object.values(metrics)
.filter(m => m.type === 'JVM')
.map((metric, index) => {
return (
<div key={index}>
{metric.name} :{' '}
{metric.name} :
<span>
{metric.value} {metric.unit ?? ''}{' '}
{metric.available && 'of' + metric.available + ' ' + metric.unit}
{metric.value} {metric.unit ?? ''}
{metric.available && 'of' + metric.available + ' ' + (metric.unit ?? '')}
</span>
</div>
)
Expand Down
6 changes: 2 additions & 4 deletions packages/hawtio/src/plugins/runtime/SysProps.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { render, screen, waitFor, within } from '@testing-library/react'
import { SysProps } from './SysProps'
import { SystemProperty } from './types'
import { runtimeService } from './runtime-service'
import userEvent from '@testing-library/user-event'

function getMockedProperties(): SystemProperty[] {
Expand All @@ -11,11 +12,8 @@ function getMockedProperties(): SystemProperty[] {
]
}

jest.mock('./runtime-service', () => ({
loadSystemProperties: jest.fn().mockResolvedValue(getMockedProperties()),
}))

describe('SysProps.tsx', () => {
jest.spyOn(runtimeService, 'loadSystemProperties').mockResolvedValue(getMockedProperties())
const renderSysProps = () => {
return render(<SysProps />)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/hawtio/src/plugins/runtime/SysProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@patternfly/react-core'
import { TableComposable, Tbody, Td, Th, Thead, ThProps, Tr } from '@patternfly/react-table'
import { SearchIcon } from '@patternfly/react-icons'
import { loadSystemProperties } from './runtime-service'
import { runtimeService } from './runtime-service'
import { objectSorter } from '@hawtiosrc/util/objects'
import { SystemProperty } from './types'

Expand All @@ -38,7 +38,7 @@ export const SysProps: React.FunctionComponent = () => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false)

useEffect(() => {
loadSystemProperties().then(props => {
runtimeService.loadSystemProperties().then(props => {
setProperties(props)
setFilteredProperties(props)
})
Expand Down
25 changes: 11 additions & 14 deletions packages/hawtio/src/plugins/runtime/Threads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ import {

import { TableComposable, Tbody, Td, Th, Thead, ThProps, Tr } from '@patternfly/react-table'
import { SearchIcon } from '@patternfly/react-icons'
import {
loadThreads,
dumpThreads,
enableThreadContentionMonitoring,
isThreadContentionMonitoringEnabled,
REFRESH_INTERVAL,
} from '@hawtiosrc/plugins/runtime/runtime-service'
import { runtimeService } from './runtime-service'
import { objectSorter } from '@hawtiosrc/util/objects'
import { Thread } from '@hawtiosrc/plugins/runtime/types'
import { ThreadInfoModal } from './ThreadInfoModal'
Expand All @@ -44,7 +38,7 @@ const ThreadsDumpModal: React.FunctionComponent<{
const [threadsDump, setThreadsDump] = useState<string>('')
useEffect(() => {
const readThreadDump = async () => {
const threadsDump = await dumpThreads()
const threadsDump = await runtimeService.dumpThreads()
setThreadsDump(threadsDump)
}
if (isOpen) {
Expand Down Expand Up @@ -86,16 +80,18 @@ export const Threads: React.FunctionComponent = () => {
const [threadConnectionMonitoring, setThreadConnectionMonitoring] = useState(false)

useEffect(() => {
let timeoutHandle: NodeJS.Timeout
const readThreads = async () => {
const threads = await loadThreads()
const threads = await runtimeService.loadThreads()
setThreads(threads)
setFilteredThreads(threads)
setThreadConnectionMonitoring(await isThreadContentionMonitoringEnabled())
timeoutHandle = setTimeout(readThreads, REFRESH_INTERVAL)
setThreadConnectionMonitoring(await runtimeService.isThreadContentionMonitoringEnabled())
await runtimeService.registerLoadThreadsRequest(threads => {
setThreads(threads)
setFilteredThreads(threads)
})
}
readThreads()
return () => timeoutHandle && clearTimeout(timeoutHandle)
return () => runtimeService.unregisterAll()
}, [])

const onDeleteFilter = (filter: string) => {
Expand Down Expand Up @@ -229,9 +225,10 @@ export const Threads: React.FunctionComponent = () => {
}

async function handleConnectionThreadMonitoring() {
enableThreadContentionMonitoring(!threadConnectionMonitoring)
runtimeService.enableThreadContentionMonitoring(!threadConnectionMonitoring)
setThreadConnectionMonitoring(!threadConnectionMonitoring)
}

return (
<Card isFullHeight>
<CardBody>
Expand Down
Loading

0 comments on commit 47a68e1

Please sign in to comment.