From 9f01dbd84abab531a654da9244c7b66f41d06337 Mon Sep 17 00:00:00 2001 From: Dzmitry Tamashevich Date: Mon, 31 Oct 2022 17:54:50 +0300 Subject: [PATCH 1/4] [Discover] track adhoc usage --- .../application/main/hooks/use_adhoc_data_views.ts | 11 +++++++++++ .../application/main/hooks/use_discover_state.ts | 4 +++- src/plugins/discover/public/build_services.ts | 6 +++++- src/plugins/discover/public/constants.ts | 2 ++ .../public/embeddable/saved_search_embeddable.tsx | 7 +++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts index 89fd4477da34c..29b9426a5e0ea 100644 --- a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts +++ b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts @@ -9,6 +9,7 @@ import { useCallback, useEffect, useState } from 'react'; import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public'; import { SavedSearch } from '@kbn/saved-search-plugin/public'; +import { METRIC_TYPE } from '@kbn/analytics'; import { UPDATE_FILTER_REFERENCES_ACTION, UPDATE_FILTER_REFERENCES_TRIGGER, @@ -16,6 +17,7 @@ import { import { ActionExecutionContext } from '@kbn/ui-actions-plugin/public'; import type { FilterManager } from '@kbn/data-plugin/public'; import type { ToastsStart } from '@kbn/core-notifications-browser'; +import { ADHOC_DATA_VIEW_CLICK_EVENT } from '../../../constants'; import { getUiActions } from '../../../kibana_services'; import { useConfirmPersistencePrompt } from '../../../hooks/use_confirm_persistence_prompt'; import { GetStateReturn } from '../services/discover_state'; @@ -29,6 +31,7 @@ export const useAdHocDataViews = ({ filterManager, dataViews, toastNotifications, + trackUiMetric, }: { dataView: DataView; savedSearch: SavedSearch; @@ -37,11 +40,19 @@ export const useAdHocDataViews = ({ dataViews: DataViewsContract; filterManager: FilterManager; toastNotifications: ToastsStart; + trackUiMetric?: (metricType: string, eventName: string | string[], count?: number) => void; }) => { const [adHocDataViewList, setAdHocDataViewList] = useState( !dataView.isPersisted() ? [dataView] : [] ); + useEffect(() => { + if (trackUiMetric && adHocDataViewList.length) { + trackUiMetric(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT, adHocDataViewList.length); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [adHocDataViewList]); + useEffect(() => { if (!dataView.isPersisted()) { setAdHocDataViewList((prev) => { diff --git a/src/plugins/discover/public/application/main/hooks/use_discover_state.ts b/src/plugins/discover/public/application/main/hooks/use_discover_state.ts index ff448e59917a8..81f1bae179d39 100644 --- a/src/plugins/discover/public/application/main/hooks/use_discover_state.ts +++ b/src/plugins/discover/public/application/main/hooks/use_discover_state.ts @@ -44,7 +44,8 @@ export function useDiscoverState({ setExpandedDoc: (doc?: DataTableRecord) => void; dataViewList: DataViewListItem[]; }) { - const { uiSettings, data, filterManager, dataViews, toastNotifications } = services; + const { uiSettings, data, filterManager, dataViews, toastNotifications, trackUiMetric } = + services; const useNewFieldsApi = useMemo(() => !uiSettings.get(SEARCH_FIELDS_FROM_SOURCE), [uiSettings]); const { timefilter } = data.query.timefilter; @@ -132,6 +133,7 @@ export function useDiscoverState({ dataViews, toastNotifications, filterManager, + trackUiMetric, }); /** diff --git a/src/plugins/discover/public/build_services.ts b/src/plugins/discover/public/build_services.ts index 86675ae54441a..fdb5fab02cca5 100644 --- a/src/plugins/discover/public/build_services.ts +++ b/src/plugins/discover/public/build_services.ts @@ -77,7 +77,11 @@ export interface DiscoverServices { toastNotifications: ToastsStart; notifications: NotificationsStart; uiSettings: IUiSettingsClient; - trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void; + trackUiMetric?: ( + metricType: UiCounterMetricType, + eventName: string | string[], + count?: number + ) => void; dataViewFieldEditor: IndexPatternFieldEditorStart; dataViewEditor: DataViewEditorStart; http: HttpStart; diff --git a/src/plugins/discover/public/constants.ts b/src/plugins/discover/public/constants.ts index 1a36ca6972a51..e1e2e28316067 100644 --- a/src/plugins/discover/public/constants.ts +++ b/src/plugins/discover/public/constants.ts @@ -6,4 +6,6 @@ * Side Public License, v 1. */ +export const ADHOC_DATA_VIEW_CLICK_EVENT = 'ad_hoc_data_view'; + export const SEARCH_SESSION_ID_QUERY_PARAM = 'searchSessionId'; diff --git a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx index ffdead82d1dae..0d700c3cf4929 100644 --- a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx +++ b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx @@ -34,6 +34,7 @@ import { DataView, DataViewField } from '@kbn/data-views-plugin/public'; import { UiActionsStart } from '@kbn/ui-actions-plugin/public'; import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public'; import { SavedSearch } from '@kbn/saved-search-plugin/public'; +import { METRIC_TYPE } from '@kbn/analytics'; import { getSortForEmbeddable, SortPair } from '../utils/sorting'; import { RecordRawType } from '../application/main/hooks/use_saved_search'; import { buildDataTableRecord } from '../utils/build_data_record'; @@ -60,6 +61,7 @@ import { updateSearchSource } from './utils/update_search_source'; import { FieldStatisticsTable } from '../application/main/components/field_stats_table'; import { getRawRecordType } from '../application/main/utils/get_raw_record_type'; import { fetchSql } from '../application/main/utils/fetch_sql'; +import { ADHOC_DATA_VIEW_CLICK_EVENT } from '../constants'; export type SearchProps = Partial & Partial & { @@ -300,6 +302,11 @@ export class SavedSearchEmbeddable } const sort = this.getSort(this.savedSearch.sort, dataView); + if (!dataView.isPersisted()) { + // one used adhoc data view + this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT, 1); + } + const props: SearchProps = { columns: this.savedSearch.columns, dataView, From 6386b868c3fd952048aa840872122f553863b5b7 Mon Sep 17 00:00:00 2001 From: Dzmitry Tamashevich Date: Thu, 10 Nov 2022 16:26:02 +0300 Subject: [PATCH 2/4] [Discover] apply suggestions --- .../application/main/hooks/use_adhoc_data_views.ts | 12 +++--------- src/plugins/discover/public/build_services.ts | 6 +----- src/plugins/discover/public/constants.ts | 2 +- .../public/embeddable/saved_search_embeddable.tsx | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts index 7bd482348a356..2a74a3388fb45 100644 --- a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts +++ b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts @@ -17,7 +17,7 @@ import { import { ActionExecutionContext } from '@kbn/ui-actions-plugin/public'; import type { FilterManager } from '@kbn/data-plugin/public'; import type { ToastsStart } from '@kbn/core-notifications-browser'; -import { ADHOC_DATA_VIEW_CLICK_EVENT } from '../../../constants'; +import { ADHOC_DATA_VIEW_RENDER_EVENT } from '../../../constants'; import { getUiActions } from '../../../kibana_services'; import { useConfirmPersistencePrompt } from '../../../hooks/use_confirm_persistence_prompt'; import { GetStateReturn } from '../services/discover_state'; @@ -46,21 +46,15 @@ export const useAdHocDataViews = ({ !dataView.isPersisted() ? [dataView] : [] ); - useEffect(() => { - if (trackUiMetric && adHocDataViewList.length) { - trackUiMetric(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT, adHocDataViewList.length); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [adHocDataViewList]); - useEffect(() => { if (!dataView.isPersisted()) { setAdHocDataViewList((prev) => { const existing = prev.find((prevDataView) => prevDataView.id === dataView.id); return existing ? prev : [...prev, dataView]; }); + trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_RENDER_EVENT); } - }, [dataView]); + }, [dataView, trackUiMetric]); /** * Takes care of checking data view id references in filters diff --git a/src/plugins/discover/public/build_services.ts b/src/plugins/discover/public/build_services.ts index 768ddc4e27bcb..4c25f466b0fdd 100644 --- a/src/plugins/discover/public/build_services.ts +++ b/src/plugins/discover/public/build_services.ts @@ -82,11 +82,7 @@ export interface DiscoverServices { toastNotifications: ToastsStart; notifications: NotificationsStart; uiSettings: IUiSettingsClient; - trackUiMetric?: ( - metricType: UiCounterMetricType, - eventName: string | string[], - count?: number - ) => void; + trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void; dataViewFieldEditor: IndexPatternFieldEditorStart; dataViewEditor: DataViewEditorStart; http: HttpStart; diff --git a/src/plugins/discover/public/constants.ts b/src/plugins/discover/public/constants.ts index e1e2e28316067..038b0eb678ec1 100644 --- a/src/plugins/discover/public/constants.ts +++ b/src/plugins/discover/public/constants.ts @@ -6,6 +6,6 @@ * Side Public License, v 1. */ -export const ADHOC_DATA_VIEW_CLICK_EVENT = 'ad_hoc_data_view'; +export const ADHOC_DATA_VIEW_RENDER_EVENT = 'ad_hoc_data_view'; export const SEARCH_SESSION_ID_QUERY_PARAM = 'searchSessionId'; diff --git a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx index 5fdbf2daca41c..e184b1545e9c6 100644 --- a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx +++ b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx @@ -306,7 +306,7 @@ export class SavedSearchEmbeddable if (!dataView.isPersisted()) { // one used adhoc data view - this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT, 1); + this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT); } const props: SearchProps = { From 07b92b8f531b811da04d448366fd6cf94f58c8f4 Mon Sep 17 00:00:00 2001 From: Dzmitry Tamashevich Date: Thu, 10 Nov 2022 17:34:16 +0300 Subject: [PATCH 3/4] [Discover] fix types --- .../discover/public/embeddable/saved_search_embeddable.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx index e184b1545e9c6..b4931e0b783f9 100644 --- a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx +++ b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx @@ -61,7 +61,7 @@ import { updateSearchSource } from './utils/update_search_source'; import { FieldStatisticsTable } from '../application/main/components/field_stats_table'; import { getRawRecordType } from '../application/main/utils/get_raw_record_type'; import { fetchSql } from '../application/main/utils/fetch_sql'; -import { ADHOC_DATA_VIEW_CLICK_EVENT } from '../constants'; +import { ADHOC_DATA_VIEW_RENDER_EVENT } from '../constants'; export type SearchProps = Partial & Partial & { @@ -306,7 +306,7 @@ export class SavedSearchEmbeddable if (!dataView.isPersisted()) { // one used adhoc data view - this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_CLICK_EVENT); + this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_RENDER_EVENT); } const props: SearchProps = { From 4b6e717b56e193607652afb8c80113cf9723138f Mon Sep 17 00:00:00 2001 From: Dzmitry Tamashevich Date: Wed, 16 Nov 2022 12:53:14 +0300 Subject: [PATCH 4/4] [Discover] apply suggestions --- .../public/application/main/hooks/use_adhoc_data_views.ts | 2 +- .../discover/public/embeddable/saved_search_embeddable.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts index 2a74a3388fb45..e3782c698fd33 100644 --- a/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts +++ b/src/plugins/discover/public/application/main/hooks/use_adhoc_data_views.ts @@ -52,7 +52,7 @@ export const useAdHocDataViews = ({ const existing = prev.find((prevDataView) => prevDataView.id === dataView.id); return existing ? prev : [...prev, dataView]; }); - trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_RENDER_EVENT); + trackUiMetric?.(METRIC_TYPE.COUNT, ADHOC_DATA_VIEW_RENDER_EVENT); } }, [dataView, trackUiMetric]); diff --git a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx index b4931e0b783f9..2fdf48086a75a 100644 --- a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx +++ b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx @@ -306,7 +306,7 @@ export class SavedSearchEmbeddable if (!dataView.isPersisted()) { // one used adhoc data view - this.services.trackUiMetric?.(METRIC_TYPE.CLICK, ADHOC_DATA_VIEW_RENDER_EVENT); + this.services.trackUiMetric?.(METRIC_TYPE.COUNT, ADHOC_DATA_VIEW_RENDER_EVENT); } const props: SearchProps = {