import { FETCH_STATUS } from '../../public/hooks/use_fetcher' ; import type { APIReturnType } from '../../public/services/rest/createCallApmApi' ; import { ENVIRONMENT_ALL } from '../environment_filter_values' ; export enum AnomalyDetectionSetupState { Loading = 'pending' , Failure = 'failure' , Unknown = 'unknown' , NoJobs = 'noJobs' , NoJobsForEnvironment = 'noJobsForEnvironment' , LegacyJobs = 'legacyJobs' , UpgradeableJobs = 'upgradeableJobs' , UpToDate = 'upToDate' } export function getAnomalyDetectionSetupState ( { environment , jobs , fetchStatus , isAuthorized , } ) { if ( ! isAuthorized ) { return AnomalyDetectionSetupState . Unknown ; } if ( fetchStatus === FETCH_STATUS . LOADING ) { return AnomalyDetectionSetupState . Loading ; } if ( fetchStatus === FETCH_STATUS . FAILURE ) { return AnomalyDetectionSetupState . Failure ; } if ( fetchStatus !== FETCH_STATUS . SUCCESS ) { return AnomalyDetectionSetupState . Unknown ; } const jobsForEnvironment = environment === ENVIRONMENT_ALL . value ? jobs : jobs . filter ( ( job ) => job . environment === environment ) ; const hasV1Jobs = jobs . some ( ( job ) => job . version === 1 ) ; const hasV2Jobs = jobsForEnvironment . some ( ( job ) => job . version === 2 ) ; const hasV3Jobs = jobsForEnvironment . some ( ( job ) => job . version === 3 ) ; const hasAnyJobs = jobs . length > 0 ; if ( hasV3Jobs ) { return AnomalyDetectionSetupState . UpToDate ; } if ( hasV2Jobs ) { return AnomalyDetectionSetupState . UpgradeableJobs ; } if ( hasV1Jobs ) { return AnomalyDetectionSetupState . LegacyJobs ; } if ( hasAnyJobs ) { return AnomalyDetectionSetupState . NoJobsForEnvironment ; } return AnomalyDetectionSetupState . NoJobs ; }