import { difference , without } from 'lodash' ; import { i18n } from '@kbn/i18n' ; import { ToastsStart } from 'kibana/public' ; import { MlJobWithTimeRange } from '../../../../common/types/anomaly_detection_jobs' ; import { getTimeRangeFromSelection } from '../../components/job_selector/job_select_service_utils' ; import { mlJobService } from '../../services/job_service' ; import { createTimeSeriesJobData } from './timeseriesexplorer_utils' ; /**
 * returns true/false if setGlobalState has been triggered
 * or returns the job id which should be loaded.
 */ export function validateJobSelection ( jobsWithTimeRange , selectedJobIds , setGlobalState , toastNotifications ) { const jobs = createTimeSeriesJobData ( mlJobService . jobs ) ; const timeSeriesJobIds = jobs . map ( ( j ) => j . id ) ; const invalidIds = difference ( selectedJobIds , timeSeriesJobIds ) ; const validSelectedJobIds = without ( selectedJobIds , ... invalidIds ) ; if ( invalidIds . length === 1 ) { const selectedJobId = invalidIds [ 0 ] ; const selectedJob = jobsWithTimeRange . find ( ( j ) => j . id === selectedJobId ) ; if ( selectedJob !== undefined && selectedJob . isNotSingleMetricViewerJobMessage !== undefined ) { const warningText = i18n . translate ( 'xpack.ml.timeSeriesExplorer.canNotViewRequestedJobsWarningWithReasonMessage' , { defaultMessage : `You can't view {selectedJobId} in this dashboard because {reason}.` , values : { selectedJobId , reason : selectedJob . isNotSingleMetricViewerJobMessage , } , } ) ; toastNotifications . addWarning ( { title : warningText , 'data-test-subj' : 'mlTimeSeriesExplorerDisabledJobReasonWarningToast' , } ) ; } } if ( invalidIds . length > 1 ) { let warningText = i18n . translate ( 'xpack.ml.timeSeriesExplorer.canNotViewRequestedJobsWarningMessage' , { defaultMessage : `You can't view requested {invalidIdsCount, plural, one {job} other {jobs}} {invalidIds} in this dashboard` , values : { invalidIdsCount : invalidIds . length , invalidIds : invalidIds . join ( ', ' ) , } , } ) ; if ( validSelectedJobIds . length === 0 && timeSeriesJobIds . length > 0 ) { warningText += i18n . translate ( 'xpack.ml.timeSeriesExplorer.autoSelectingFirstJobText' , { defaultMessage : ', auto selecting first job' , } ) ; } toastNotifications . addWarning ( warningText ) ; } if ( validSelectedJobIds . length > 1 ) { toastNotifications . addWarning ( i18n . translate ( 'xpack.ml.timeSeriesExplorer.youCanViewOneJobAtTimeWarningMessage' , { defaultMessage : 'You can only view one job at a time in this dashboard' , } ) ) ; setGlobalState ( 'ml' , { jobIds : [ validSelectedJobIds [ 0 ] ] } ) ; return true ; } else if ( invalidIds . length > 0 && validSelectedJobIds . length > 0 ) { setGlobalState ( 'ml' , { jobIds : [ validSelectedJobIds [ 0 ] ] } ) ; return true ; } else if ( validSelectedJobIds . length === 1 ) { return validSelectedJobIds [ 0 ] ; } else if ( validSelectedJobIds . length === 0 && jobs . length > 0 ) { const jobIds = [ jobs [ 0 ] . id ] ; const time = getTimeRangeFromSelection ( jobsWithTimeRange , jobIds ) ; setGlobalState ( { ... { ml : { jobIds } } , ... ( time !== undefined ? { time } : { } ) , } ) ; return true ; } else { return false ; } }