-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Notifications for Log Viewer #2502
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7317525
Setup useNotification hook
andrewbaldwin44 0e02578
Refactor LogViewer to register notifications
andrewbaldwin44 c0fd967
Add notification alert to tabs
andrewbaldwin44 9616514
Improve typing of ui slice
andrewbaldwin44 cd2363d
Add useNotification tests
andrewbaldwin44 6d27acb
Add isEmpty and objectLength tests
andrewbaldwin44 b5d086e
Add optional shouldNotify callback
andrewbaldwin44 649dbde
Add useNotify tests
andrewbaldwin44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 58 additions & 58 deletions
116
locust/webui/dist/assets/index-5727afdb.js → locust/webui/dist/assets/index-d0af3b25.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { Box, Typography } from '@mui/material'; | ||
|
||
import { SWARM_STATE } from 'constants/swarm'; | ||
import useInterval from 'hooks/useInterval'; | ||
import { useGetLogsQuery } from 'redux/api/swarm'; | ||
import { useSelector } from 'redux/hooks'; | ||
|
||
export default function LogViewer() { | ||
const swarm = useSelector(({ swarm }) => swarm); | ||
const { data, refetch: refetchLogs } = useGetLogsQuery(); | ||
|
||
useInterval(refetchLogs, 5000, { shouldRunInterval: swarm.state !== SWARM_STATE.STOPPED }); | ||
const logs = useSelector(({ logViewer: { logs } }) => logs); | ||
|
||
return ( | ||
<Box> | ||
<Typography component='h2' variant='h4'> | ||
Logs | ||
</Typography> | ||
|
||
<ul>{data && data.logs.map((log, index) => <li key={`log-${index}`}>{log}</li>)}</ul> | ||
<ul> | ||
{logs.map((log, index) => ( | ||
<li key={`log-${index}`}>{log}</li> | ||
))} | ||
</ul> | ||
</Box> | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
locust/webui/src/components/LogViewer/tests/LogViewer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import LogViewer from 'components/LogViewer/LogViewer'; | ||
import { swarmStateMock } from 'test/mocks/swarmState.mock'; | ||
import { renderWithProvider } from 'test/testUtils'; | ||
|
||
describe('LogViewer', () => { | ||
test('should render host, status, RPS, and failures on first load', async () => { | ||
const { getByText } = renderWithProvider(<LogViewer />, { | ||
swarm: swarmStateMock, | ||
logViewer: { | ||
logs: ['Log 1', 'Log 2', 'Log 3'], | ||
}, | ||
}); | ||
|
||
expect(getByText('Log 1')).toBeTruthy(); | ||
expect(getByText('Log 2')).toBeTruthy(); | ||
expect(getByText('Log 3')).toBeTruthy(); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
locust/webui/src/components/LogViewer/tests/useLogViewer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { waitFor } from '@testing-library/react'; | ||
import { http, HttpResponse } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest'; | ||
|
||
import useLogViewer from 'components/LogViewer/useLogViewer'; | ||
import { TEST_BASE_API } from 'test/constants'; | ||
import { swarmStateMock } from 'test/mocks/swarmState.mock'; | ||
import { renderWithProvider } from 'test/testUtils'; | ||
|
||
const mockLogs = ['Log 1', 'Log 2', 'Log 3']; | ||
|
||
const server = setupServer( | ||
http.get(`${TEST_BASE_API}/logs`, () => HttpResponse.json({ logs: mockLogs })), | ||
); | ||
|
||
function MockHook() { | ||
const logs = useLogViewer(); | ||
|
||
return <span data-testid='logs'>{JSON.stringify(logs)}</span>; | ||
} | ||
|
||
describe('useLogViewer', () => { | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
test('should fetch logs from server and store them in state', async () => { | ||
const { store, getByTestId } = renderWithProvider(<MockHook />, { | ||
swarm: swarmStateMock, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('logs').textContent).toBe(JSON.stringify(mockLogs)); | ||
expect(store.getState().logViewer.logs).toEqual(mockLogs); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
import { SWARM_STATE } from 'constants/swarm'; | ||
import useInterval from 'hooks/useInterval'; | ||
import useNotifications from 'hooks/useNotifications'; | ||
import { useGetLogsQuery } from 'redux/api/swarm'; | ||
import { useAction, useSelector } from 'redux/hooks'; | ||
import { logViewerActions } from 'redux/slice/logViewer.slice'; | ||
|
||
const isImportantLog = (log: string) => | ||
log.includes('WARNING') || log.includes('ERROR') || log.includes('CRITICAL'); | ||
|
||
export default function useLogViewer() { | ||
const swarm = useSelector(({ swarm }) => swarm); | ||
const setLogs = useAction(logViewerActions.setLogs); | ||
const { data, refetch: refetchLogs } = useGetLogsQuery(); | ||
|
||
const logs = data ? data.logs : []; | ||
|
||
const shouldNotifyLogsUpdate = useCallback( | ||
() => logs.slice(localStorage['logViewer']).some(isImportantLog), | ||
[logs], | ||
); | ||
|
||
useInterval(refetchLogs, 5000, { | ||
shouldRunInterval: swarm.state === SWARM_STATE.SPAWNING || swarm.state == SWARM_STATE.RUNNING, | ||
}); | ||
useNotifications(logs, { key: 'logViewer', shouldNotify: shouldNotifyLogsUpdate }); | ||
|
||
useEffect(() => { | ||
setLogs({ logs }); | ||
}, [logs]); | ||
|
||
return logs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Moved, not deleted)