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

[8.11] [ML] AIOps: Fix Data View runtime fields support in Change point detection UI (#168249) #168491

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
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 @@ -9,12 +9,17 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { type QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { i18n } from '@kbn/i18n';
import { isDefined } from '@kbn/ml-is-defined';
import type {
MappingRuntimeFields,
SearchRequest,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { useReload } from '../../hooks/use_reload';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import {
ChangePointAnnotation,
ChangePointDetectionRequestParams,
FieldConfig,
useChangePointDetectionControlsContext,
} from './change_point_detection_context';
import { useDataSource } from '../../hooks/use_data_source';
import { useCancellableSearch } from '../../hooks/use_cancellable_search';
Expand All @@ -37,8 +42,9 @@ interface RequestOptions {

function getChangePointDetectionRequestBody(
{ index, fn, metricField, splitField, timeInterval, timeField, afterKey }: RequestOptions,
query: QueryDslQueryContainer
) {
query: QueryDslQueryContainer,
runtimeMappings: MappingRuntimeFields
): SearchRequest {
const timeSeriesAgg = {
over_time: {
date_histogram: {
Expand Down Expand Up @@ -98,15 +104,14 @@ function getChangePointDetectionRequestBody(
: timeSeriesAgg;

return {
params: {
index,
size: 0,
body: {
...(query ? { query } : {}),
aggregations,
},
index,
size: 0,
body: {
...(query ? { query } : {}),
...(runtimeMappings ? { runtime_mappings: runtimeMappings } : {}),
aggregations,
},
};
} as SearchRequest;
}

export function useChangePointResults(
Expand All @@ -120,7 +125,7 @@ export function useChangePointResults(
} = useAiopsAppContext();

const { dataView } = useDataSource();

const { splitFieldsOptions, metricFieldOptions } = useChangePointDetectionControlsContext();
const { refreshTimestamp: refresh } = useReload();

const [results, setResults] = useState<ChangePointAnnotation[]>([]);
Expand Down Expand Up @@ -152,7 +157,23 @@ export function useChangePointResults(
return;
}

const requestPayload = getChangePointDetectionRequestBody(
const metricFieldDV = metricFieldOptions.find(
(option) => option.name === fieldConfig.metricField
);
const splitFieldDV = splitFieldsOptions.find(
(option) => option.name === fieldConfig.splitField
);

const runtimeMappings = {
...(metricFieldDV?.isRuntimeField
? { [metricFieldDV.name]: metricFieldDV.runtimeField! }
: {}),
...(splitFieldDV?.isRuntimeField
? { [splitFieldDV.name]: splitFieldDV.runtimeField! }
: {}),
} as MappingRuntimeFields;

const requestPayload: SearchRequest = getChangePointDetectionRequestBody(
{
index: dataView.getIndexPattern(),
fn: fieldConfig.fn,
Expand All @@ -162,21 +183,22 @@ export function useChangePointResults(
splitField: fieldConfig.splitField,
afterKey,
},
query
query,
runtimeMappings
);

const result = await runRequest<
typeof requestPayload,
{ params: SearchRequest },
{ rawResponse: ChangePointAggResponse }
>(requestPayload);
>({ params: requestPayload });

if (result === null) {
setProgress(null);
return;
}

const isFetchCompleted = !(
result.rawResponse.aggregations?.groupings?.after_key?.splitFieldTerm &&
isDefined(result.rawResponse.aggregations?.groupings?.after_key?.splitFieldTerm) &&
pageNumber < totalAggPages
);

Expand Down Expand Up @@ -227,11 +249,11 @@ export function useChangePointResults(

if (
!isFetchCompleted &&
result.rawResponse.aggregations?.groupings?.after_key?.splitFieldTerm
isDefined(result.rawResponse.aggregations?.groupings?.after_key?.splitFieldTerm)
) {
await fetchResults(
pageNumber + 1,
result.rawResponse.aggregations.groupings.after_key.splitFieldTerm
result.rawResponse.aggregations.groupings.after_key!.splitFieldTerm
);
}
} catch (e) {
Expand All @@ -243,17 +265,19 @@ export function useChangePointResults(
}
},
[
runRequest,
requestParams.interval,
requestParams.changePointType,
isSingleMetric,
totalAggPages,
dataView,
fieldConfig.fn,
fieldConfig.metricField,
fieldConfig.splitField,
requestParams.interval,
requestParams.changePointType,
query,
dataView,
totalAggPages,
metricFieldOptions,
splitFieldsOptions,
runRequest,
toasts,
isSingleMetric,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
SearchResponseBody,
} from '@elastic/elasticsearch/lib/api/types';
import usePrevious from 'react-use/lib/usePrevious';
import { useChangePointDetectionControlsContext } from './change_point_detection_context';
import { useCancellableSearch } from '../../hooks/use_cancellable_search';
import { useDataSource } from '../../hooks/use_data_source';

Expand All @@ -25,11 +26,19 @@ export function useSplitFieldCardinality(
query: QueryDslQueryContainer
) {
const prevSplitField = usePrevious(splitField);
const { splitFieldsOptions } = useChangePointDetectionControlsContext();

const [cardinality, setCardinality] = useState<number | null>(null);
const { dataView } = useDataSource();

const requestPayload = useMemo(() => {
const optionDefinition = splitFieldsOptions.find((option) => option.name === splitField);
let runtimeMappings = {};
if (optionDefinition?.isRuntimeField) {
runtimeMappings = {
runtime_mappings: { [optionDefinition.name]: optionDefinition.runtimeField },
};
}
return {
params: {
index: dataView.getIndexPattern(),
Expand All @@ -43,10 +52,11 @@ export function useSplitFieldCardinality(
},
},
},
...runtimeMappings,
},
},
};
}, [splitField, dataView, query]);
}, [splitField, dataView, query, splitFieldsOptions]);

const { runRequest: getSplitFieldCardinality, cancelRequest } = useCancellableSearch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { useTimefilter } from '@kbn/ml-date-picker';
import { css } from '@emotion/react';
import useObservable from 'react-use/lib/useObservable';
import { ReloadContextProvider } from '../hooks/use_reload';
import type {
ChangePointAnnotation,
ChangePointDetectionRequestParams,
import {
type ChangePointAnnotation,
ChangePointDetectionControlsContextProvider,
type ChangePointDetectionRequestParams,
} from '../components/change_point_detection/change_point_detection_context';
import type {
EmbeddableChangePointChartInput,
Expand Down Expand Up @@ -82,22 +83,24 @@ export const EmbeddableInputTracker: FC<EmbeddableInputTrackerProps> = ({
return (
<ReloadContextProvider reload$={resultObservable$}>
<DataSourceContextProvider dataViewId={input.dataViewId}>
<FilterQueryContextProvider timeRange={input.timeRange}>
<ChartGridEmbeddableWrapper
timeRange={input.timeRange}
fn={input.fn}
metricField={input.metricField}
splitField={input.splitField}
maxSeriesToPlot={input.maxSeriesToPlot}
dataViewId={input.dataViewId}
partitions={input.partitions}
onLoading={onLoading}
onRenderComplete={onRenderComplete}
onError={onError}
onChange={input.onChange}
emptyState={input.emptyState}
/>
</FilterQueryContextProvider>
<ChangePointDetectionControlsContextProvider>
<FilterQueryContextProvider timeRange={input.timeRange}>
<ChartGridEmbeddableWrapper
timeRange={input.timeRange}
fn={input.fn}
metricField={input.metricField}
splitField={input.splitField}
maxSeriesToPlot={input.maxSeriesToPlot}
dataViewId={input.dataViewId}
partitions={input.partitions}
onLoading={onLoading}
onRenderComplete={onRenderComplete}
onError={onError}
onChange={input.onChange}
emptyState={input.emptyState}
/>
</FilterQueryContextProvider>
</ChangePointDetectionControlsContextProvider>
</DataSourceContextProvider>
</ReloadContextProvider>
);
Expand Down