Skip to content

Commit

Permalink
[Discover] Track adhoc data views usage (#144244)
Browse files Browse the repository at this point in the history
## Summary

Closes #141090

This PR enabled metrics for adhoc data views usage in Discover.

Co-authored-by: Julia Rechkunova <[email protected]>
  • Loading branch information
dimaanj and jughosta authored Nov 17, 2022
1 parent 90f0ae2 commit 2454fd0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
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,
} from '@kbn/unified-search-plugin/public';
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_RENDER_EVENT } from '../../../constants';
import { getUiActions } from '../../../kibana_services';
import { useConfirmPersistencePrompt } from '../../../hooks/use_confirm_persistence_prompt';
import { GetStateReturn } from '../services/discover_state';
Expand All @@ -29,6 +31,7 @@ export const useAdHocDataViews = ({
filterManager,
dataViews,
toastNotifications,
trackUiMetric,
}: {
dataView: DataView;
savedSearch: SavedSearch;
Expand All @@ -37,6 +40,7 @@ export const useAdHocDataViews = ({
dataViews: DataViewsContract;
filterManager: FilterManager;
toastNotifications: ToastsStart;
trackUiMetric?: (metricType: string, eventName: string | string[], count?: number) => void;
}) => {
const [adHocDataViewList, setAdHocDataViewList] = useState<DataView[]>(
!dataView.isPersisted() ? [dataView] : []
Expand All @@ -48,8 +52,9 @@ export const useAdHocDataViews = ({
const existing = prev.find((prevDataView) => prevDataView.id === dataView.id);
return existing ? prev : [...prev, dataView];
});
trackUiMetric?.(METRIC_TYPE.COUNT, ADHOC_DATA_VIEW_RENDER_EVENT);
}
}, [dataView]);
}, [dataView, trackUiMetric]);

/**
* Takes care of checking data view id references in filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -133,6 +134,7 @@ export function useDiscoverState({
setUrlTracking,
filterManager,
toastNotifications,
trackUiMetric,
});

const [savedDataViewList, setSavedDataViewList] = useState(initialDataViewList);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/discover/public/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
* Side Public License, v 1.
*/

export const ADHOC_DATA_VIEW_RENDER_EVENT = 'ad_hoc_data_view';

export const SEARCH_SESSION_ID_QUERY_PARAM = 'searchSessionId';
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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_RENDER_EVENT } from '../constants';

export type SearchProps = Partial<DiscoverGridProps> &
Partial<DocTableProps> & {
Expand Down Expand Up @@ -302,6 +304,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.COUNT, ADHOC_DATA_VIEW_RENDER_EVENT);
}

const props: SearchProps = {
columns: this.savedSearch.columns,
savedSearchId: this.savedSearch.id,
Expand Down

0 comments on commit 2454fd0

Please sign in to comment.