From 457aacfe841ec413c11d1d6becb96bc49b02f650 Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Thu, 2 Apr 2020 13:04:24 -0500 Subject: [PATCH] [Uptime] Remove static constant for index name completely (#62256) (#62322) Fixes #62255 . There were some remaining usages of a static defined index name. Co-authored-by: Elastic Machine Co-authored-by: Elastic Machine --- .../plugins/uptime/common/constants/index.ts | 1 - .../uptime/common/constants/index_names.ts | 9 --------- .../ml/ml_flyout_container.tsx | 19 +++++++++++++++---- .../uptime/public/state/actions/ml_anomaly.ts | 14 ++++++++++---- .../uptime/public/state/actions/types.ts | 4 ++++ .../uptime/public/state/api/ml_anomaly.ts | 14 ++++++++++---- .../server/lib/requests/get_monitor_status.ts | 5 ++--- 7 files changed, 41 insertions(+), 25 deletions(-) delete mode 100644 x-pack/legacy/plugins/uptime/common/constants/index_names.ts diff --git a/x-pack/legacy/plugins/uptime/common/constants/index.ts b/x-pack/legacy/plugins/uptime/common/constants/index.ts index 19f2de3c6f0f4..74783cf46550f 100644 --- a/x-pack/legacy/plugins/uptime/common/constants/index.ts +++ b/x-pack/legacy/plugins/uptime/common/constants/index.ts @@ -8,7 +8,6 @@ export { ACTION_GROUP_DEFINITIONS } from './alerts'; export { CHART_FORMAT_LIMITS } from './chart_format_limits'; export { CLIENT_DEFAULTS } from './client_defaults'; export { CONTEXT_DEFAULTS } from './context_defaults'; -export { INDEX_NAMES } from './index_names'; export * from './capabilities'; export { PLUGIN } from './plugin'; export { QUERY, STATES } from './query'; diff --git a/x-pack/legacy/plugins/uptime/common/constants/index_names.ts b/x-pack/legacy/plugins/uptime/common/constants/index_names.ts deleted file mode 100644 index 4fce01f4cd408..0000000000000 --- a/x-pack/legacy/plugins/uptime/common/constants/index_names.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* - * 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 const INDEX_NAMES = { - HEARTBEAT: 'heartbeat-7*', -}; diff --git a/x-pack/legacy/plugins/uptime/public/components/monitor_details/ml/ml_flyout_container.tsx b/x-pack/legacy/plugins/uptime/public/components/monitor_details/ml/ml_flyout_container.tsx index fad12de94fd81..0a3226bc064a8 100644 --- a/x-pack/legacy/plugins/uptime/public/components/monitor_details/ml/ml_flyout_container.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/monitor_details/ml/ml_flyout_container.tsx @@ -12,6 +12,7 @@ import { hasMLJobSelector, hasNewMLJobSelector, isMLJobCreatingSelector, + selectDynamicSettings, } from '../../../state/selectors'; import { createMLJobAction, getExistingMLJobAction } from '../../../state/actions'; import { MLJobLink } from './ml_job_link'; @@ -24,6 +25,7 @@ import { MLFlyoutView } from './ml_flyout'; import { ML_JOB_ID } from '../../../../common/constants'; import { UptimeRefreshContext, UptimeSettingsContext } from '../../../contexts'; import { useUrlParams } from '../../../hooks'; +import { getDynamicSettings } from '../../../state/actions/dynamic_settings'; interface Props { onClose: () => void; @@ -65,6 +67,12 @@ export const MachineLearningFlyout: React.FC = ({ onClose }) => { const dispatch = useDispatch(); const { data: hasMLJob, error } = useSelector(hasNewMLJobSelector); const isMLJobCreating = useSelector(isMLJobCreatingSelector); + const { settings } = useSelector(selectDynamicSettings); + useEffect(() => { + // Attempt to load or refresh the dynamic settings + dispatch(getDynamicSettings({})); + }, [dispatch]); + const heartbeatIndices = settings?.heartbeatIndices || ''; const { basePath } = useContext(UptimeSettingsContext); const { refreshApp } = useContext(UptimeRefreshContext); @@ -72,9 +80,12 @@ export const MachineLearningFlyout: React.FC = ({ onClose }) => { let { monitorId } = useParams(); monitorId = atob(monitorId || ''); - const createMLJob = () => dispatch(createMLJobAction.get({ monitorId: monitorId as string })); + const canCreateMLJob = useSelector(canCreateMLJobSelector) && heartbeatIndices !== ''; - const canCreateMLJob = useSelector(canCreateMLJobSelector); + // This function is a noop in the form's disabled state + const createMLJob = heartbeatIndices + ? () => dispatch(createMLJobAction.get({ monitorId: monitorId as string, heartbeatIndices })) + : () => null; const { data: uptimeJobs } = useSelector(hasMLJobSelector); @@ -130,9 +141,9 @@ export const MachineLearningFlyout: React.FC = ({ onClose }) => { useEffect(() => { if (hasExistingMLJob) { setIsCreatingJob(true); - dispatch(createMLJobAction.get({ monitorId: monitorId as string })); + dispatch(createMLJobAction.get({ monitorId: monitorId as string, heartbeatIndices })); } - }, [dispatch, hasExistingMLJob, monitorId]); + }, [dispatch, hasExistingMLJob, heartbeatIndices, monitorId]); if (hasExistingMLJob) { return null; diff --git a/x-pack/legacy/plugins/uptime/public/state/actions/ml_anomaly.ts b/x-pack/legacy/plugins/uptime/public/state/actions/ml_anomaly.ts index 9a8e4036f2cff..2e83490b71b54 100644 --- a/x-pack/legacy/plugins/uptime/public/state/actions/ml_anomaly.ts +++ b/x-pack/legacy/plugins/uptime/public/state/actions/ml_anomaly.ts @@ -8,7 +8,12 @@ import { createAction } from 'redux-actions'; import { createAsyncAction } from './utils'; import { PrivilegesResponse } from '../../../../../../plugins/ml/common/types/privileges'; import { AnomaliesTableRecord } from '../../../../../../plugins/ml/common/types/anomalies'; -import { CreateMLJobSuccess, DeleteJobResults, MonitorIdParam } from './types'; +import { + CreateMLJobSuccess, + DeleteJobResults, + MonitorIdParam, + HeartbeatIndicesParam, +} from './types'; import { JobExistResult } from '../../../../../../plugins/ml/common/types/data_recognizer'; export const resetMLState = createAction('RESET_ML_STATE'); @@ -17,9 +22,10 @@ export const getExistingMLJobAction = createAsyncAction( - 'CREATE_ML_JOB' -); +export const createMLJobAction = createAsyncAction< + MonitorIdParam & HeartbeatIndicesParam, + CreateMLJobSuccess | null +>('CREATE_ML_JOB'); export const getMLCapabilitiesAction = createAsyncAction( 'GET_ML_CAPABILITIES' diff --git a/x-pack/legacy/plugins/uptime/public/state/actions/types.ts b/x-pack/legacy/plugins/uptime/public/state/actions/types.ts index 236b263414a26..41381afd31453 100644 --- a/x-pack/legacy/plugins/uptime/public/state/actions/types.ts +++ b/x-pack/legacy/plugins/uptime/public/state/actions/types.ts @@ -22,6 +22,10 @@ export interface MonitorIdParam { monitorId: string; } +export interface HeartbeatIndicesParam { + heartbeatIndices: string; +} + export interface QueryParams { monitorId: string; dateStart: string; diff --git a/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts b/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts index 80b97783769b5..0b31a74a40133 100644 --- a/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts +++ b/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts @@ -7,9 +7,14 @@ import moment from 'moment'; import { apiService } from './utils'; import { AnomalyRecords, AnomalyRecordsParams } from '../actions'; -import { API_URLS, INDEX_NAMES, ML_JOB_ID, ML_MODULE_ID } from '../../../common/constants'; +import { API_URLS, ML_JOB_ID, ML_MODULE_ID } from '../../../common/constants'; import { PrivilegesResponse } from '../../../../../../plugins/ml/common/types/privileges'; -import { CreateMLJobSuccess, DeleteJobResults, MonitorIdParam } from '../actions/types'; +import { + CreateMLJobSuccess, + DeleteJobResults, + MonitorIdParam, + HeartbeatIndicesParam, +} from '../actions/types'; import { DataRecognizerConfigResponse } from '../../../../../../plugins/ml/common/types/modules'; import { JobExistResult } from '../../../../../../plugins/ml/common/types/data_recognizer'; @@ -25,7 +30,8 @@ export const getExistingJobs = async (): Promise => { export const createMLJob = async ({ monitorId, -}: MonitorIdParam): Promise => { + heartbeatIndices, +}: MonitorIdParam & HeartbeatIndicesParam): Promise => { const url = API_URLS.ML_SETUP_MODULE + ML_MODULE_ID; const data = { @@ -35,7 +41,7 @@ export const createMLJob = async ({ start: moment() .subtract(24, 'h') .valueOf(), - indexPatternName: INDEX_NAMES.HEARTBEAT, + indexPatternName: heartbeatIndices, query: { bool: { filter: [ diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 2cebd532fd29b..cea8b819f403e 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -5,7 +5,6 @@ */ import { UMElasticsearchQueryFn } from '../adapters'; -import { INDEX_NAMES } from '../../../../../legacy/plugins/uptime/common/constants'; export interface GetMonitorStatusParams { filters?: string; @@ -51,7 +50,7 @@ const getLocationClause = (locations: string[]) => ({ export const getMonitorStatus: UMElasticsearchQueryFn< GetMonitorStatusParams, GetMonitorStatusResult[] -> = async ({ callES, filters, locations, numTimes, timerange: { from, to } }) => { +> = async ({ callES, dynamicSettings, filters, locations, numTimes, timerange: { from, to } }) => { const queryResults: Array> = []; let afterKey: MonitorStatusKey | undefined; @@ -60,7 +59,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< // multiple status types for this alert, and this will become a parameter const STATUS = 'down'; const esParams: any = { - index: INDEX_NAMES.HEARTBEAT, + index: dynamicSettings.heartbeatIndices, body: { query: { bool: {