Skip to content

Commit

Permalink
move ui_metric service to shim
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Oct 30, 2019
1 parent 16c751d commit e903a50
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 43 deletions.
5 changes: 2 additions & 3 deletions x-pack/legacy/plugins/index_management/public/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<HashRouter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -164,7 +164,7 @@ export const TemplateDetails: React.FunctionComponent<Props> = ({
{TABS.map(tab => (
<EuiTab
onClick={() => {
trackUiMetric(METRIC_TYPE.CLICK, tabToUiMetricMap[tab.id]);
uiMetricService.track('click', tabToUiMetricMap[tab.id]);
setActiveTab(tab.id);
}}
isSelected={tab.id === activeTab}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -66,7 +66,7 @@ export const TemplateList: React.FunctionComponent<RouteComponentProps<MatchPara

// Track component loaded
useEffect(() => {
trackUiMetric(METRIC_TYPE.LOADED, UIM_TEMPLATE_LIST_LOAD);
uiMetricService.track('loaded', UIM_TEMPLATE_LIST_LOAD);
}, []);

if (isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -45,7 +45,7 @@ export const TemplateTable: React.FunctionComponent<Props> = ({
<EuiLink
href={getTemplateDetailsLink(name, true)}
data-test-subj="templateDetailsLink"
onClick={() => trackUiMetric(METRIC_TYPE.CLICK, UIM_TEMPLATE_SHOW_DETAILS_CLICK)}
onClick={() => uiMetricService.track('click', UIM_TEMPLATE_SHOW_DETAILS_CLICK)}
>
{name}
</EuiLink>
Expand Down
28 changes: 14 additions & 14 deletions x-pack/legacy/plugins/index_management/public/app/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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[]) {
Expand All @@ -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[]) {
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -210,7 +210,7 @@ export async function deleteTemplates(names: Array<Template['name']>) {

const uimActionType = names.length > 1 ? UIM_TEMPLATE_DELETE_MANY : UIM_TEMPLATE_DELETE;

trackUiMetric(METRIC_TYPE.COUNT, uimActionType);
uiMetricService.track('count', uimActionType);

return result;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<typeof createUiStatsReporter>;

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();
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const detailPanel = handleActions(
};

if (panelTypeToUiMetricMap[panelType]) {
trackUiMetric('count', panelTypeToUiMetricMap[panelType]);
uiMetricService.track('count', panelTypeToUiMetricMap[panelType]);
}

return {
Expand Down
7 changes: 7 additions & 0 deletions x-pack/legacy/plugins/index_management/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -12,6 +13,9 @@ export interface LegacyStart {
BREADCRUMB: typeof MANAGEMENT_BREADCRUMB;
};
};
uiMetric: {
createUiStatsReporter: typeof createUiStatsReporter;
};
}

export const __LEGACYStart = {
Expand All @@ -21,4 +25,7 @@ export const __LEGACYStart = {
BREADCRUMB: MANAGEMENT_BREADCRUMB,
},
},
uiMetric: {
createUiStatsReporter,
},
};
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/index_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ 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
httpService.init(http);
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'));
Expand Down

0 comments on commit e903a50

Please sign in to comment.