Skip to content

Commit

Permalink
add placeholder and validation with helpText to modelMemoryLimit field
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Nov 15, 2019
1 parent 2769cfc commit f8c50cf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import { JOB_ID_MAX_LENGTH } from '../../../../../../common/constants/validation';
import { Messages } from './messages';
import { JobType } from './job_type';
import { mmlUnitInvalidErrorMessage } from '../../hooks/use_create_analytics_form/reducer';

// based on code used by `ui/index_patterns` internally
// remove the space character from the list of illegal characters
Expand Down Expand Up @@ -79,6 +80,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
jobType,
loadingDepFieldOptions,
modelMemoryLimit,
modelMemoryLimitUnitValid,
sourceIndex,
sourceIndexNameEmpty,
sourceIndexNameValid,
Expand Down Expand Up @@ -113,6 +115,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
try {
const jobConfig = getJobConfigFromFormState(form);
delete jobConfig.dest;
delete jobConfig.model_memory_limit;
const resp = await ml.dataFrameAnalytics.estimateMemoryUsage(jobConfig);
setFormState({
modelMemoryLimit: resp.expected_memory_without_disk,
Expand Down Expand Up @@ -316,7 +319,7 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
placeholder={i18n.translate(
'xpack.ml.dataframe.analytics.create.sourceIndexPlaceholder',
{
defaultMessage: 'Choose a source index pattern or saved search.',
defaultMessage: 'Choose a source index pattern.',
}
)}
singleSelection={{ asPlainText: true }}
Expand Down Expand Up @@ -480,8 +483,14 @@ export const CreateAnalyticsForm: FC<CreateAnalyticsFormProps> = ({ actions, sta
label={i18n.translate('xpack.ml.dataframe.analytics.create.modelMemoryLimitLabel', {
defaultMessage: 'Model memory limit',
})}
helpText={!modelMemoryLimitUnitValid && mmlUnitInvalidErrorMessage}
>
<EuiFieldText
placeholder={
jobType !== undefined
? DEFAULT_MODEL_MEMORY_LIMIT[jobType]
: DEFAULT_MODEL_MEMORY_LIMIT.outlier_detection
}
disabled={isJobCreated}
value={modelMemoryLimit}
onChange={e => setFormState({ modelMemoryLimit: e.target.value })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,29 @@ import { isValidIndexName } from '../../../../../../common/util/es_utils';

import { Action, ACTION } from './actions';
import { getInitialState, getJobConfigFromFormState, State, JOB_TYPES } from './state';
import { isJobIdValid } from '../../../../../../common/util/job_utils';
import {
isJobIdValid,
validateModelMemoryLimitUnits,
} from '../../../../../../common/util/job_utils';
import { maxLengthValidator } from '../../../../../../common/util/validators';
import { JOB_ID_MAX_LENGTH } from '../../../../../../common/constants/validation';
import {
JOB_ID_MAX_LENGTH,
ALLOWED_DATA_UNITS,
} from '../../../../../../common/constants/validation';
import { getDependentVar, isRegressionAnalysis } from '../../../../common/analytics';

const mmlAllowedUnitsStr = `${ALLOWED_DATA_UNITS.slice(0, ALLOWED_DATA_UNITS.length - 1).join(
', '
)} or ${[...ALLOWED_DATA_UNITS].pop()}`;

export const mmlUnitInvalidErrorMessage = i18n.translate(
'xpack.ml.dataframe.analytics.create.modelMemoryUnitsInvalidError',
{
defaultMessage: 'Model memory limit data unit unrecognized. It must be {str}',
values: { str: mmlAllowedUnitsStr },
}
);

const getSourceIndexString = (state: State) => {
const { jobConfig } = state;

Expand Down Expand Up @@ -65,6 +83,10 @@ export const validateAdvancedEditor = (state: State): State => {
state.indexPatternsMap[destinationIndexName] !== undefined;
const mml = jobConfig.model_memory_limit;
const modelMemoryLimitEmpty = mml === '';
if (!modelMemoryLimitEmpty && mml !== undefined) {
const { valid } = validateModelMemoryLimitUnits(mml);
state.form.modelMemoryLimitUnitValid = valid;
}

let dependentVariableEmpty = false;
if (isRegressionAnalysis(jobConfig.analysis)) {
Expand Down Expand Up @@ -140,7 +162,15 @@ export const validateAdvancedEditor = (state: State): State => {
});
}

if (!state.form.modelMemoryLimitUnitValid) {
state.advancedEditorMessages.push({
error: mmlUnitInvalidErrorMessage,
message: '',
});
}

state.isValid =
state.form.modelMemoryLimitUnitValid &&
!jobIdEmpty &&
jobIdValid &&
!jobIdExists &&
Expand Down Expand Up @@ -174,7 +204,13 @@ const validateForm = (state: State): State => {
const dependentVariableEmpty = jobType === JOB_TYPES.REGRESSION && dependentVariable === '';
const modelMemoryLimitEmpty = modelMemoryLimit === '';

if (modelMemoryLimit !== undefined && modelMemoryLimit !== '') {
const { valid } = validateModelMemoryLimitUnits(modelMemoryLimit);
state.form.modelMemoryLimitUnitValid = valid;
}

state.isValid =
state.form.modelMemoryLimitUnitValid &&
!jobIdEmpty &&
jobIdValid &&
!jobIdExists &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export interface State {
jobIdValid: boolean;
jobType: AnalyticsJobType;
loadingDepFieldOptions: boolean;
modelMemoryLimit: string;
modelMemoryLimit: string | undefined;
modelMemoryLimitUnitValid: boolean;
sourceIndex: EsIndexName;
sourceIndexNameEmpty: boolean;
sourceIndexNameValid: boolean;
Expand Down Expand Up @@ -97,7 +98,8 @@ export const getInitialState = (): State => ({
jobIdValid: false,
jobType: undefined,
loadingDepFieldOptions: false,
modelMemoryLimit: DEFAULT_MODEL_MEMORY_LIMIT.regression,
modelMemoryLimit: undefined,
modelMemoryLimitUnitValid: true,
sourceIndex: '',
sourceIndexNameEmpty: true,
sourceIndexNameValid: false,
Expand Down

0 comments on commit f8c50cf

Please sign in to comment.