[Lens] [ES|QL] Refresh ES|QL results grid on data load#264017
Conversation
|
/ci |
|
/ci |
|
Pinging @elastic/kibana-visualizations (Team:Visualizations) |
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Async chunks
History
|
| const lensAdaptersRef = useRef(lensAdapters); | ||
| useEffect(() => { | ||
| lensAdaptersRef.current = lensAdapters; | ||
| }, [lensAdapters]); |
There was a problem hiding this comment.
Do we need the useEffect here? Maybe we can do the same like with the refs further up like:
const lensAdaptersRef = useRef(lensAdapters);
lensAdaptersRef.current = lensAdapters;| lensAdaptersRef.current = lensAdapters; | ||
| }, [lensAdapters]); | ||
|
|
||
| /** Skips duplicate `getSuggestions` when chart reload follows a query submit (see `runQuery`) */ |
There was a problem hiding this comment.
Maybe we can be more specific with that triggers an update? Then we don't have to track duplicates but can separate what's triggering an update. As far as I could see searchSessionId is in redux what tracks external context change (time range, filters, auto-refresh, manual refresh), so it would cover triggers besides the editor submit. So we could do something like the following:
const searchSessionId = useLensSelector(selectSearchSessionId);
const isInitialRenderRef = useRef(true);
useEffect(() => {
// Skip the initial render — the grid is populated by useInitializeChart → runQuery
if (isInitialRenderRef.current) {
isInitialRenderRef.current = false;
return;
}
const lastSubmittedQuery = submittedQueryRef.current;
if (isOfAggregateQueryType(lastSubmittedQuery)) {
getSuggestions(
lastSubmittedQuery,
data,
http,
uiSettings,
datasourceMap,
visualizationMap,
adHocDataViews,
undefined,
undefined,
setDataGridAttrs,
esqlVariables,
false,
currentAttributesRef.current
).catch(() => {
// Grid refresh is best-effort; errors here (e.g. expired time range) are non-fatal.
// The chart itself will surface query errors via its own error handling path.
});
}
}, [
searchSessionId,
esqlVariables,
data,
http,
uiSettings,
datasourceMap,
visualizationMap,
adHocDataViews,
]);We could then use useObservable() instead of managing the subscription ourselves. Like:
// Update activeData and column limit indicator when chart data finishes loading
const isDataLoading = useObservable(dataLoading$ ?? EMPTY);
useEffect(() => {
if (isDataLoading !== false) {
return;
}
const activeData = getActiveDataFromDatatable(layerId, lensAdaptersRef.current?.tables?.tables);
const table = activeData?.[layerId];
if (table) {
// there are cases where a query can return a big amount of columns
// at this case we don't suggest all columns in a table but the first `MAX_NUM_OF_COLUMNS`
setSuggestsLimitedColumns(table.columns.length >= MAX_NUM_OF_COLUMNS);
}
if (Object.keys(activeData).length > 0) {
dispatch(onActiveDataChange({ activeData }));
}
}, [isDataLoading, dispatch, layerId]);We won't get rid of refs completely but it's less nested/complex than what suppressNextChartLoadGridRefreshRef tries to do maybe. What do you think?
There was a problem hiding this comment.
I like you idea! It makes sense to make it less complicated. Applied your suggestion here
ee99cb2 to
0c2ee06
Compare
There was a problem hiding this comment.
Nit
I've noticed that when you run the query, the "ES|QL Query Results" section is collapsed:
Screen.Recording.2026-05-05.at.16.39.23.mov
@teresaalvarezsoler, do you think we should do the same when the ES|QL query results section updates on date / filters change?
💚 Build Succeeded
Metrics [docs]Module Count
Async chunks
History
|
Summary
Fix #259082
How it was fixed
When the visualization finishes reloading (signaled by
dataLoading$→false), the editor re-runsgetSuggestionsfor the last submitted ES|QL query with current time/filters sodataGridAttrs(the results section) stays in sync with what the chart just used. Additionally,suppressNextChartLoadGridRefreshRefavoids a duplicate grid fetch right after submit.Before
Screen.Recording.2026-04-17.at.12.35.26.mov
Changing the time range or applying filters updated the visualization but did not refresh the “ES|QL query results” section.
After
Screen.Recording.2026-04-17.at.12.57.34.mov
Changing the time range or applying filters updates the visualization and refreshes the “ES|QL query results” section.
Checklist
release_note:*label is applied per the guidelinesbackport:*labels.