Skip to content

Commit 367b9e7

Browse files
committed
#RI-2640 - final implementation, add tests
1 parent 966ce80 commit 367b9e7

File tree

12 files changed

+206
-55
lines changed

12 files changed

+206
-55
lines changed

redisinsight/ui/src/components/monitor-config/MonitorConfig.spec.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { cloneDeep } from 'lodash'
33
import React from 'react'
44
import MockedSocket from 'socket.io-mock'
55
import socketIO from 'socket.io-client'
6-
import { monitorSelector, setSocket, stopMonitor, toggleRunMonitor } from 'uiSrc/slices/cli/monitor'
6+
import { monitorSelector, setLoadingPause, setSocket, stopMonitor } from 'uiSrc/slices/cli/monitor'
77
import { cleanup, mockedStore, render } from 'uiSrc/utils/test-utils'
88
import { MonitorEvent, SocketEvent } from 'uiSrc/constants'
99
import MonitorConfig from './MonitorConfig'
@@ -52,6 +52,7 @@ describe('MonitorConfig', () => {
5252
const { unmount } = render(<MonitorConfig />)
5353
const afterRenderActions = [
5454
setSocket(socket),
55+
setLoadingPause(true)
5556
]
5657
expect(store.getActions()).toEqual([...afterRenderActions])
5758

@@ -66,14 +67,15 @@ describe('MonitorConfig', () => {
6667

6768
const { unmount } = render(<MonitorConfig />)
6869

69-
socket.on(MonitorEvent.MonitorData, (data) => {
70+
socket.on(MonitorEvent.MonitorData, (data: []) => {
7071
expect(data).toEqual(['message1', 'message2'])
7172
})
7273

7374
socket.socketClient.emit(MonitorEvent.MonitorData, ['message1', 'message2'])
7475

7576
const afterRenderActions = [
7677
setSocket(socket),
78+
setLoadingPause(true)
7779
]
7880
expect(store.getActions()).toEqual([...afterRenderActions])
7981

@@ -88,7 +90,7 @@ describe('MonitorConfig', () => {
8890
})
8991
monitorSelector.mockImplementation(monitorSelectorMock)
9092

91-
socket.on(MonitorEvent.Exception, (error) => {
93+
socket.on(MonitorEvent.Exception, (error: Error) => {
9294
expect(error).toEqual({ message: 'test', name: 'error' })
9395
// done()
9496
})
@@ -97,7 +99,8 @@ describe('MonitorConfig', () => {
9799

98100
const afterRenderActions = [
99101
setSocket(socket),
100-
toggleRunMonitor()
102+
setLoadingPause(true),
103+
stopMonitor()
101104
]
102105
expect(store.getActions()).toEqual([...afterRenderActions])
103106

@@ -112,15 +115,16 @@ describe('MonitorConfig', () => {
112115
})
113116
monitorSelector.mockImplementation(monitorSelectorMock)
114117

115-
socket.on(SocketEvent.ConnectionError, (error) => {
118+
socket.on(SocketEvent.ConnectionError, (error: Error) => {
116119
expect(error).toEqual({ message: 'test', name: 'error' })
117120
})
118121

119122
socket.socketClient.emit(SocketEvent.ConnectionError, { message: 'test', name: 'error' })
120123

121124
const afterRenderActions = [
122125
setSocket(socket),
123-
toggleRunMonitor()
126+
setLoadingPause(true),
127+
stopMonitor()
124128
]
125129
expect(store.getActions()).toEqual([...afterRenderActions])
126130

@@ -139,6 +143,7 @@ describe('MonitorConfig', () => {
139143

140144
const afterRenderActions = [
141145
setSocket(socket),
146+
setLoadingPause(true),
142147
stopMonitor()
143148
]
144149
expect(store.getActions()).toEqual([...afterRenderActions])

redisinsight/ui/src/components/monitor-config/MonitorConfig.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import {
1010
stopMonitor,
1111
setError,
1212
resetMonitorItems,
13-
setStartTimestamp
13+
setStartTimestamp,
14+
setLoadingPause
1415
} from 'uiSrc/slices/cli/monitor'
1516
import { getBaseApiUrl } from 'uiSrc/utils'
1617
import { MonitorErrorMessages, MonitorEvent, SocketErrors, SocketEvent } from 'uiSrc/constants'
1718
import { IMonitorDataPayload } from 'uiSrc/slices/interfaces'
1819
import { connectedInstanceSelector } from 'uiSrc/slices/instances'
19-
import { IOnDatePayload } from 'apiSrc/modules/profiler/helpers/client-monitor-observer'
20+
import { IMonitorData } from 'apiSrc/modules/profiler/interfaces/monitor-data.interface'
2021

2122
import ApiStatusCode from '../../constants/apiStatusCode'
2223

@@ -59,7 +60,8 @@ const MonitorConfig = ({ retryDelay = 10000 } : IProps) => {
5960
let payloads: IMonitorDataPayload[] = []
6061

6162
const handleMonitorEvents = () => {
62-
newSocket.on(MonitorEvent.MonitorData, (payload:IOnDatePayload[]) => {
63+
dispatch(setLoadingPause(false))
64+
newSocket.on(MonitorEvent.MonitorData, (payload: IMonitorData[]) => {
6365
payloads = payloads.concat(payload)
6466

6567
// set batch of payloads and then clear batch
@@ -121,8 +123,14 @@ const MonitorConfig = ({ retryDelay = 10000 } : IProps) => {
121123

122124
useEffect(() => {
123125
if (!isRunning) return
124-
!isPaused && socket?.emit(MonitorEvent.Monitor)
125-
isPaused && socket?.emit(MonitorEvent.Pause)
126+
127+
const pauseUnpause = async () => {
128+
!isPaused && await new Promise<void>((resolve) => { socket?.emit(MonitorEvent.Monitor, () => resolve()) })
129+
isPaused && await new Promise<void>((resolve) => socket?.emit(MonitorEvent.Pause, () => resolve()))
130+
dispatch(setLoadingPause(false))
131+
}
132+
dispatch(setLoadingPause(true))
133+
pauseUnpause().catch(console.error)
126134
}, [isPaused, isRunning])
127135

128136
useEffect(() => {

redisinsight/ui/src/components/monitor/MonitorHeader/MonitorHeader.spec.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
screen,
99
} from 'uiSrc/utils/test-utils'
1010
import {
11+
monitorSelector,
1112
resetMonitorItems,
1213
setMonitorInitialState,
1314
toggleHideMonitor,
@@ -16,8 +17,21 @@ import {
1617
import MonitorHeader, { Props } from './MonitorHeader'
1718

1819
const mockedProps = mock<Props>()
20+
const monitorPath = 'uiSrc/slices/cli/monitor'
1921
let store: typeof mockedStore
2022

23+
jest.mock(monitorPath, () => {
24+
const defaultState = jest.requireActual(monitorPath).initialState
25+
return {
26+
...jest.requireActual(monitorPath),
27+
monitorSelector: jest.fn().mockReturnValue({
28+
...defaultState,
29+
isMinimizedMonitor: false,
30+
isShowMonitor: true,
31+
})
32+
}
33+
})
34+
2135
beforeEach(() => {
2236
cleanup()
2337
store = cloneDeep(mockedStore)
@@ -47,6 +61,10 @@ describe('MonitorHeader', () => {
4761

4862
it('Should call handleRunMonitor after click on the play button', () => {
4963
const handleRunMonitor = jest.fn()
64+
const monitorSelectorMock = jest.fn().mockReturnValue({
65+
isStarted: true,
66+
})
67+
monitorSelector.mockImplementation(monitorSelectorMock)
5068
render(<MonitorHeader handleRunMonitor={handleRunMonitor} />)
5169

5270
fireEvent.click(screen.getByTestId('toggle-run-monitor'))
@@ -55,6 +73,10 @@ describe('MonitorHeader', () => {
5573
})
5674

5775
it('Should clear Monitor items after click on the clear button', () => {
76+
const monitorSelectorMock = jest.fn().mockReturnValue({
77+
isStarted: true,
78+
})
79+
monitorSelector.mockImplementation(monitorSelectorMock)
5880
render(<MonitorHeader {...instance(mockedProps)} />)
5981

6082
fireEvent.click(screen.getByTestId('clear-monitor'))

redisinsight/ui/src/components/monitor/MonitorHeader/MonitorHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface Props {
2929

3030
const MonitorHeader = ({ handleRunMonitor }: Props) => {
3131
const { instanceId = '' } = useParams<{ instanceId: string }>()
32-
const { isRunning, isPaused, isStarted, items, error } = useSelector(monitorSelector)
32+
const { isRunning, isPaused, isStarted, items = [], error, loadingPause } = useSelector(monitorSelector)
3333
const isErrorShown = !!error && !isRunning
3434
const dispatch = useDispatch()
3535

@@ -89,7 +89,7 @@ const MonitorHeader = ({ handleRunMonitor }: Props) => {
8989
onClick={() => handleRunMonitor()}
9090
aria-label="start/stop monitor"
9191
data-testid="toggle-run-monitor"
92-
disabled={isErrorShown}
92+
disabled={isErrorShown || loadingPause}
9393
/>
9494
</EuiToolTip>
9595
<EuiToolTip
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { cloneDeep } from 'lodash'
2+
import React from 'react'
3+
import { resetProfiler, stopMonitor } from 'uiSrc/slices/cli/monitor'
4+
import { cleanup, fireEvent, mockedStore, render, screen } from 'uiSrc/utils/test-utils'
5+
import MonitorLog from './MonitorLog'
6+
7+
let store: typeof mockedStore
8+
9+
beforeEach(() => {
10+
cleanup()
11+
store = cloneDeep(mockedStore)
12+
store.clearActions()
13+
})
14+
15+
describe('MonitorLog', () => {
16+
it('should render', () => {
17+
expect(render(<MonitorLog />)).toBeTruthy()
18+
})
19+
20+
it('should call proper actions on click reset profiler', () => {
21+
render(<MonitorLog />)
22+
fireEvent.click(screen.getByTestId('reset-profiler-btn'))
23+
24+
const expectedActions = [stopMonitor(), resetProfiler()]
25+
expect(store.getActions()).toEqual(expectedActions)
26+
})
27+
})

redisinsight/ui/src/components/monitor/MonitorLog/MonitorLog.tsx

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiIcon, EuiText } from '@elastic/eui'
1+
import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiIcon, EuiText, EuiToolTip } from '@elastic/eui'
22
import { format, formatDuration, intervalToDuration } from 'date-fns'
33
import React from 'react'
44
import { useDispatch, useSelector } from 'react-redux'
@@ -39,19 +39,29 @@ const MonitorLog = () => {
3939
dispatch(resetProfiler())
4040
}
4141

42+
const getPaddingByWidth = (width: number): number => {
43+
if (width < 360) return 6
44+
if (width < 460) return 12
45+
return 18
46+
}
47+
4248
return (
4349
<div className={styles.monitorLogWrapper}>
4450
<AutoSizer disableHeight>
4551
{({ width }) => (
46-
<div className={styles.container} style={{ width }}>
52+
<div
53+
className={styles.container}
54+
style={{ width, paddingLeft: getPaddingByWidth(width), paddingRight: getPaddingByWidth(width) }}
55+
>
4756
<EuiText size="xs" color="subdued" className={styles.time}>
4857
<EuiIcon type="clock" />
4958
{format(timestamp.start, 'hh:mm:ss')}
5059
&nbsp;&#8211;&nbsp;
51-
{format(timestamp.end, 'hh:mm:ss')}
60+
{format(timestamp.paused, 'hh:mm:ss')}
5261
&nbsp;(
5362
{duration}
54-
&nbsp;Running time)
63+
{width > 360 && ' Running time'}
64+
)
5565
</EuiText>
5666
<EuiFlexGroup
5767
className={styles.actions}
@@ -61,17 +71,22 @@ const MonitorLog = () => {
6171
responsive={false}
6272
>
6373
<EuiFlexItem grow={false}>
64-
<EuiButton
65-
size="s"
66-
color="secondary"
67-
href={linkToDownload}
68-
iconType="download"
69-
className={styles.btn}
70-
{...downloadBtnProps}
74+
<EuiToolTip
75+
content="Download Profiler Log"
7176
>
72-
{width > 360 && ' Download '}
73-
Log
74-
</EuiButton>
77+
<EuiButton
78+
size="s"
79+
color="secondary"
80+
href={linkToDownload}
81+
iconType="download"
82+
className={styles.btn}
83+
data-testid="download-log-btn"
84+
{...downloadBtnProps}
85+
>
86+
{width > 360 && ' Download '}
87+
Log
88+
</EuiButton>
89+
</EuiToolTip>
7590
</EuiFlexItem>
7691
<EuiFlexItem grow={false}>
7792
<EuiButton
@@ -81,6 +96,7 @@ const MonitorLog = () => {
8196
onClick={onResetProfiler}
8297
iconType="refresh"
8398
className={styles.btn}
99+
data-testid="reset-profiler-btn"
84100
>
85101
Reset
86102
{width > 360 && ' Profiler'}

redisinsight/ui/src/components/monitor/MonitorLog/styles.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
min-height: 72px;
44

55
margin: 10px 6px 6px 6px;
6-
padding: 6px 18px;
6+
padding: 6px 0;
77

88
background: var(--euiColorLightestShade);
99
box-shadow: 0 3px 15px var(--controlsBoxShadowColor);

redisinsight/ui/src/components/monitor/MonitorWrapper.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const MonitorWrapper = () => {
1919

2020
const handleRunMonitor = () => {
2121
sendEventTelemetry({
22-
event: isRunning ? TelemetryEvent.PROFILER_STOPPED : TelemetryEvent.PROFILER_STARTED,
22+
event: isPaused ? TelemetryEvent.PROFILER_RESUMED : TelemetryEvent.PROFILER_PAUSED,
2323
eventData: { databaseId: instanceId }
2424
})
2525
dispatch(togglePauseMonitor())
@@ -28,7 +28,10 @@ const MonitorWrapper = () => {
2828
const onRunMonitor = (isSaveToLog?: boolean) => {
2929
sendEventTelemetry({
3030
event: TelemetryEvent.PROFILER_STARTED,
31-
eventData: { databaseId: instanceId }
31+
eventData: {
32+
databaseId: instanceId,
33+
logSaving: isSaveToLog
34+
}
3235
})
3336
dispatch(startMonitor(isSaveToLog))
3437
}

0 commit comments

Comments
 (0)