From e903a50ee3540b3a4ab2cdc8a9384d37fc5ca3b7 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Wed, 30 Oct 2019 11:56:31 -0400 Subject: [PATCH] move ui_metric service to shim --- .../index_management/public/app/app.tsx | 5 ++-- .../index_list/index_table/index_table.js | 5 ++-- .../template_details/template_details.tsx | 4 +-- .../home/template_list/template_list.tsx | 4 +-- .../template_table/template_table.tsx | 4 +-- .../public/app/services/api.ts | 28 +++++++++---------- .../public/app/services/index.ts | 1 - .../public/app/services/track_ui_metric.ts | 14 ---------- .../public/app/services/ui_metric.ts | 25 +++++++++++++++++ .../public/app/store/reducers/detail_panel.js | 4 +-- .../plugins/index_management/public/legacy.ts | 7 +++++ .../plugins/index_management/public/plugin.ts | 4 ++- 12 files changed, 62 insertions(+), 43 deletions(-) delete mode 100644 x-pack/legacy/plugins/index_management/public/app/services/track_ui_metric.ts create mode 100644 x-pack/legacy/plugins/index_management/public/app/services/ui_metric.ts diff --git a/x-pack/legacy/plugins/index_management/public/app/app.tsx b/x-pack/legacy/plugins/index_management/public/app/app.tsx index b104ac3fec2eca..aa3035c7823310 100644 --- a/x-pack/legacy/plugins/index_management/public/app/app.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/app.tsx @@ -11,11 +11,10 @@ import { IndexManagementHome } from './sections/home'; import { TemplateCreate } from './sections/template_create'; import { TemplateClone } from './sections/template_clone'; import { TemplateEdit } from './sections/template_edit'; -import { trackUiMetric } from './services'; -import { METRIC_TYPE } from './services/track_ui_metric'; +import { uiMetricService } from './services/ui_metric'; export const App = () => { - useEffect(() => trackUiMetric(METRIC_TYPE.LOADED, UIM_APP_LOAD), []); + useEffect(() => uiMetricService.track('loaded', UIM_APP_LOAD), []); return ( diff --git a/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_table/index_table.js b/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_table/index_table.js index cf858a3cf55405..4c3e45e8c00177 100644 --- a/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_table/index_table.js +++ b/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_table/index_table.js @@ -37,7 +37,8 @@ import { import { UIM_SHOW_DETAILS_CLICK } from '../../../../../../common/constants'; import { REFRESH_RATE_INDEX_LIST } from '../../../../constants'; -import { healthToColor, trackUiMetric } from '../../../../services'; +import { healthToColor } from '../../../../services'; +import { uiMetricService } from '../../../../services/ui_metric'; import { getBannerExtensions, getFilterExtensions, @@ -221,7 +222,7 @@ export class IndexTable extends Component { className="indTable__link" data-test-subj="indexTableIndexNameLink" onClick={() => { - trackUiMetric('click', UIM_SHOW_DETAILS_CLICK); + uiMetricService.track('click', UIM_SHOW_DETAILS_CLICK); openDetailPanel(value); }} > diff --git a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_details/template_details.tsx b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_details/template_details.tsx index d5542b60e2f9f5..42e0f6efc17c6d 100644 --- a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_details/template_details.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_details/template_details.tsx @@ -34,7 +34,7 @@ import { Template } from '../../../../../../common/types'; import { TemplateDeleteModal, SectionLoading, SectionError, Error } from '../../../../components'; import { loadIndexTemplate } from '../../../../services/api'; import { decodePath } from '../../../../services/routing'; -import { trackUiMetric, METRIC_TYPE } from '../../../../services/track_ui_metric'; +import { uiMetricService } from '../../../../services/ui_metric'; import { SendRequestResponse } from '../../../../../shared_imports'; import { TabSummary, TabMappings, TabSettings, TabAliases } from './tabs'; @@ -164,7 +164,7 @@ export const TemplateDetails: React.FunctionComponent = ({ {TABS.map(tab => ( { - trackUiMetric(METRIC_TYPE.CLICK, tabToUiMetricMap[tab.id]); + uiMetricService.track('click', tabToUiMetricMap[tab.id]); setActiveTab(tab.id); }} isSelected={tab.id === activeTab} diff --git a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_list.tsx b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_list.tsx index ac8837f902e53b..af1169673d9d10 100644 --- a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_list.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_list.tsx @@ -20,7 +20,7 @@ import { SectionError, SectionLoading, Error } from '../../../components'; import { TemplateTable } from './template_table'; import { loadIndexTemplates } from '../../../services/api'; import { Template } from '../../../../../common/types'; -import { trackUiMetric, METRIC_TYPE } from '../../../services/track_ui_metric'; +import { uiMetricService } from '../../../services/ui_metric'; import { getTemplateEditLink, getTemplateListLink, @@ -66,7 +66,7 @@ export const TemplateList: React.FunctionComponent { - trackUiMetric(METRIC_TYPE.LOADED, UIM_TEMPLATE_LIST_LOAD); + uiMetricService.track('loaded', UIM_TEMPLATE_LIST_LOAD); }, []); if (isLoading) { diff --git a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_table/template_table.tsx b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_table/template_table.tsx index 146a412b81b105..2a48f04b1dcc67 100644 --- a/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_table/template_table.tsx +++ b/x-pack/legacy/plugins/index_management/public/app/sections/home/template_list/template_table/template_table.tsx @@ -11,7 +11,7 @@ import { EuiInMemoryTable, EuiIcon, EuiButton, EuiLink } from '@elastic/eui'; import { TemplateListItem, Template } from '../../../../../../common/types'; import { BASE_PATH, UIM_TEMPLATE_SHOW_DETAILS_CLICK } from '../../../../../../common/constants'; import { TemplateDeleteModal } from '../../../../components'; -import { trackUiMetric, METRIC_TYPE } from '../../../../services/track_ui_metric'; +import { uiMetricService } from '../../../../services/ui_metric'; import { getTemplateDetailsLink } from '../../../../services/routing'; import { SendRequestResponse } from '../../../../../shared_imports'; @@ -45,7 +45,7 @@ export const TemplateTable: React.FunctionComponent = ({ trackUiMetric(METRIC_TYPE.CLICK, UIM_TEMPLATE_SHOW_DETAILS_CLICK)} + onClick={() => uiMetricService.track('click', UIM_TEMPLATE_SHOW_DETAILS_CLICK)} > {name} diff --git a/x-pack/legacy/plugins/index_management/public/app/services/api.ts b/x-pack/legacy/plugins/index_management/public/app/services/api.ts index ac6b8e3a7d0bbb..794aefb1547783 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/api.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/api.ts @@ -34,7 +34,7 @@ import { import { TAB_SETTINGS, TAB_MAPPING, TAB_STATS } from '../constants'; -import { trackUiMetric, METRIC_TYPE } from './track_ui_metric'; +import { uiMetricService } from './ui_metric'; import { useRequest, sendRequest } from './use_request'; import { httpService } from './http'; import { Template } from '../../../common/types'; @@ -59,7 +59,7 @@ export async function closeIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/close`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_CLOSE_MANY : UIM_INDEX_CLOSE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -70,7 +70,7 @@ export async function deleteIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/delete`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_DELETE_MANY : UIM_INDEX_DELETE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -81,7 +81,7 @@ export async function openIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/open`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_OPEN_MANY : UIM_INDEX_OPEN; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -92,7 +92,7 @@ export async function refreshIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/refresh`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_REFRESH_MANY : UIM_INDEX_REFRESH; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -103,7 +103,7 @@ export async function flushIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/flush`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_FLUSH_MANY : UIM_INDEX_FLUSH; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -117,7 +117,7 @@ export async function forcemergeIndices(indices: string[], maxNumSegments: strin }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_FORCE_MERGE_MANY : UIM_INDEX_FORCE_MERGE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -130,7 +130,7 @@ export async function clearCacheIndices(indices: string[]) { }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_CLEAR_CACHE_MANY : UIM_INDEX_CLEAR_CACHE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } export async function freezeIndices(indices: string[]) { @@ -140,7 +140,7 @@ export async function freezeIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/freeze`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_FREEZE_MANY : UIM_INDEX_FREEZE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } export async function unfreezeIndices(indices: string[]) { @@ -150,7 +150,7 @@ export async function unfreezeIndices(indices: string[]) { const response = await httpService.httpClient.post(`${API_BASE_PATH}/indices/unfreeze`, { body }); // Only track successful requests. const eventName = indices.length > 1 ? UIM_INDEX_UNFREEZE_MANY : UIM_INDEX_UNFREEZE; - trackUiMetric(METRIC_TYPE.COUNT, eventName); + uiMetricService.track('count', eventName); return response; } @@ -164,7 +164,7 @@ export async function updateIndexSettings(indexName: string, body: object) { body: JSON.stringify(body), }); // Only track successful requests. - trackUiMetric(METRIC_TYPE.COUNT, UIM_UPDATE_SETTINGS); + uiMetricService.track('count', eventName); return response; } @@ -210,7 +210,7 @@ export async function deleteTemplates(names: Array) { const uimActionType = names.length > 1 ? UIM_TEMPLATE_DELETE_MANY : UIM_TEMPLATE_DELETE; - trackUiMetric(METRIC_TYPE.COUNT, uimActionType); + uiMetricService.track('count', uimActionType); return result; } @@ -232,7 +232,7 @@ export async function saveTemplate(template: Template, isClone?: boolean) { const uimActionType = isClone ? UIM_TEMPLATE_CLONE : UIM_TEMPLATE_CREATE; - trackUiMetric(METRIC_TYPE.COUNT, uimActionType); + uiMetricService.track('count', uimActionType); return result; } @@ -244,7 +244,7 @@ export async function updateTemplate(template: Template) { body: JSON.stringify(template), }); - trackUiMetric(METRIC_TYPE.COUNT, UIM_TEMPLATE_UPDATE); + uiMetricService.track('count', uimActionType); return result; } diff --git a/x-pack/legacy/plugins/index_management/public/app/services/index.ts b/x-pack/legacy/plugins/index_management/public/app/services/index.ts index f5a276e66c096e..bb5c854e5d96c1 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/index.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/index.ts @@ -24,5 +24,4 @@ export { loadIndexTemplates, } from './api'; export { healthToColor } from './health_to_color'; -export { trackUiMetric } from './track_ui_metric'; export { sortTable } from './sort_table'; diff --git a/x-pack/legacy/plugins/index_management/public/app/services/track_ui_metric.ts b/x-pack/legacy/plugins/index_management/public/app/services/track_ui_metric.ts deleted file mode 100644 index 5f1551a3dcefce..00000000000000 --- a/x-pack/legacy/plugins/index_management/public/app/services/track_ui_metric.ts +++ /dev/null @@ -1,14 +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. - */ - -import { - createUiStatsReporter, - METRIC_TYPE, -} from '../../../../../../../src/legacy/core_plugins/ui_metric/public'; -import { UIM_APP_NAME } from '../../../common/constants'; - -export { METRIC_TYPE }; -export const trackUiMetric = createUiStatsReporter(UIM_APP_NAME); diff --git a/x-pack/legacy/plugins/index_management/public/app/services/ui_metric.ts b/x-pack/legacy/plugins/index_management/public/app/services/ui_metric.ts new file mode 100644 index 00000000000000..b891d968f53cd4 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/public/app/services/ui_metric.ts @@ -0,0 +1,25 @@ +/* + * 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 { UIM_APP_NAME } from '../../../common/constants'; +import { + createUiStatsReporter, + METRIC_TYPE, +} from '../../../../../../../../src/legacy/core_plugins/ui_metric/public'; + +class UiMetricService { + track?: ReturnType; + + public init = (getReporter: typeof createUiStatsReporter): void => { + this.track = getReporter(UIM_APP_NAME); + }; + + public track = (type: METRIC_TYPE, eventName: string): void => { + if (!this.track) throw Error('UiMetricService not initialized.'); + return this.track(type, eventName); + }; +} + +export const uiMetricService = new UiMetricService(); diff --git a/x-pack/legacy/plugins/index_management/public/app/store/reducers/detail_panel.js b/x-pack/legacy/plugins/index_management/public/app/store/reducers/detail_panel.js index 28d7d26052c9db..b55da7bb8af5d6 100644 --- a/x-pack/legacy/plugins/index_management/public/app/store/reducers/detail_panel.js +++ b/x-pack/legacy/plugins/index_management/public/app/store/reducers/detail_panel.js @@ -19,7 +19,7 @@ import { TAB_STATS, TAB_EDIT_SETTINGS, } from '../../constants'; -import { trackUiMetric } from '../../services'; +import { uiMetricService } from '../../services/ui_metric'; import { openDetailPanel, closeDetailPanel } from '../actions/detail_panel'; import { loadIndexDataSuccess } from '../actions/load_index_data'; import { updateIndexSettingsSuccess, updateIndexSettingsError } from '../actions/update_index_settings'; @@ -54,7 +54,7 @@ export const detailPanel = handleActions( }; if (panelTypeToUiMetricMap[panelType]) { - trackUiMetric('count', panelTypeToUiMetricMap[panelType]); + uiMetricService.track('count', panelTypeToUiMetricMap[panelType]); } return { diff --git a/x-pack/legacy/plugins/index_management/public/legacy.ts b/x-pack/legacy/plugins/index_management/public/legacy.ts index 49313aaa664210..eee24feff5a678 100644 --- a/x-pack/legacy/plugins/index_management/public/legacy.ts +++ b/x-pack/legacy/plugins/index_management/public/legacy.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { management, MANAGEMENT_BREADCRUMB } from 'ui/management'; +import { createUiStatsReporter } from '../../../../../src/legacy/core_plugins/ui_metric/public'; export interface LegacyStart { management: { @@ -12,6 +13,9 @@ export interface LegacyStart { BREADCRUMB: typeof MANAGEMENT_BREADCRUMB; }; }; + uiMetric: { + createUiStatsReporter: typeof createUiStatsReporter; + }; } export const __LEGACYStart = { @@ -21,4 +25,7 @@ export const __LEGACYStart = { BREADCRUMB: MANAGEMENT_BREADCRUMB, }, }, + uiMetric: { + createUiStatsReporter, + }, }; diff --git a/x-pack/legacy/plugins/index_management/public/plugin.ts b/x-pack/legacy/plugins/index_management/public/plugin.ts index 9728c0822e8304..c7d1eb0725c91a 100644 --- a/x-pack/legacy/plugins/index_management/public/plugin.ts +++ b/x-pack/legacy/plugins/index_management/public/plugin.ts @@ -13,10 +13,11 @@ import { httpService } from './app/services/http'; import { breadcrumbService } from './app/services/set_breadcrumbs'; import { documentationService } from './app/services/documentation'; import { notificationService } from './app/services/notification'; +import { uiMetricService } from './app/services/ui_metric'; export class IndexMgmtPlugin { public start(core: CoreStart, plugins: {}, __LEGACY: LegacyStart) { - const { management } = __LEGACY; + const { management, uiMetric } = __LEGACY; const { http, chrome, docLinks, notifications } = core; // Initialize services @@ -24,6 +25,7 @@ export class IndexMgmtPlugin { breadcrumbService.init(chrome, management.constants.BREADCRUMB); documentationService.init(docLinks); notificationService.init(notifications); + uiMetricService.init(uiMetric.createUiStatsReporter); // Register management section and Angular route registerManagementSection(management.getSection('elasticsearch'));