Skip to content

Commit

Permalink
[ML] populate excludesOptions on the first update, skip setting mml o…
Browse files Browse the repository at this point in the history
…n the fist update
  • Loading branch information
darnautov committed Mar 13, 2020
1 parent 6b7d0bf commit 5939ddc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { debounce } from 'lodash';

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import useUpdateEffect from 'react-use/lib/useUpdateEffect';

import { useMlKibana } from '../../../../../contexts/kibana';
import { ml } from '../../../../../services/ml_api_service';
Expand Down Expand Up @@ -58,6 +57,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
const { form, indexPatternsMap, isAdvancedEditorEnabled, isJobCreated, requestMessages } = state;

const forceInput = useRef<HTMLInputElement | null>(null);
const firstUpdate = useRef<boolean>(true);

const {
createIndexPattern,
Expand Down Expand Up @@ -148,6 +148,10 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
};

const debouncedGetExplainData = debounce(async () => {
const shouldUpdateModelMemoryLimit = !firstUpdate.current || !modelMemoryLimit;
if (firstUpdate.current) {
firstUpdate.current = false;
}
// Reset if sourceIndex or jobType changes (jobType requires dependent_variable to be set -
// which won't be the case if switching from outlier detection)
if (previousSourceIndex !== sourceIndex || previousJobType !== jobType) {
Expand All @@ -166,10 +170,12 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
);
const expectedMemoryWithoutDisk = resp.memory_estimation?.expected_memory_without_disk;

// If sourceIndex has changed load analysis field options again
if (previousSourceIndex !== sourceIndex || previousJobType !== jobType) {
if (shouldUpdateModelMemoryLimit) {
setEstimatedModelMemoryLimit(expectedMemoryWithoutDisk);
}

// If sourceIndex has changed load analysis field options again
if (previousSourceIndex !== sourceIndex || previousJobType !== jobType) {
const analyzedFieldsOptions: EuiComboBoxOptionOption[] = [];

if (resp.field_selection) {
Expand All @@ -181,15 +187,15 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
}

setFormState({
...(!modelMemoryLimit ? { modelMemoryLimit: expectedMemoryWithoutDisk } : {}),
...(shouldUpdateModelMemoryLimit ? { modelMemoryLimit: expectedMemoryWithoutDisk } : {}),
excludesOptions: analyzedFieldsOptions,
loadingFieldOptions: false,
fieldOptionsFetchFail: false,
maxDistinctValuesError: undefined,
});
} else {
setFormState({
...(!modelMemoryLimit ? { modelMemoryLimit: expectedMemoryWithoutDisk } : {}),
...(shouldUpdateModelMemoryLimit ? { modelMemoryLimit: expectedMemoryWithoutDisk } : {}),
});
}
} catch (e) {
Expand All @@ -211,7 +217,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
fieldOptionsFetchFail: true,
maxDistinctValuesError: errorMessage,
loadingFieldOptions: false,
modelMemoryLimit: fallbackModelMemoryLimit,
...(shouldUpdateModelMemoryLimit ? { modelMemoryLimit: fallbackModelMemoryLimit } : {}),
});
}
}, 400);
Expand Down Expand Up @@ -316,7 +322,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
}
}, [sourceIndex, jobType, sourceIndexNameEmpty]);

useUpdateEffect(() => {
useEffect(() => {
const hasBasicRequiredFields =
jobType !== undefined && sourceIndex !== '' && sourceIndexNameValid === true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export const useCreateAnalyticsForm = (): CreateAnalyticsFormProps => {
switchToAdvancedEditor();
} else {
setFormState(getCloneFormStateFromJobConfig(config));
setEstimatedModelMemoryLimit(config.model_memory_limit);
}

dispatch({ type: ACTION.SET_JOB_CLONE, cloneJob });
Expand Down

0 comments on commit 5939ddc

Please sign in to comment.