Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fixes data drift numeric fields not showing correctly #172504

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { isDefined } from '@kbn/ml-is-defined';
import { computeChi2PValue, type Histogram } from '@kbn/ml-chi2test';
import { mapAndFlattenFilters } from '@kbn/data-plugin/public';

import type { AggregationsRangeBucketKeys } from '@elastic/elasticsearch/lib/api/types';
import type { AggregationsMultiTermsBucketKeys } from '@elastic/elasticsearch/lib/api/types';
import { createMergedEsQuery } from '../index_data_visualizer/utils/saved_search_utils';
import { useDataVisualizerKibana } from '../kibana_context';

Expand Down Expand Up @@ -459,12 +459,19 @@ const fetchComparisonDriftedData = async ({
);

const fieldsWithNoOverlap = new Set<string>();
const rangesAggs = rangesResp?.aggregations?.sample
? rangesResp.aggregations.sample
: rangesResp?.aggregations;
for (const { field } of fields) {
if (rangesResp.aggregations[`${field}_ranges`]) {
const buckets = rangesResp.aggregations[`${field}_ranges`]
.buckets as AggregationsRangeBucketKeys[];
if (
isPopulatedObject<
string,
estypes.AggregationsMultiBucketAggregateBase<AggregationsMultiTermsBucketKeys>
>(rangesAggs, [`${field}_ranges`])
) {
const buckets = rangesAggs[`${field}_ranges`].buckets;

if (buckets) {
if (Array.isArray(buckets)) {
const totalSumOfAllBuckets = buckets.reduce((acc, bucket) => acc + bucket.doc_count, 0);

const fractions = buckets.map((bucket) => ({
Expand All @@ -475,7 +482,7 @@ const fetchComparisonDriftedData = async ({
if (totalSumOfAllBuckets > 0) {
driftedRequestAggs[`${field}_ks_test`] = {
bucket_count_ks_test: {
buckets_path: `${field}_ranges>_count`,
buckets_path: `${field}_ranges > _count`,
alternative: ['two_sided'],
...(totalSumOfAllBuckets > 0
? { fractions: fractions.map((bucket) => Number(bucket.fraction.toFixed(3))) }
Expand Down Expand Up @@ -870,6 +877,7 @@ export const useFetchDataComparisonResult = (
signal,
}),
});

if (isReturnedError(driftedRespAggs)) {
setResult({
data: undefined,
Expand Down
Loading