-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[ML] Adding basic job validation #41459
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
Merged
jgowdyelastic
merged 9 commits into
elastic:feature-new-ml-job-wizards-new
from
jgowdyelastic:adding-basic-validation
Jul 19, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7b7a63e
[ML] Adding basic job validation
jgowdyelastic e337a79
removing observable
jgowdyelastic 2fef328
small refactor
jgowdyelastic d460ea6
comments and refactors
jgowdyelastic 32d0731
disabling create job button
jgowdyelastic 71b00c7
adding duplicate job and group id checks
jgowdyelastic ea2c043
disabling next on invalid wizard step
jgowdyelastic 2fd810e
changes based on review
jgowdyelastic e1db016
removing unused includes
jgowdyelastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_validator/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| export { JobValidator, Validation, BasicValidations, ValidationSummary } from './job_validator'; |
134 changes: 134 additions & 0 deletions
134
x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_validator/job_validator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { basicJobValidation } from '../../../../../common/util/job_utils'; | ||
| import { newJobLimits } from '../../../new_job/utils/new_job_defaults'; | ||
| import { JobCreator } from '../job_creator'; | ||
| import { populateValidationMessages, checkForExistingJobAndGroupIds } from './util'; | ||
| import { ExistingJobsAndGroups } from '../../../../services/job_service'; | ||
|
|
||
| // delay start of validation to allow the user to make changes | ||
| // e.g. if they are typing in a new value, try not to validate | ||
| // after every keystroke | ||
| const VALIDATION_DELAY_MS = 500; | ||
|
|
||
| export interface ValidationSummary { | ||
| basic: boolean; | ||
| advanced: boolean; | ||
| } | ||
|
|
||
| export interface Validation { | ||
| valid: boolean; | ||
| message?: string; | ||
| } | ||
|
|
||
| export interface BasicValidations { | ||
| jobId: Validation; | ||
| groupIds: Validation; | ||
| modelMemoryLimit: Validation; | ||
| bucketSpan: Validation; | ||
| duplicateDetectors: Validation; | ||
| } | ||
|
|
||
| export class JobValidator { | ||
| private _jobCreator: JobCreator; | ||
| private _validationSummary: ValidationSummary; | ||
| private _lastJobConfig: string; | ||
| private _validateTimeout: NodeJS.Timeout; | ||
| private _existingJobsAndGroups: ExistingJobsAndGroups; | ||
| private _basicValidations: BasicValidations = { | ||
| jobId: { valid: true }, | ||
| groupIds: { valid: true }, | ||
| modelMemoryLimit: { valid: true }, | ||
| bucketSpan: { valid: true }, | ||
| duplicateDetectors: { valid: true }, | ||
| }; | ||
|
|
||
| constructor(jobCreator: JobCreator, existingJobsAndGroups: ExistingJobsAndGroups) { | ||
| this._jobCreator = jobCreator; | ||
| this._lastJobConfig = this._jobCreator.formattedJobJson; | ||
| this._validationSummary = { | ||
| basic: false, | ||
| advanced: false, | ||
| }; | ||
| this._validateTimeout = setTimeout(() => {}, 0); | ||
| this._existingJobsAndGroups = existingJobsAndGroups; | ||
| } | ||
|
|
||
| public validate() { | ||
| const formattedJobConfig = this._jobCreator.formattedJobJson; | ||
| return new Promise((resolve: () => void) => { | ||
| // only validate if the config has changed | ||
| if (formattedJobConfig !== this._lastJobConfig) { | ||
| clearTimeout(this._validateTimeout); | ||
| this._lastJobConfig = formattedJobConfig; | ||
| this._validateTimeout = setTimeout(() => { | ||
| this._runBasicValidation(); | ||
| resolve(); | ||
| }, VALIDATION_DELAY_MS); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private _resetBasicValidations() { | ||
| this._validationSummary.basic = true; | ||
| Object.values(this._basicValidations).forEach(v => { | ||
| v.valid = true; | ||
| delete v.message; | ||
| }); | ||
| } | ||
|
|
||
| private _runBasicValidation() { | ||
| this._resetBasicValidations(); | ||
|
|
||
| const jobConfig = this._jobCreator.jobConfig; | ||
| const limits = newJobLimits(); | ||
|
|
||
| // run standard basic validation | ||
| const basicResults = basicJobValidation(jobConfig, undefined, limits); | ||
| populateValidationMessages(basicResults, this._basicValidations, jobConfig); | ||
|
|
||
| // run addition job and group id validation | ||
| const idResults = checkForExistingJobAndGroupIds( | ||
| this._jobCreator.jobId, | ||
| this._jobCreator.groups, | ||
| this._existingJobsAndGroups | ||
| ); | ||
| populateValidationMessages(idResults, this._basicValidations, jobConfig); | ||
|
|
||
| this._validationSummary.basic = this._isOverallBasicValid(); | ||
| } | ||
|
|
||
| private _isOverallBasicValid() { | ||
| return Object.values(this._basicValidations).some(v => v.valid === false) === false; | ||
| } | ||
|
|
||
| public get validationSummary(): ValidationSummary { | ||
| return this._validationSummary; | ||
| } | ||
|
|
||
| public get bucketSpan(): Validation { | ||
| return this._basicValidations.bucketSpan; | ||
| } | ||
|
|
||
| public get duplicateDetectors(): Validation { | ||
| return this._basicValidations.duplicateDetectors; | ||
| } | ||
|
|
||
| public get jobId(): Validation { | ||
| return this._basicValidations.jobId; | ||
| } | ||
|
|
||
| public get groupIds(): Validation { | ||
| return this._basicValidations.groupIds; | ||
| } | ||
|
|
||
| public get modelMemoryLimit(): Validation { | ||
| return this._basicValidations.modelMemoryLimit; | ||
| } | ||
| } |
160 changes: 160 additions & 0 deletions
160
x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_validator/util.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { i18n } from '@kbn/i18n'; | ||
| import { BasicValidations } from './job_validator'; | ||
| import { Job } from '../job_creator/configs'; | ||
| import { ALLOWED_DATA_UNITS } from '../../../../../common/constants/validation'; | ||
| import { newJobLimits } from '../../../new_job/utils/new_job_defaults'; | ||
| import { ValidationResults, ValidationMessage } from '../../../../../common/util/job_utils'; | ||
| import { ExistingJobsAndGroups } from '../../../../services/job_service'; | ||
|
|
||
| export function populateValidationMessages( | ||
| validationResults: ValidationResults, | ||
| basicValidations: BasicValidations, | ||
| jobConfig: Job | ||
| ) { | ||
| const limits = newJobLimits(); | ||
|
|
||
| if (validationResults.contains('job_id_empty')) { | ||
| basicValidations.jobId.valid = false; | ||
| } else if (validationResults.contains('job_id_invalid')) { | ||
| basicValidations.jobId.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.jobNameAllowedCharactersDescription', | ||
| { | ||
| defaultMessage: | ||
| 'Job name can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores; ' + | ||
| 'must start and end with an alphanumeric character', | ||
| } | ||
| ); | ||
| basicValidations.jobId.message = msg; | ||
| } else if (validationResults.contains('job_id_already_exists')) { | ||
| basicValidations.jobId.valid = false; | ||
| const msg = i18n.translate('xpack.ml.newJob.wizard.validateJob.jobNameAlreadyExists', { | ||
| defaultMessage: | ||
| 'Job ID already exists. A job ID cannot be the same as an existing job or group.', | ||
| }); | ||
| basicValidations.jobId.message = msg; | ||
| } | ||
|
|
||
| if (validationResults.contains('job_group_id_invalid')) { | ||
| basicValidations.groupIds.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.jobGroupAllowedCharactersDescription', | ||
| { | ||
| defaultMessage: | ||
| 'Job group names can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores; ' + | ||
| 'must start and end with an alphanumeric character', | ||
| } | ||
| ); | ||
| basicValidations.groupIds.message = msg; | ||
| } else if (validationResults.contains('job_group_id_already_exists')) { | ||
| basicValidations.groupIds.valid = false; | ||
| const msg = i18n.translate('xpack.ml.newJob.wizard.validateJob.groupNameAlreadyExists', { | ||
| defaultMessage: | ||
| 'Group ID already exists. A group ID cannot be the same as an existing job or group.', | ||
| }); | ||
| basicValidations.groupIds.message = msg; | ||
| } | ||
|
|
||
| if (validationResults.contains('model_memory_limit_units_invalid')) { | ||
| basicValidations.modelMemoryLimit.valid = false; | ||
| const str = `${ALLOWED_DATA_UNITS.slice(0, ALLOWED_DATA_UNITS.length - 1).join(', ')} or ${[ | ||
| ...ALLOWED_DATA_UNITS, | ||
| ].pop()}`; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.modelMemoryLimitUnitsInvalidErrorMessage', | ||
| { | ||
| defaultMessage: 'Model memory limit data unit unrecognized. It must be {str}', | ||
| values: { str }, | ||
| } | ||
| ); | ||
| basicValidations.modelMemoryLimit.message = msg; | ||
| } | ||
|
|
||
| if (validationResults.contains('model_memory_limit_invalid')) { | ||
| basicValidations.modelMemoryLimit.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.modelMemoryLimitRangeInvalidErrorMessage', | ||
| { | ||
| defaultMessage: | ||
| 'Model memory limit cannot be higher than the maximum value of {maxModelMemoryLimit}', | ||
| values: { maxModelMemoryLimit: limits.max_model_memory_limit.toUpperCase() }, | ||
| } | ||
| ); | ||
| basicValidations.modelMemoryLimit.message = msg; | ||
| } | ||
|
|
||
| if (validationResults.contains('detectors_duplicates')) { | ||
| basicValidations.duplicateDetectors.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.duplicatedDetectorsErrorMessage', | ||
| { | ||
| defaultMessage: 'Duplicate detectors were found.', | ||
| } | ||
| ); | ||
| basicValidations.duplicateDetectors.message = msg; | ||
| } | ||
|
|
||
| if (validationResults.contains('bucket_span_empty')) { | ||
| basicValidations.bucketSpan.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.bucketSpanMustBeSetErrorMessage', | ||
| { | ||
| defaultMessage: 'Bucket span must be set', | ||
| } | ||
| ); | ||
|
|
||
| basicValidations.bucketSpan.message = msg; | ||
| } else if (validationResults.contains('bucket_span_invalid')) { | ||
| basicValidations.bucketSpan.valid = false; | ||
| const msg = i18n.translate( | ||
| 'xpack.ml.newJob.wizard.validateJob.bucketSpanInvalidTimeIntervalFormatErrorMessage', | ||
| { | ||
| defaultMessage: | ||
| '{bucketSpan} is not a valid time interval format e.g. {tenMinutes}, {oneHour}. It also needs to be higher than zero.', | ||
| values: { | ||
| bucketSpan: jobConfig.analysis_config.bucket_span, | ||
| tenMinutes: '10m', | ||
| oneHour: '1h', | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| basicValidations.bucketSpan.message = msg; | ||
| } | ||
| } | ||
|
|
||
| export function checkForExistingJobAndGroupIds( | ||
| jobId: string, | ||
| groupIds: string[], | ||
| existingJobsAndGroups: ExistingJobsAndGroups | ||
| ): ValidationResults { | ||
| const messages: ValidationMessage[] = []; | ||
|
|
||
| // check that job id does not already exist as a job or group or a newly created group | ||
| if ( | ||
| existingJobsAndGroups.jobIds.includes(jobId) || | ||
| existingJobsAndGroups.groupIds.includes(jobId) || | ||
| groupIds.includes(jobId) | ||
| ) { | ||
| messages.push({ id: 'job_id_already_exists' }); | ||
| } | ||
|
|
||
| // check that groups that have been newly added in this job do not already exist as job ids | ||
| const newGroups = groupIds.filter(g => !existingJobsAndGroups.groupIds.includes(g)); | ||
| if (existingJobsAndGroups.jobIds.some(g => newGroups.includes(g))) { | ||
| messages.push({ id: 'job_group_id_already_exists' }); | ||
| } | ||
|
|
||
| return { | ||
| messages, | ||
| valid: messages.length === 0, | ||
| contains: (id: string) => messages.some(m => id === m.id), | ||
| find: (id: string) => messages.find(m => id === m.id), | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.