Skip to content

Commit

Permalink
refactor(springboot-plugin): incorporate PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelko authored and tadayosi committed Nov 24, 2023
1 parent e1cf816 commit a23b6c5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 94 deletions.
4 changes: 2 additions & 2 deletions packages/hawtio/src/plugins/springboot/Health.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
PageSection,
Title,
} from '@patternfly/react-core'
import { springbootService } from '@hawtiosrc/plugins/springboot/springboot-service'
import { HealthComponentDetail, HealthData } from '@hawtiosrc/plugins/springboot/types'
import { springbootService } from './springboot-service'
import { HealthComponentDetail, HealthData } from './types'
import { TableComposable, Tbody, Td, Tr } from '@patternfly/react-table'
import { humanizeLabels } from '@hawtiosrc/util/strings'
import { ChartDonutUtilization } from '@patternfly/react-charts'
Expand Down
2 changes: 1 addition & 1 deletion packages/hawtio/src/plugins/springboot/Info.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'
import { FormGroup, PageSection } from '@patternfly/react-core'
import { springbootService } from '@hawtiosrc/plugins/springboot/springboot-service'
import { springbootService } from './springboot-service'
import { TableComposable, Tbody, Td, Th, Thead, Tr } from '@patternfly/react-table'

export const Info: React.FunctionComponent = () => {
Expand Down
39 changes: 11 additions & 28 deletions packages/hawtio/src/plugins/springboot/Loggers.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react'
import React, { useEffect, useState } from 'react'
import {
Bullseye,
Button,
Expand All @@ -19,10 +19,10 @@ import {
ToolbarGroup,
ToolbarItem,
} from '@patternfly/react-core'
import { springbootService } from '@hawtiosrc/plugins/springboot/springboot-service'
import { springbootService } from './springboot-service'
import { TableComposable, Tbody, Td, Th, Thead, Tr } from '@patternfly/react-table'
import { SearchIcon } from '@patternfly/react-icons'
import { Logger } from '@hawtiosrc/plugins/springboot/types'
import { Logger } from './types'

const SetLogDropdown: React.FunctionComponent<{
currentLevel: string
Expand Down Expand Up @@ -105,33 +105,16 @@ export const Loggers: React.FunctionComponent = () => {
})
}, [reloadLoggers])

const handleSearch = useCallback(
(value: string, logLevel: string, filters: string[]) => {
let filtered: Logger[] =
loggers.filter(logger => {
if (logLevel === 'ALL') {
if (value === '') {
return true
} else {
return logger.name.toLowerCase().includes(value.toLowerCase())
}
}
return logger.configuredLevel === logLevel && logger.name.toLowerCase().includes(value.toLowerCase())
}) ?? []

//filter with the rest of the filters
filters.forEach(value => {
filtered = filtered.filter(prop => prop.name.toLowerCase().includes(value.toLowerCase()))
})
setFilteredLoggers([...filtered])
},
[loggers],
)

useEffect(() => {
handleSearch(searchTerm, logLevel, filters)
let filtered: Logger[] = loggers.filter(logger => logLevel === 'ALL' || logger.configuredLevel === logLevel)

//filter with the rest of the filters
;[...filters, searchTerm].forEach(value => {
filtered = filtered.filter(prop => prop.name.toLowerCase().includes(value.toLowerCase()))
})
setFilteredLoggers([...filtered])
setPage(1)
}, [filters, searchTerm, logLevel, loggers, handleSearch])
}, [filters, searchTerm, logLevel, loggers])

const onDeleteFilter = (filter: string) => {
const newFilters = filters.filter(f => f !== filter)
Expand Down
14 changes: 7 additions & 7 deletions packages/hawtio/src/plugins/springboot/SpringBoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Nav, NavItem, NavList, PageGroup, PageNavigation, PageSection, Title }
import React, { useEffect, useState } from 'react'

import { Navigate, NavLink, Route, Routes, useLocation } from 'react-router-dom'
import { Health } from '@hawtiosrc/plugins/springboot/Health'
import { Info } from '@hawtiosrc/plugins/springboot/Info'
import { Loggers } from '@hawtiosrc/plugins/springboot/Loggers'
import { TraceView } from '@hawtiosrc/plugins/springboot/TraceView'
import { springbootService } from '@hawtiosrc/plugins/springboot/springboot-service'
import { Health } from './Health'
import { Info } from './Info'
import { Loggers } from './Loggers'
import { TraceView } from './TraceView'
import { springbootService } from './springboot-service'

type NavItem = {
id: string
Expand All @@ -33,12 +33,12 @@ export const SpringBoot: React.FunctionComponent = () => {
}

if (await springbootService.hasEndpoint('Httptrace')) {
springbootService.setisSb3(false)
springbootService.setIsSpringBoot3(false)
nav.push({ id: 'trace', title: 'Trace', component: <TraceView /> })
}

if (await springbootService.hasEndpoint('Httpexchanges')) {
springbootService.setisSb3(true)
springbootService.setIsSpringBoot3(true)
nav.push({ id: 'trace', title: 'Trace', component: <TraceView /> })
}

Expand Down
70 changes: 29 additions & 41 deletions packages/hawtio/src/plugins/springboot/TraceView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react'
import React, { useEffect, useState } from 'react'
import {
Bullseye,
Button,
Expand Down Expand Up @@ -29,8 +29,8 @@ import { Trace } from './types'
import { springbootService } from './springboot-service'

const HttpStatusIcon: React.FunctionComponent<{ code: number }> = ({ code }) => {
if (code < 400) return <CheckCircleIcon color={'#3E8635'} />
else return <ExclamationCircleIcon color={'#C9190B'} />
if (code < 400) return <CheckCircleIcon color='#3E8635' />
else return <ExclamationCircleIcon color='#C9190B' />
}

const HttpMethodLabel: React.FunctionComponent<{ method: string }> = ({ method }) => {
Expand Down Expand Up @@ -85,42 +85,6 @@ export const TraceView: React.FunctionComponent = () => {
const [isTraceDetailsOpen, setIsTraceDetailsOpen] = useState(false)
const [traceDetails, setTraceDetails] = useState<string>('')

const handleSearch = useCallback(
(filters: string[], searchTerm: string, httpMethod: string, currentFilter: string) => {
let filtered: Trace[] = []

if (httpMethod === 'ALL') {
filtered = [...traces]
} else {
filtered = traces.filter(trace => {
return trace.method.toLowerCase().includes(httpMethod.toLowerCase())
})
}

//filter with the rest of the filters
;[...filters, `${currentFilter}: ${searchTerm}`].forEach(value => {
const attr = value.split(': ')[0] ?? ''
const searchTerm = value.split(': ')[1] ?? ''
switch (attr) {
case 'Timestamp':
filtered = filtered.filter(trace => trace.timestamp.includes(searchTerm))
break
case 'HTTP Status':
filtered = filtered.filter(trace => trace.httpStatusCode.toString().includes(searchTerm))
break
case 'Path':
filtered = filtered.filter(trace => trace.path.includes(searchTerm))
break
case 'Time Taken':
filtered = filtered.filter(trace => trace.timeTaken.includes(searchTerm))
break
}
})
setFilteredTraces([...filtered])
},
[traces],
)

useEffect(() => {
springbootService.loadTraces().then(traces => {
setTraces(traces)
Expand All @@ -129,8 +93,32 @@ export const TraceView: React.FunctionComponent = () => {
}, [])

useEffect(() => {
handleSearch(filters, searchTerm, httpMethodFilter, currentTraceFilter)
}, [currentTraceFilter, filters, handleSearch, httpMethodFilter, searchTerm])
let filtered: Trace[] = traces.filter(
trace => httpMethodFilter === 'ALL' || trace.method.toLowerCase().includes(httpMethodFilter.toLowerCase()),
)

//filter with the rest of the filters
;[...filters, `${currentTraceFilter}: ${searchTerm}`].forEach(value => {
const attr = value.split(': ')[0] ?? ''
const searchTerm = value.split(': ')[1] ?? ''
switch (attr) {
case 'Timestamp':
filtered = filtered.filter(trace => trace.timestamp.includes(searchTerm))
break
case 'HTTP Status':
filtered = filtered.filter(trace => trace.httpStatusCode.toString().includes(searchTerm))
break
case 'Time Taken':
filtered = filtered.filter(trace => trace.timeTaken.includes(searchTerm))
break
case 'Path':
default:
filtered = filtered.filter(trace => trace.path.includes(searchTerm))
break
}
})
setFilteredTraces(filtered)
}, [traces, currentTraceFilter, filters, httpMethodFilter, searchTerm])

const onDeleteFilter = (filter: string) => {
const newFilters = filters.filter(f => f !== filter)
Expand Down
2 changes: 1 addition & 1 deletion packages/hawtio/src/plugins/springboot/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hawtio, HawtioPlugin } from '@hawtiosrc/core'

import { helpRegistry } from '@hawtiosrc/help'
import { SpringBoot } from '@hawtiosrc/plugins/springboot/SpringBoot'
import { SpringBoot } from './SpringBoot'
import { pluginId, pluginPath } from './globals'
import help from './help.md'
import { springbootService } from './springboot-service'
Expand Down
22 changes: 11 additions & 11 deletions packages/hawtio/src/plugins/springboot/springboot-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { jolokiaService, workspace } from '@hawtiosrc/plugins'
import { HealthComponent, HealthData, JmXTrace, JolokiaHealthData, Logger, LoggerConfiguration, Trace } from './types'
import { HealthComponent, HealthData, JmxTrace, JolokiaHealthData, Logger, LoggerConfiguration, Trace } from './types'

class SpringbootService {
isSb3 = true
class SpringBootService {
isSpringBoot3 = true

isActive(): Promise<boolean> {
return workspace.treeContainsDomainAndProperties('org.springframework.boot')
}

setisSb3(is: boolean) {
this.isSb3 = is
setIsSpringBoot3(is: boolean) {
this.isSpringBoot3 = is
}

async loadHealth(): Promise<HealthData> {
Expand Down Expand Up @@ -115,19 +115,19 @@ class SpringbootService {
const traces: Trace[] = []
let mbeanName = 'Httpexchanges'
let mbeanOperation = 'httpExchanges'
let jmxTraces: JmXTrace[] = []
if (!this.isSb3) {
let jmxTraces: JmxTrace[] = []
if (!this.isSpringBoot3) {
mbeanName = 'Httptrace'
mbeanOperation = 'traces'
}
const data = await jolokiaService.execute(
`org.springframework.boot:type=Endpoint,name=${mbeanName}`,
mbeanOperation,
)
if (this.isSb3) {
jmxTraces = (data as { exchanges: JmXTrace[] }).exchanges
if (this.isSpringBoot3) {
jmxTraces = (data as { exchanges: JmxTrace[] }).exchanges
} else {
jmxTraces = (data as { traces: JmXTrace[] }).traces
jmxTraces = (data as { traces: JmxTrace[] }).traces
}

jmxTraces
Expand All @@ -143,4 +143,4 @@ class SpringbootService {
}
}

export const springbootService = new SpringbootService()
export const springbootService = new SpringBootService()
5 changes: 2 additions & 3 deletions packages/hawtio/src/plugins/springboot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export class Trace {
timeTaken: string = '-1'
info: string = ''

constructor(traceData: unknown) {
const trace = traceData as JmXTrace
constructor(trace: JmxTrace) {
this.timestamp = trace.timestamp

if (trace.info) {
Expand Down Expand Up @@ -78,7 +77,7 @@ export class Trace {
}
}

export type JmXTrace = {
export type JmxTrace = {
timestamp: string
info?: {
method: string
Expand Down

0 comments on commit a23b6c5

Please sign in to comment.