Skip to content

Commit

Permalink
[APM] Fix APM search bar (8.4 backport only)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Sep 26, 2022
1 parent aaf4230 commit a3ce25e
Showing 1 changed file with 4 additions and 35 deletions.
39 changes: 4 additions & 35 deletions x-pack/plugins/apm/public/hooks/use_apm_data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@

import { DataView } from '@kbn/data-views-plugin/common';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
import { useEffect, useState } from 'react';
import { APM_STATIC_DATA_VIEW_ID } from '../../common/data_view_constants';
import { useApmPluginContext } from '../context/apm_plugin/use_apm_plugin_context';
import { ApmPluginStartDeps } from '../plugin';
import { callApmApi } from '../services/rest/create_call_apm_api';

async function createStaticApmDataView() {
const res = await callApmApi('POST /internal/apm/data_view/static', {
signal: null,
});
return res.dataView;
}

async function getApmDataViewTitle() {
const res = await callApmApi('GET /internal/apm/data_view/title', {
signal: null,
Expand All @@ -30,37 +20,16 @@ async function getApmDataViewTitle() {

export function useApmDataView() {
const { services } = useKibana<ApmPluginStartDeps>();
const { core } = useApmPluginContext();
const [dataView, setDataView] = useState<DataView | undefined>();

const canCreateDataView =
core.application.capabilities.savedObjectsManagement.edit;

useEffect(() => {
async function fetchDataView() {
try {
// load static data view
return await services.dataViews.get(APM_STATIC_DATA_VIEW_ID);
} catch (e) {
// re-throw if an unhandled error occurred
const notFound = e instanceof SavedObjectNotFound;
if (!notFound) {
throw e;
}

// create static data view if user has permissions
if (canCreateDataView) {
return createStaticApmDataView();
} else {
// or create dynamic data view if user does not have permissions to create a static
const title = await getApmDataViewTitle();
return services.dataViews.create({ title });
}
}
const title = await getApmDataViewTitle();
return services.dataViews.create({ title });
}

fetchDataView().then((dv) => setDataView(dv));
}, [canCreateDataView, services.dataViews]);
fetchDataView().then(setDataView);
}, [services.dataViews]);

return { dataView };
}

0 comments on commit a3ce25e

Please sign in to comment.