From 3ca8d2b5f2c10ad5a0aa3d9d8bb81161c0d9fd5d Mon Sep 17 00:00:00 2001 From: "Quynh Nguyen (Quinn)" <43350163+qn895@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:29:51 -0500 Subject: [PATCH] [ML] Fix runtime fields occasionally cause redundant refetch of data in Index data visualizer (#181853) ## Summary The runtime fields object occasionally cause redundant update to the hook, which triggers a refetch of data in Index data visualizer. This PR fixes that. ### Checklist Delete any items that are not applicable to this PR. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) (cherry picked from commit 7d94898976a335fd36fc30e097d238a815e56b2c) --- .../hooks/use_overall_stats.ts | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts index 599839f3aa78e..cb72592742d44 100644 --- a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts +++ b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts @@ -19,6 +19,7 @@ import { getProcessedFields } from '@kbn/ml-data-grid'; import { buildBaseFilterCriteria } from '@kbn/ml-query-utils'; import { isDefined } from '@kbn/ml-is-defined'; import type { FieldSpec } from '@kbn/data-views-plugin/common'; +import type { MappingRuntimeFields } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { useDataVisualizerKibana } from '../../kibana_context'; import type { AggregatableFieldOverallStats, @@ -51,6 +52,18 @@ import { fetchDataWithTimeout, rateLimitingForkJoin, } from '../search_strategy/requests/fetch_utils'; + +const getPopulatedFieldsInIndex = ( + populatedFieldsInIndexWithoutRuntimeFields: Set | undefined | null, + runtimeFieldMap: MappingRuntimeFields | undefined +): Set | undefined | null => { + if (!populatedFieldsInIndexWithoutRuntimeFields) return undefined; + const runtimeFields = runtimeFieldMap ? Object.keys(runtimeFieldMap) : undefined; + return runtimeFields && runtimeFields?.length > 0 + ? new Set([...Array.from(populatedFieldsInIndexWithoutRuntimeFields), ...runtimeFields]) + : populatedFieldsInIndexWithoutRuntimeFields; +}; + export function useOverallStats( esql = false, searchStrategyParams: TParams | undefined, @@ -68,7 +81,7 @@ export function useOverallStats(getDefaultPageState().overallStats); - const [populatedFieldsInIndex, setPopulatedFieldsInIndex] = useState< + const [populatedFieldsInIndexWithoutRuntimeFields, setPopulatedFieldsInIndex] = useState< | Set // request to fields caps has not been made yet | undefined @@ -92,10 +105,9 @@ export function useOverallStats { populatedFieldsAbortCtrl.current.abort(); @@ -131,15 +143,7 @@ export function useOverallStats field.name), - // Field caps API don't know about runtime fields - // so by default we expect runtime fields to be populated - // so we can later check as needed - ...Object.keys(runtimeFieldMap ?? {}), - ]) - ); + setPopulatedFieldsInIndex(new Set([...nonEmptyFields.map((field) => field.name)])); } else { setPopulatedFieldsInIndex(null); } @@ -154,25 +158,33 @@ export function useOverallStats { try { searchSubscription$.current?.unsubscribe(); abortCtrl.current.abort(); abortCtrl.current = new AbortController(); - if (!searchStrategyParams || lastRefresh === 0 || populatedFieldsInIndex === undefined) { + if ( + !searchStrategyParams || + lastRefresh === 0 || + populatedFieldsInIndexWithoutRuntimeFields === undefined + ) { return; } + const populatedFieldsInIndex = getPopulatedFieldsInIndex( + populatedFieldsInIndexWithoutRuntimeFields, + searchStrategyParams.runtimeFieldMap + ); + setFetchState({ ...getInitialProgress(), isRunning: true, @@ -391,7 +403,14 @@ export function useOverallStats { searchSubscription$.current?.unsubscribe();