Skip to content

Commit

Permalink
Set probability to required
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Aug 16, 2022
1 parent e4f786e commit 2bd9db0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/data_visualizer/common/types/field_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface DocumentCountStats {
timeRangeEarliest?: number;
timeRangeLatest?: number;
totalCount: number;
probability?: number | null;
probability: number | null;
took?: number;
randomlySampled?: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function useOverallStats<TParams extends OverallStatsSearchStrategyParams
index,
searchQuery,
aggregatableFieldsChunk,
documentCountStats.probability,
documentCountStats.probability ?? 1,
documentCountStats.totalCount,
browserSessionSeed,
timeFieldName,
Expand Down Expand Up @@ -217,7 +217,6 @@ export function useOverallStats<TParams extends OverallStatsSearchStrategyParams
next: (value) => {
const aggregatableOverallStatsResp: AggregatableFieldOverallStats[] = [];
const nonAggregatableOverallStatsResp: NonAggregatableFieldOverallStats[] = [];
// const documentCountStats = value[0] as DocumentCountStats;

value.forEach((resp, idx) => {
if (isAggregatableFieldOverallStats(resp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
{
Expand All @@ -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,
Expand Down Expand Up @@ -184,6 +201,7 @@ export const getDocumentCountStats = async (
searchOptions
)
.toPromise();

return {
...result,
randomlySampled: false,
Expand Down Expand Up @@ -221,7 +239,7 @@ export const processDocumentCountStats = (
body: estypes.SearchResponse | undefined,
params: OverallStatsSearchStrategyParams,
randomlySampled = false
): DocumentCountStats | undefined => {
): Omit<DocumentCountStats, 'probability'> | undefined => {
if (!body) return undefined;

let totalCount = 0;
Expand Down

0 comments on commit 2bd9db0

Please sign in to comment.