Skip to content

Commit

Permalink
[ML] Use indices options in anomaly detection job wizards (elastic#91830
Browse files Browse the repository at this point in the history
)

* [ML] WIP Datafeed preview refactor

* adding indices options to ad job creation searches

* update datafeed preview schema

* updating types

* recalculating wizard time range on JSON edit save

* updating endpoint docs

* fixing types

* more type fixes

* fixing missing runtime fields

* using isPopulatedObject

* adding indices options schema

* fixing test

* fixing schema

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
jgowdyelastic and kibanamachine committed Mar 16, 2021
1 parent f88b143 commit c85fbd4
Show file tree
Hide file tree
Showing 45 changed files with 490 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type Aggregation = Record<
}
>;

interface IndicesOptions {
export interface IndicesOptions {
expand_wildcards?: 'all' | 'open' | 'closed' | 'hidden' | 'none';
ignore_unavailable?: boolean;
allow_no_indices?: boolean;
Expand Down
19 changes: 18 additions & 1 deletion x-pack/plugins/ml/common/types/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* 2.0.
*/

import { Job, JobStats } from './anomaly_detection_jobs';
import { Job, JobStats, IndicesOptions } from './anomaly_detection_jobs';
import { RuntimeMappings } from './fields';
import { ES_AGGREGATION } from '../constants/aggregation_types';

export interface MlJobsResponse {
jobs: Job[];
Expand All @@ -23,3 +25,18 @@ export interface JobsExistResponse {
isGroup: boolean;
};
}

export interface BucketSpanEstimatorData {
aggTypes: Array<ES_AGGREGATION | null>;
duration: {
start: number;
end: number;
};
fields: Array<string | null>;
index: string;
query: any;
splitField: string | undefined;
timeField: string | undefined;
runtimeMappings: RuntimeMappings | undefined;
indicesOptions: IndicesOptions | undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function chartLoaderProvider(mlResultsService: MlResultsService) {
job.data_description.time_field,
job.data_counts.earliest_record_timestamp,
job.data_counts.latest_record_timestamp,
intervalMs
intervalMs,
job.datafeed_config.indices_options
);
if (resp.error !== undefined) {
throw resp.error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import memoizeOne from 'memoize-one';
import { isEqual } from 'lodash';
import { IndexPatternTitle } from '../../../../../../common/types/kibana';
import { IndicesOptions } from '../../../../../../common/types/anomaly_detection_jobs';
import {
Field,
SplitField,
Expand Down Expand Up @@ -56,7 +57,8 @@ export class ChartLoader {
splitField: SplitField,
splitFieldValue: SplitFieldValue,
intervalMs: number,
runtimeMappings: RuntimeMappings | null
runtimeMappings: RuntimeMappings | null,
indicesOptions?: IndicesOptions
): Promise<LineChartData> {
if (this._timeFieldName !== '') {
if (aggFieldPairsCanBeCharted(aggFieldPairs) === false) {
Expand All @@ -77,7 +79,8 @@ export class ChartLoader {
aggFieldPairNames,
splitFieldName,
splitFieldValue,
runtimeMappings ?? undefined
runtimeMappings ?? undefined,
indicesOptions
);

return resp.results;
Expand All @@ -91,7 +94,8 @@ export class ChartLoader {
aggFieldPairs: AggFieldPair[],
splitField: SplitField,
intervalMs: number,
runtimeMappings: RuntimeMappings | null
runtimeMappings: RuntimeMappings | null,
indicesOptions?: IndicesOptions
): Promise<LineChartData> {
if (this._timeFieldName !== '') {
if (aggFieldPairsCanBeCharted(aggFieldPairs) === false) {
Expand All @@ -111,7 +115,8 @@ export class ChartLoader {
this._query,
aggFieldPairNames,
splitFieldName,
runtimeMappings ?? undefined
runtimeMappings ?? undefined,
indicesOptions
);

return resp.results;
Expand All @@ -122,7 +127,8 @@ export class ChartLoader {
async loadEventRateChart(
start: number,
end: number,
intervalMs: number
intervalMs: number,
indicesOptions?: IndicesOptions
): Promise<LineChartPoint[]> {
if (this._timeFieldName !== '') {
const resp = await getEventRateData(
Expand All @@ -131,7 +137,8 @@ export class ChartLoader {
this._timeFieldName,
start,
end,
intervalMs * 3
intervalMs * 3,
indicesOptions
);
if (resp.error !== undefined) {
throw resp.error;
Expand All @@ -147,14 +154,16 @@ export class ChartLoader {

async loadFieldExampleValues(
field: Field,
runtimeMappings: RuntimeMappings | null
runtimeMappings: RuntimeMappings | null,
indicesOptions?: IndicesOptions
): Promise<string[]> {
const { results } = await getCategoryFields(
this._indexPatternTitle,
field.name,
10,
this._query,
runtimeMappings ?? undefined
runtimeMappings ?? undefined,
indicesOptions
);
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { get } from 'lodash';

import { ml } from '../../../../services/ml_api_service';
import { RuntimeMappings } from '../../../../../../common/types/fields';
import { IndicesOptions } from '../../../../../../common/types/anomaly_detection_jobs';

interface CategoryResults {
success: boolean;
Expand All @@ -20,7 +21,8 @@ export function getCategoryFields(
fieldName: string,
size: number,
query: any,
runtimeMappings?: RuntimeMappings
runtimeMappings?: RuntimeMappings,
indicesOptions?: IndicesOptions
): Promise<CategoryResults> {
return new Promise((resolve, reject) => {
ml.esSearch({
Expand All @@ -38,6 +40,7 @@ export function getCategoryFields(
},
...(runtimeMappings !== undefined ? { runtime_mappings: runtimeMappings } : {}),
},
...(indicesOptions ?? {}),
})
.then((resp: any) => {
const catFields = get(resp, ['aggregations', 'catFields', 'buckets'], []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,13 @@ export class AdvancedJobCreator extends JobCreator {
// load the start and end times for the selected index
// and apply them to the job creator
public async autoSetTimeRange() {
try {
const { start, end } = await ml.getTimeFieldRange({
index: this._indexPatternTitle,
timeFieldName: this.timeFieldName,
query: this.query,
});
this.setTimeRange(start.epoch, end.epoch);
} catch (error) {
throw Error(error);
}
const { start, end } = await ml.getTimeFieldRange({
index: this._indexPatternTitle,
timeFieldName: this.timeFieldName,
query: this.query,
indicesOptions: this.datafeedConfig.indices_options,
});
this.setTimeRange(start.epoch, end.epoch);
}

public cloneFromExistingJob(job: Job, datafeed: Datafeed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class CategorizationExamplesLoader {
this._jobCreator.start,
this._jobCreator.end,
analyzer,
this._jobCreator.runtimeMappings ?? undefined
this._jobCreator.runtimeMappings ?? undefined,
this._jobCreator.datafeedConfig.indices_options
);
return resp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ export class ResultsLoader {
if (this._jobCreator.splitField !== null) {
const fieldValues = await this._chartLoader.loadFieldExampleValues(
this._jobCreator.splitField,
this._jobCreator.runtimeMappings
this._jobCreator.runtimeMappings,
this._jobCreator.datafeedConfig.indices_options
);
if (fieldValues.length > 0) {
this._detectorSplitFieldFilters = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { CombinedJob, Datafeed } from '../../../../../../../../common/types/anom
import { ML_EDITOR_MODE, MLJobEditor } from '../../../../../jobs_list/components/ml_job_editor';
import { isValidJson } from '../../../../../../../../common/util/validation_utils';
import { JobCreatorContext } from '../../job_creator_context';
import { isAdvancedJobCreator } from '../../../../common/job_creator';
import { DatafeedPreview } from '../datafeed_preview_flyout';
import { useToastNotificationService } from '../../../../../../services/toast_notification_service';

export enum EDITOR_MODE {
HIDDEN,
Expand All @@ -40,6 +42,7 @@ interface Props {
}
export const JsonEditorFlyout: FC<Props> = ({ isDisabled, jobEditorMode, datafeedEditorMode }) => {
const { jobCreator, jobCreatorUpdate, jobCreatorUpdated } = useContext(JobCreatorContext);
const { displayErrorToast } = useToastNotificationService();
const [showJsonFlyout, setShowJsonFlyout] = useState(false);
const [showChangedIndicesWarning, setShowChangedIndicesWarning] = useState(false);

Expand Down Expand Up @@ -120,10 +123,23 @@ export const JsonEditorFlyout: FC<Props> = ({ isDisabled, jobEditorMode, datafee
setSaveable(valid);
}

function onSave() {
async function onSave() {
const jobConfig = JSON.parse(jobConfigString);
const datafeedConfig = JSON.parse(collapseLiteralStrings(datafeedConfigString));
jobCreator.cloneFromExistingJob(jobConfig, datafeedConfig);
if (isAdvancedJobCreator(jobCreator)) {
try {
await jobCreator.autoSetTimeRange();
} catch (error) {
const title = i18n.translate(
'xpack.ml.newJob.wizard.jsonFlyout.autoSetJobCreatorTimeRange.error',
{
defaultMessage: `Error retrieving beginning and end times of index`,
}
);
displayErrorToast(error, title);
}
}
jobCreatorUpdate();
setShowJsonFlyout(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { useContext, useState } from 'react';

import { JobCreatorContext } from '../../../job_creator_context';
import { EVENT_RATE_FIELD_ID } from '../../../../../../../../../common/types/fields';
import { BucketSpanEstimatorData } from '../../../../../../../../../common/types/job_service';
import {
isMultiMetricJobCreator,
isPopulationJobCreator,
isAdvancedJobCreator,
} from '../../../../../common/job_creator';
import { ml, BucketSpanEstimatorData } from '../../../../../../../services/ml_api_service';
import { ml } from '../../../../../../../services/ml_api_service';
import { useMlContext } from '../../../../../../../contexts/ml';
import { getToastNotificationService } from '../../../../../../../services/toast_notification_service';

Expand All @@ -41,6 +42,7 @@ export function useEstimateBucketSpan() {
splitField: undefined,
timeField: mlContext.currentIndexPattern.timeFieldName,
runtimeMappings: jobCreator.runtimeMappings ?? undefined,
indicesOptions: jobCreator.datafeedConfig.indices_options,
};

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const CategorizationDetectorsSummary: FC = () => {
const resp = await chartLoader.loadEventRateChart(
jobCreator.start,
jobCreator.end,
chartInterval.getInterval().asMilliseconds()
chartInterval.getInterval().asMilliseconds(),
jobCreator.datafeedConfig.indices_options
);
setEventRateChartData(resp);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export const MultiMetricDetectors: FC<Props> = ({ setIsValid }) => {
useEffect(() => {
if (splitField !== null) {
chartLoader
.loadFieldExampleValues(splitField, jobCreator.runtimeMappings)
.loadFieldExampleValues(
splitField,
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
)
.then(setFieldValues)
.catch((error) => {
getToastNotificationService().displayErrorToast(error);
Expand Down Expand Up @@ -140,7 +144,8 @@ export const MultiMetricDetectors: FC<Props> = ({ setIsValid }) => {
jobCreator.splitField,
fieldValues.length > 0 ? fieldValues[0] : null,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);
setLineChartsData(resp);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const MultiMetricDetectorsSummary: FC = () => {
try {
const tempFieldValues = await chartLoader.loadFieldExampleValues(
jobCreator.splitField,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);
setFieldValues(tempFieldValues);
} catch (error) {
Expand Down Expand Up @@ -76,7 +77,8 @@ export const MultiMetricDetectorsSummary: FC = () => {
jobCreator.splitField,
fieldValues.length > 0 ? fieldValues[0] : null,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);
setLineChartsData(resp);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export const PopulationDetectors: FC<Props> = ({ setIsValid }) => {
aggFieldPairList,
jobCreator.splitField,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);

setLineChartsData(resp);
Expand All @@ -180,7 +181,11 @@ export const PopulationDetectors: FC<Props> = ({ setIsValid }) => {
(async (index: number, field: Field) => {
return {
index,
fields: await chartLoader.loadFieldExampleValues(field, jobCreator.runtimeMappings),
fields: await chartLoader.loadFieldExampleValues(
field,
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
),
};
})(i, af.by.field)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export const PopulationDetectorsSummary: FC = () => {
aggFieldPairList,
jobCreator.splitField,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);

setLineChartsData(resp);
Expand All @@ -98,7 +99,11 @@ export const PopulationDetectorsSummary: FC = () => {
(async (index: number, field: Field) => {
return {
index,
fields: await chartLoader.loadFieldExampleValues(field, jobCreator.runtimeMappings),
fields: await chartLoader.loadFieldExampleValues(
field,
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
),
};
})(i, af.by.field)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export const SingleMetricDetectors: FC<Props> = ({ setIsValid }) => {
null,
null,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);
if (resp[DTR_IDX] !== undefined) {
setLineChartData(resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export const SingleMetricDetectorsSummary: FC = () => {
null,
null,
cs.intervalMs,
jobCreator.runtimeMappings
jobCreator.runtimeMappings,
jobCreator.datafeedConfig.indices_options
);
if (resp[DTR_IDX] !== undefined) {
setLineChartData(resp);
Expand Down
Loading

0 comments on commit c85fbd4

Please sign in to comment.