Skip to content

Commit 0e1b874

Browse files
Merge pull request #214 from alanconway/multi-fetch
COO-1689: fix: prevent repeated reload of fetch useEffect
2 parents 65bb04b + 9df9278 commit 0e1b874

2 files changed

Lines changed: 43 additions & 51 deletions

File tree

web/locales/en/plugin__troubleshooting-panel-console-plugin.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@
2020
"From": "From",
2121
"Goals": "Goals",
2222
"Include data from this time range": "Include data from this time range",
23-
"Korrel8r Error": "Korrel8r Error",
2423
"Last": "Last",
2524
"Logging Plugin Disabled": "Logging Plugin Disabled",
2625
"Neighbours": "Neighbours",
2726
"Neighbours or Goal search": "Neighbours or Goal search",
2827
"Netflow Plugin Disabled": "Netflow Plugin Disabled",
29-
"No correlated data was found": "No correlated data was found",
28+
"No correlated data found": "No correlated data found",
3029
"No Correlated Signals Found": "No Correlated Signals Found",
3130
"No starting point for correlation": "No starting point for correlation",
3231
"Open the Troubleshooting Panel": "Open the Troubleshooting Panel",
3332
"Other duration": "Other duration",
3433
"Quickly diagnose and resolve issues by exploring correlated observability signals for resources.": "Quickly diagnose and resolve issues by exploring correlated observability signals for resources.",
3534
"Refresh": "Refresh",
3635
"Refresh the graph by re-running the current search.": "Refresh the graph by re-running the current search.",
37-
"Request Failed": "Request Failed",
3836
"Save": "Save",
3937
"Search": "Search",
38+
"Search Error": "Search Error",
39+
"Search Failed": "Search Failed",
4040
"Search Type": "Search Type",
4141
"Searching": "Searching",
4242
"Select starting data": "Select starting data",

web/src/components/Korrel8rPanel.tsx

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,40 @@ export default function Korrel8rPanel() {
5353
// Showing advanced query
5454
const [showAdvanced, setShowAdvanced] = React.useState(false);
5555

56-
// Compute constraint from period for the API call and topology navigation.
56+
// Compute constraint from search period.
5757
const constraint = React.useMemo((): korrel8r.Constraint | undefined => {
5858
if (!search.period) return undefined;
59-
const [pStart, pEnd] = search.period.startEnd();
60-
return new korrel8r.Constraint({ start: pStart, end: pEnd });
61-
}, [search.period]);
59+
const [start, end] = search.period.startEnd();
60+
return new korrel8r.Constraint({ start, end });
61+
}, [search?.period]);
6262

63-
// Call korrel8r service to get results for the current search.
64-
React.useEffect(() => {
65-
// If we already have a stored result, do nothing.
66-
// Dispatching SetSearch sets result=null, triggering a new fetch.
67-
if (result) return;
68-
69-
let cancelled = false;
70-
const onResult = (newResult: Result) => {
71-
if (!cancelled) dispatch(setResult(newResult));
72-
};
63+
// Dispatch a new search value, making a new reference (reducer clears result automatically).
64+
const dispatchSearch = React.useCallback(
65+
(search: Search) => dispatch(setSearch({ ...search })),
66+
[dispatch],
67+
);
68+
// Dispatch a new result, no special actions.
69+
const dispatchResult = React.useCallback(
70+
(result: Result) => dispatch(setResult(result)),
71+
[dispatch],
72+
);
7373

74-
const queryStr = (search?.queryStr ?? '').trim();
74+
// Set up default locationQuery search on mount, when query is blank.
75+
React.useEffect(() => {
76+
if (!search?.queryStr && locationQuery?.toString())
77+
dispatchSearch({ ...defaultSearch, queryStr: locationQuery.toString() });
78+
}, [locationQuery, dispatchSearch, search?.queryStr]);
7579

80+
// Fetch a new result from the korrel8r service when the search changes.
81+
React.useEffect(() => {
82+
const queryStr = search?.queryStr;
7683
if (!queryStr) {
77-
// Default query or empty result
78-
if (locationQuery?.toString()) {
79-
dispatch(setSearch({ ...defaultSearch, queryStr: locationQuery?.toString() }));
80-
} else {
81-
dispatch(
82-
setResult({
83-
title: t('Empty Query'),
84-
message: t('No starting point for correlation'),
85-
}),
86-
);
87-
}
84+
dispatchResult({ title: t('Empty Query'), message: t('No starting point for correlation') });
8885
return;
8986
}
90-
87+
let cancelled = false;
9188
const start: api.Start = {
92-
queries: queryStr ? [queryStr] : undefined,
89+
queries: [queryStr],
9390
constraint: constraint?.toAPI(),
9491
};
9592

@@ -99,18 +96,17 @@ export default function Korrel8rPanel() {
9996
: getNeighborsGraph({ start, depth: search.depth });
10097
fetch
10198
.then((response: api.Graph) => {
102-
if (Array.isArray(response?.nodes) && response.nodes.length > 0) {
103-
onResult({ graph: new korrel8r.Graph(response) });
104-
} else {
105-
onResult({
106-
title: t('Empty Result'),
107-
message: t('No correlated data was found'),
108-
});
109-
}
99+
if (cancelled) return;
100+
dispatchResult(
101+
Array.isArray(response?.nodes) && response.nodes.length > 0
102+
? { graph: new korrel8r.Graph(response) }
103+
: { title: t('Empty Result'), message: t('No correlated data found') },
104+
);
110105
})
111106
.catch((e: api.ApiError) => {
112-
onResult({
113-
title: e?.body?.error ? t('Korrel8r Error') : t('Request Failed'),
107+
if (cancelled) return;
108+
dispatchResult({
109+
title: e?.body?.error ? t('Search Error') : t('Search Failed'),
114110
message: e?.body?.error || e.message || 'Unknown Error',
115111
isError: true,
116112
});
@@ -119,22 +115,18 @@ export default function Korrel8rPanel() {
119115
cancelled = true;
120116
fetch.cancel();
121117
};
122-
}, [search, t, result, constraint, dispatch, locationQuery]);
118+
}, [search, constraint, dispatchResult, t]);
123119

124120
const advancedToggleID = 'query-toggle';
125121
const advancedContentID = 'query-content';
126122

127-
// Dispatch a new search.
128-
// The SetSearch reducer clears result, triggering the fetch effect.
129-
const doSearch = React.useCallback((s: Search) => dispatch(setSearch(s)), [dispatch]);
130-
131123
return (
132124
<>
133125
<Stack>
134126
<Flex
135127
className="tp-plugin__panel-toolbar"
136128
direction={{ default: 'row' }}
137-
flexWrap={{ default: 'nowrap' }}
129+
flexWrap={{ default: 'wrap' }}
138130
alignItems={{ default: 'alignItemsCenter' }}
139131
spaceItems={{ default: 'spaceItemsXs' }}
140132
>
@@ -152,7 +144,7 @@ export default function Korrel8rPanel() {
152144
isAriaDisabled={!locationQuery || isFocused}
153145
size="sm"
154146
onClick={() => {
155-
doSearch({
147+
dispatchSearch({
156148
...defaultSearch,
157149
queryStr: locationQuery?.toString(),
158150
period: search?.period,
@@ -170,7 +162,7 @@ export default function Korrel8rPanel() {
170162
<TimeRangeDropdown
171163
className="tp-plugin__compact-control"
172164
period={search.period ?? defaultSearch.period}
173-
onChange={(period: time.Period) => doSearch({ ...search, period })}
165+
onChange={(period: time.Period) => dispatchSearch({ ...search, period })}
174166
/>
175167
</Tooltip>
176168
</Flex>
@@ -196,7 +188,7 @@ export default function Korrel8rPanel() {
196188
variant="link"
197189
size="sm"
198190
isAriaDisabled={!search?.queryStr}
199-
onClick={() => doSearch(search)}
191+
onClick={() => dispatchSearch(search)}
200192
aria-label={t('Refresh')}
201193
>
202194
<SyncIcon />
@@ -214,7 +206,7 @@ export default function Korrel8rPanel() {
214206
>
215207
<AdvancedSearchForm
216208
search={search}
217-
onSearch={doSearch}
209+
onSearch={dispatchSearch}
218210
onCancel={() => setShowAdvanced(false)}
219211
/>
220212
</ExpandableSection>

0 commit comments

Comments
 (0)