From 2bd9db08f1c475d430f539a624b599b2782116dd Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Tue, 16 Aug 2022 14:22:20 -0500 Subject: [PATCH] Set probability to required --- .../common/types/field_stats.ts | 2 +- .../hooks/use_data_visualizer_grid_data.ts | 3 +- .../hooks/use_field_stats.ts | 3 +- .../hooks/use_overall_stats.ts | 3 +- .../requests/get_document_stats.ts | 36 ++++++++++++++----- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/data_visualizer/common/types/field_stats.ts b/x-pack/plugins/data_visualizer/common/types/field_stats.ts index 976975847dd64..b035cd8f0476c 100644 --- a/x-pack/plugins/data_visualizer/common/types/field_stats.ts +++ b/x-pack/plugins/data_visualizer/common/types/field_stats.ts @@ -95,7 +95,7 @@ export interface DocumentCountStats { timeRangeEarliest?: number; timeRangeLatest?: number; totalCount: number; - probability?: number | null; + probability: number | null; took?: number; randomlySampled?: boolean; } diff --git a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_data_visualizer_grid_data.ts b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_data_visualizer_grid_data.ts index e4321d5018bd1..c6791fee7f66a 100644 --- a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_data_visualizer_grid_data.ts +++ b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_data_visualizer_grid_data.ts @@ -276,11 +276,10 @@ export const useDataVisualizerGridData = ( fieldStatsRequest, configsWithoutStats, dataVisualizerListState, - // @todo: use this in a function for maintainability (dataVisualizerListState.probability === null ? overallStats?.documentCountStats?.probability : dataVisualizerListState.probability) ?? 1, - overallStats?.documentCountStats?.totalCount + overallStats?.documentCountStats?.totalCount ?? 0 ); const combinedProgress = useMemo( diff --git a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_field_stats.ts b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_field_stats.ts index 142f05d66884d..c74a1c335fee0 100644 --- a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_field_stats.ts +++ b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_field_stats.ts @@ -170,7 +170,8 @@ export function useFieldStatsSearchStrategy( }, }, maxExamples: MAX_EXAMPLES_DEFAULT, - samplingProbability, + // @todo get correct sampling prob for indices without time field + samplingProbability: samplingProbability ?? 1, browserSessionSeed: searchStrategyParams.browserSessionSeed, totalCount, }; 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 36ea4bbcb945a..d14bf66327755 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 @@ -187,7 +187,7 @@ export function useOverallStats { const aggregatableOverallStatsResp: AggregatableFieldOverallStats[] = []; const nonAggregatableOverallStatsResp: NonAggregatableFieldOverallStats[] = []; - // const documentCountStats = value[0] as DocumentCountStats; value.forEach((resp, idx) => { if (isAggregatableFieldOverallStats(resp)) { diff --git a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/search_strategy/requests/get_document_stats.ts b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/search_strategy/requests/get_document_stats.ts index 7787cdf970367..efd2b7da1ac7b 100644 --- a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/search_strategy/requests/get_document_stats.ts +++ b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/search_strategy/requests/get_document_stats.ts @@ -86,7 +86,9 @@ export const getDocumentCountStats = async ( fieldsToFetch, } = params; - const result = { randomlySampled: false, took: 0, totalCount: 0 }; + // Probability = 1 represents no sampling + const result = { randomlySampled: false, took: 0, totalCount: 0, probability: 1 }; + const filterCriteria = buildBaseFilterCriteria(timeFieldName, earliestMs, latestMs, searchQuery); const query = { @@ -121,21 +123,23 @@ export const getDocumentCountStats = async ( }, }); + const hasTimeField = + Array.isArray(fieldsToFetch) && + timeFieldName !== undefined && + intervalMs !== undefined && + intervalMs > 0; + const getSearchParams = (aggregations: unknown) => ({ index, body: { query, - ...(Array.isArray(fieldsToFetch) && - timeFieldName !== undefined && - intervalMs !== undefined && - intervalMs > 0 - ? { aggs: aggregations } - : {}), + ...(hasTimeField ? { aggs: aggregations } : {}), ...(isPopulatedObject(runtimeFieldMap) ? { runtime_mappings: runtimeFieldMap } : {}), }, - track_total_hits: false, + track_total_hits: !hasTimeField, size: 0, }); + const firstResp = await search .search( { @@ -152,6 +156,19 @@ export const getDocumentCountStats = async ( )}` ); } + + if (!hasTimeField && isDefined(firstResp.rawResponse)) { + return { + ...result, + took: firstResp.rawResponse.took, + totalCount: + (typeof firstResp.rawResponse.hits?.total === 'number' + ? firstResp.rawResponse.hits?.total + : firstResp.rawResponse.hits?.total?.value) ?? 0, + probability: 1, + }; + } + if (isDefined(probability)) { return { ...result, @@ -184,6 +201,7 @@ export const getDocumentCountStats = async ( searchOptions ) .toPromise(); + return { ...result, randomlySampled: false, @@ -221,7 +239,7 @@ export const processDocumentCountStats = ( body: estypes.SearchResponse | undefined, params: OverallStatsSearchStrategyParams, randomlySampled = false -): DocumentCountStats | undefined => { +): Omit | undefined => { if (!body) return undefined; let totalCount = 0;