1- import * as React from 'react' ;
2- import { CancellableFetch } from '../cancellable-fetch' ;
31import {
42 Button ,
53 Divider ,
@@ -16,25 +14,36 @@ import {
1614 Radio ,
1715 TextArea ,
1816 TextInput ,
19- Tooltip ,
17+ Tooltip
2018} from '@patternfly/react-core' ;
21- import { Korrel8rTopology } from './topology/Korrel8rTopology' ;
22- import './korrel8rpanel.css' ;
23- import { getGoalsGraph , getNeighborsGraph } from '../korrel8r-client' ;
24- import { Korrel8rGraphResponse } from '../korrel8r/query.types' ;
25- import { LoadingTopology } from './topology/LoadingTopology' ;
26- import { TFunction , useTranslation } from 'react-i18next' ;
2719import { CubesIcon , ExclamationCircleIcon } from '@patternfly/react-icons' ;
28- import { useURLState } from '../hooks/useURLState ' ;
29- import { usePluginAvailable } from '../hooks/usePluginAvailable ' ;
20+ import * as React from 'react ' ;
21+ import { TFunction , useTranslation } from 'react-i18next ' ;
3022import { useDispatch , useSelector } from 'react-redux' ;
31- import { State } from '../redux-reducers' ;
23+ import { usePluginAvailable } from '../hooks/usePluginAvailable' ;
24+ import { useURLState } from '../hooks/useURLState' ;
25+ import { getGoalsGraph , getNeighborsGraph } from '../korrel8r-client' ;
26+ import { Korrel8rGraphResponse } from '../korrel8r/query.types' ;
3227import { Query , QueryType , setPersistedQuery } from '../redux-actions' ;
28+ import { State } from '../redux-reducers' ;
29+ import './korrel8rpanel.css' ;
30+ import { Korrel8rTopology } from './topology/Korrel8rTopology' ;
31+ import { LoadingTopology } from './topology/LoadingTopology' ;
3332
3433type Result = {
3534 graph ?: Korrel8rGraphResponse ;
3635 message ?: string ;
3736 title ?: string ;
37+ isError ?: boolean ;
38+ } ;
39+
40+ const focusQuery = ( urlQuery : string ) : Query => {
41+ return {
42+ query : urlQuery ,
43+ queryType : QueryType . Neighbour ,
44+ depth : 3 ,
45+ goal : null ,
46+ } ;
3847} ;
3948
4049export default function Korrel8rPanel ( ) {
@@ -46,34 +55,28 @@ export default function Korrel8rPanel() {
4655
4756 // State
4857 const { korrel8rQueryFromURL } = useURLState ( ) ;
49- // Initial value from persisted query if available (when opened from anywhere but
50- // the alerts page or if never opened before), otherwise URL
51- const initialQuery = persistedQuery . query
52- ? persistedQuery
53- : {
54- query : korrel8rQueryFromURL ,
55- queryType : QueryType . Neighbour ,
56- depth : 3 ,
57- goal : null ,
58- } ;
59- const [ query , setQuery ] = React . useState < Query > ( initialQuery ) ;
58+ const [ query , setQuery ] = React . useState < Query > (
59+ persistedQuery . query ? persistedQuery : focusQuery ( korrel8rQueryFromURL ) ,
60+ ) ;
6061 const [ result , setResult ] = React . useState < Result | null > ( null ) ;
6162 const [ showQuery , setShowQuery ] = React . useState ( false ) ;
6263
64+ const cannotFocus = t (
65+ 'The current console page does not show resources that are supported for correlation.' ,
66+ ) ;
67+
6368 React . useEffect ( ( ) => {
6469 // Set result = null to trigger a reload, don't run the query till then.
6570 if ( result !== null ) {
6671 return ;
6772 }
68- let fetch : CancellableFetch < Korrel8rGraphResponse > ;
69- if ( query . queryType === QueryType . Neighbour ) {
70- fetch = getNeighborsGraph ( query ) ;
71- } else if ( query . queryType === QueryType . Goal ) {
72- fetch = getGoalsGraph ( query ) ;
73- } else {
73+ if ( ! query ?. query && ! korrel8rQueryFromURL ) {
74+ setResult ( { message : cannotFocus } ) ;
7475 return ;
7576 }
76- const { request, abort } = fetch ;
77+ // Make the query request
78+ const { request, abort } =
79+ query . queryType === QueryType . Goal ? getGoalsGraph ( query ) : getNeighborsGraph ( query ) ;
7780 request ( )
7881 . then ( ( response : Korrel8rGraphResponse ) => {
7982 setResult ( { graph : { nodes : response . nodes , edges : response . edges } } ) ;
@@ -84,22 +87,23 @@ export default function Korrel8rPanel() {
8487 } )
8588 . catch ( ( e : Error ) => {
8689 try {
87- setResult ( { message : JSON . parse ( e . message ) . error , title : t ( 'Korrel8r Error' ) } ) ;
90+ setResult ( {
91+ isError : true ,
92+ message : JSON . parse ( e . message ) . error ,
93+ title : t ( 'Korrel8r Error' ) ,
94+ } ) ;
8895 } catch {
89- setResult ( { message : e . message , title : t ( 'Error Loading Data ' ) } ) ;
96+ setResult ( { isError : true , message : e . message , title : t ( 'Request Failed ' ) } ) ;
9097 }
9198 } ) ;
9299 return abort ;
93- } , [ result , t , dispatch , query ] ) ;
100+ } , [ result , t , dispatch , query , cannotFocus , korrel8rQueryFromURL ] ) ;
94101
95102 const queryToggleID = 'query-toggle' ;
96103 const queryContentID = 'query-content' ;
97104 const queryInputID = 'query-input' ;
98105 const queryTypeOptions = 'query-type-options' ;
99106
100- const cannotFocus = t (
101- 'The current console page does not show resources that are supported for correlation.' ,
102- ) ;
103107 const focusTip = korrel8rQueryFromURL
104108 ? t ( 'Re-calculate the correlation graph starting from resources on the current console page.' )
105109 : cannotFocus ;
@@ -110,7 +114,6 @@ export default function Korrel8rPanel() {
110114 ( newQuery : Query ) => {
111115 newQuery . depth = depthBounds ( newQuery . depth ) ;
112116 newQuery . queryType = ! newQuery . goal ? QueryType . Neighbour : newQuery . queryType ;
113-
114117 setQuery ( newQuery ) ;
115118 setResult ( null ) ;
116119 } ,
@@ -123,14 +126,7 @@ export default function Korrel8rPanel() {
123126 < Tooltip content = { focusTip } >
124127 < Button
125128 isAriaDisabled = { ! korrel8rQueryFromURL }
126- onClick = { ( ) => {
127- runQuery ( {
128- query : korrel8rQueryFromURL ,
129- queryType : QueryType . Neighbour ,
130- depth : 3 ,
131- goal : null ,
132- } ) ;
133- } }
129+ onClick = { ( ) => runQuery ( focusQuery ( korrel8rQueryFromURL ) ) }
134130 >
135131 { t ( 'Focus' ) }
136132 </ Button >
@@ -244,7 +240,7 @@ export default function Korrel8rPanel() {
244240 </ FlexItem >
245241 </ Flex >
246242 </ Flex >
247- < Button isAriaDisabled = { ! query } onClick = { ( ) => runQuery ( query ) } variant = "secondary" >
243+ < Button isAriaDisabled = { ! query ?. query } onClick = { ( ) => runQuery ( query ) } variant = "secondary" >
248244 { t ( 'Query' ) }
249245 </ Button >
250246 </ ExpandableSection >
@@ -284,23 +280,12 @@ const Topology: React.FC<TopologyProps> = ({ result, t, setQuery }) => {
284280 ) ;
285281 }
286282
287- if ( result . message ) {
288- // Error returned from korrel8r
289- return (
290- < TopologyInfoState
291- titleText = { result . title }
292- // Only display fisrt 400 characters of error to prevent repeating errors
293- text = { result . message . slice ( 0 , 400 ) }
294- isError = { true }
295- />
296- ) ;
297- }
298-
299283 return (
300284 < TopologyInfoState
301- titleText = { t ( 'No Correlated Signals Found' ) }
302- text = { t ( 'Correlation result was empty.' ) }
303- isError = { false }
285+ titleText = { result . title || t ( 'No Correlated Signals Found' ) }
286+ // Only display fisrt 400 characters of error to prevent repeating errors
287+ text = { result . message ? result . message . slice ( 0 , 400 ) : t ( 'Correlation result was empty.' ) }
288+ isError = { result . isError }
304289 />
305290 ) ;
306291} ;
0 commit comments