-
Notifications
You must be signed in to change notification settings - Fork 11
feat: SSE console updates from Korrel8r #231
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
Open
alanconway
wants to merge
2
commits into
openshift:main
Choose a base branch
from
alanconway:mcp-korrel8r
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| import { SVGProps } from 'react'; | ||
|
|
||
| // Created from http://redhat.brand-portal.adobe.com/aem/search.html, file name is rh-ui-icon-ai-experience. | ||
| export const AIExperienceIcon = (props: SVGProps<SVGSVGElement>) => ( | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| viewBox="0 0 32 32" | ||
| fill="currentColor" | ||
| width="1em" | ||
| height="1em" | ||
| {...props} | ||
| > | ||
| <path d="M26.03125,16.96191c-5.90527-.46875-10.53076-5.09473-10.99902-11-.0415-.51953-.5166-.96094-1.03809-.96094s-.99658.44238-1.03809.96191c-.46777,5.9043-5.09375,10.53027-10.99121,10.99902-.52344.03711-.96973.51367-.96973,1.03809,0,.52148.44189.99707.96191,1.03809,5.90527.46875,10.53125,5.09473,10.99951,11,.0415.51953.5166.96094,1.0376.96094.52197,0,.99707-.44238,1.03857-.96191.46777-5.9043,5.09326-10.53027,10.99854-10.99902.51953-.04199.96191-.51562.96191-1.03809,0-.52148-.44238-.99707-.96191-1.03809ZM13.99414,25.76465c-1.4126-3.54492-4.21875-6.35059-7.7666-7.76367,3.54639-1.41309,6.354-4.2207,7.76709-7.76562,1.4126,3.54492,4.21826,6.35059,7.76611,7.76367-3.5459,1.41309-6.35352,4.2207-7.7666,7.76562ZM30.50195,7c0,.28906-.20898.53613-.49805.58984-2.2334.4082-4.00879,2.18359-4.41699,4.41699-.05371.28906-.30078.49805-.58984.49805s-.53613-.20898-.58984-.49805c-.40723-2.2334-2.18262-4.00879-4.41699-4.41699-.28906-.05371-.49805-.30078-.49805-.58984s.20898-.53613.49805-.58984c2.23438-.4082,4.00977-2.18359,4.41699-4.41699.05371-.28906.30078-.49805.58984-.49805s.53613.20898.58984.49805c.4082,2.2334,2.18359,4.00879,4.41699,4.41699.28906.05371.49805.30078.49805.58984Z" /> | ||
| </svg> | ||
| ); |
This file contains hidden or 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,79 @@ | ||
| import { | ||
| Dropdown, | ||
| DropdownItem, | ||
| DropdownList, | ||
| Flex, | ||
| FlexItem, | ||
| Icon, | ||
| Label, | ||
| MenuToggle, | ||
| MenuToggleElement, | ||
| Switch, | ||
| } from '@patternfly/react-core'; | ||
| import { Ref, useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { setAgentEnabled } from '../redux-actions'; | ||
| import { State } from '../redux-reducers'; | ||
| import { AIExperienceIcon } from './AIExperienceIcon'; | ||
|
|
||
| const AgentMenu = () => { | ||
| const { t } = useTranslation('plugin__troubleshooting-panel-console-plugin'); | ||
| const dispatch = useDispatch(); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
|
|
||
| const agentEnabled: boolean = useSelector((s: State) => s.plugins?.tp?.get('agentEnabled')); | ||
| const agentConnected: boolean = useSelector((s: State) => s.plugins?.tp?.get('agentConnected')); | ||
|
|
||
| const statusLabel = agentEnabled | ||
| ? agentConnected | ||
| ? t('Connected') | ||
| : t('Connecting...') | ||
| : t('Off'); | ||
|
|
||
| const statusColor = agentEnabled ? (agentConnected ? 'green' : 'orange') : undefined; | ||
|
|
||
| return ( | ||
| <Dropdown | ||
| isOpen={isOpen} | ||
| onOpenChange={setIsOpen} | ||
| popperProps={{ position: 'end' }} | ||
| toggle={(toggleRef: Ref<MenuToggleElement>) => ( | ||
| <MenuToggle | ||
| ref={toggleRef} | ||
| variant="plain" | ||
| onClick={() => setIsOpen(!isOpen)} | ||
| isExpanded={isOpen} | ||
| aria-label={t('AI Agent settings')} | ||
| > | ||
| <Icon> | ||
| <AIExperienceIcon /> | ||
| </Icon> | ||
| </MenuToggle> | ||
| )} | ||
| > | ||
| <DropdownList> | ||
| <DropdownItem key="header"> | ||
| <Flex alignItems={{ default: 'alignItemsCenter' }}> | ||
| <FlexItem grow={{ default: 'grow' }}> | ||
| <Switch | ||
| id="agent-enabled" | ||
| isChecked={agentEnabled} | ||
| hasCheckIcon | ||
| label={t('Agent Navigation')} | ||
| onChange={(_event, checked: boolean) => dispatch(setAgentEnabled(checked))} | ||
| /> | ||
| </FlexItem> | ||
| <FlexItem> | ||
| <Label color={statusColor} isCompact> | ||
| {statusLabel} | ||
| </Label> | ||
| </FlexItem> | ||
| </Flex> | ||
| </DropdownItem> | ||
| </DropdownList> | ||
| </Dropdown> | ||
| ); | ||
| }; | ||
|
|
||
| export default AgentMenu; |
This file contains hidden or 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 hidden or 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,20 +1,90 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { listDomains } from '../korrel8r-client'; | ||
|
|
||
| export const useKorrel8r = () => { | ||
| const [isKorrel8rReachable, setIsKorrel8rReachable] = useState<boolean>(false); | ||
|
|
||
| useEffect(() => { | ||
| listDomains() | ||
| .then(() => { | ||
| setIsKorrel8rReachable(true); | ||
| }) | ||
| .catch(() => { | ||
| setIsKorrel8rReachable(false); | ||
| }); | ||
| }, []); | ||
|
|
||
| return { | ||
| isKorrel8rReachable, | ||
| }; | ||
| import * as React from 'react'; | ||
| import { shallowEqual, useDispatch, useSelector } from 'react-redux'; | ||
| import { consoleEvents, retryWithBackoff, setConsole } from '../korrel8r-client'; | ||
| import { Query } from '../korrel8r/types'; | ||
| import { | ||
| apiSearch, | ||
| openTP, | ||
| reduxSearch, | ||
| Search, | ||
| setAgentConnected, | ||
| setSearch, | ||
| } from '../redux-actions'; | ||
| import { State } from '../redux-reducers'; | ||
| import { useLocationQuery } from './useLocationQuery'; | ||
| import { useNavigateToQuery } from './useNavigateToQuery'; | ||
|
|
||
| const logErr = (what: string, err: any) => { | ||
| if (err?.name !== 'AbortError') { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`korrel8r console ${what}: ${err?.body?.error || err?.message || String(err)}`); | ||
| } | ||
| }; | ||
|
|
||
| const useKorrel8r = ({ | ||
|
alanconway marked this conversation as resolved.
|
||
| minDelay = 100, | ||
| maxDelay = 5000, | ||
| }: { minDelay?: number; maxDelay?: number } = {}) => { | ||
| const view = useLocationQuery()?.toString(); | ||
| const search: Search = useSelector((s: State) => s.plugins?.tp?.get('search'), shallowEqual); | ||
| const isOpen: boolean = useSelector((s: State) => s.plugins?.tp?.get('isOpen')); | ||
|
alanconway marked this conversation as resolved.
|
||
| const agentEnabled: boolean = useSelector((s: State) => s.plugins?.tp?.get('agentEnabled')); | ||
| const dispatch = useDispatch(); | ||
| const navigateToQuery = useNavigateToQuery(); | ||
|
|
||
| // Send console update to korrel8r if there is one. | ||
| React.useEffect(() => { | ||
| if (!(agentEnabled && (view || (isOpen && search?.queryStr)))) { | ||
| return; | ||
| } | ||
|
|
||
| const cleanup = retryWithBackoff( | ||
| async (signal) => { | ||
| const req = setConsole({ | ||
| view, | ||
| search: search?.queryStr ? apiSearch(search) : undefined, | ||
| }); | ||
| signal.addEventListener('abort', () => req.cancel(), { once: true }); | ||
| await req; | ||
| }, | ||
| (err: any) => logErr('send', err), | ||
| { minDelay, maxDelay }, | ||
| ); | ||
|
|
||
| return () => cleanup(); | ||
| }, [view, search, isOpen, agentEnabled, minDelay, maxDelay]); | ||
|
|
||
| const navigateToQueryRef = React.useRef(navigateToQuery); | ||
| React.useEffect(() => { | ||
| navigateToQueryRef.current = navigateToQuery; | ||
| }); | ||
|
|
||
| // Subscribe to console update events from the agent. | ||
| React.useEffect(() => { | ||
| if (!agentEnabled) { | ||
| return; | ||
| } | ||
| return consoleEvents( | ||
| async (event) => { | ||
| if (event.view) { | ||
| navigateToQueryRef.current(Query.parse(event.view), null); | ||
| } | ||
| if (event.search) { | ||
| const search = reduxSearch(event.search); | ||
| if (search) { | ||
| dispatch(setSearch(search)); | ||
| dispatch(openTP()); | ||
| } | ||
| } | ||
| }, | ||
|
alanconway marked this conversation as resolved.
|
||
| (err: Error) => { | ||
| dispatch(setAgentConnected(false)); | ||
| logErr('disconnected', err); | ||
| }, | ||
| () => dispatch(setAgentConnected(true)), | ||
| { minDelay, maxDelay }, | ||
| ); | ||
| }, [agentEnabled, minDelay, maxDelay, dispatch]); | ||
| }; | ||
|
|
||
| export default useKorrel8r; | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.