diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index c30906c56c2ac..f66c4921d50d4 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -803,7 +803,7 @@ packages/kbn-yarn-lock-validator @elastic/kibana-operations /x-pack/test/functional/services/uptime @elastic/uptime /x-pack/test/api_integration/apis/uptime @elastic/uptime /x-pack/plugins/observability/public/components/shared/exploratory_view @elastic/uptime - +/x-pack/plugins/observability/public/utils/observability_data_views @elastic/uptime # Client Side Monitoring / Uptime (lives in APM directories but owned by Uptime) /x-pack/plugins/apm/public/application/uxApp.tsx @elastic/uptime diff --git a/src/plugins/data_views/common/index.ts b/src/plugins/data_views/common/index.ts index 5f7b3a544db9d..a26895f6f9d78 100644 --- a/src/plugins/data_views/common/index.ts +++ b/src/plugins/data_views/common/index.ts @@ -79,3 +79,4 @@ export type { IndexPatternLoadExpressionFunctionDefinition, } from './expressions'; export { getIndexPatternLoadMeta } from './expressions'; +export { DataViewMissingIndices } from './lib/errors'; diff --git a/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/index.tsx b/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/index.tsx index 6647d2499159c..48747afe53305 100644 --- a/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/index.tsx +++ b/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/index.tsx @@ -91,6 +91,7 @@ export function getExploratoryViewEmbeddable( ); const { dataViews, loading } = useAppDataView({ + series, dataViewCache, dataViewsService, dataTypesIndexPatterns, diff --git a/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/use_app_data_view.ts b/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/use_app_data_view.ts index 768a0ac46238c..99df5a1fb6952 100644 --- a/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/use_app_data_view.ts +++ b/x-pack/plugins/observability/public/components/shared/exploratory_view/embeddable/use_app_data_view.ts @@ -12,13 +12,16 @@ import type { ExploratoryEmbeddableProps, ObservabilityPublicPluginsStart } from import type { DataViewState } from '../hooks/use_app_data_view'; import type { AppDataType } from '../types'; import { ObservabilityDataViews } from '../../../../utils/observability_data_views/observability_data_views'; +import { SeriesUrl } from '../../../..'; export const useAppDataView = ({ + series, dataViewCache, seriesDataType, dataViewsService, dataTypesIndexPatterns, }: { + series: SeriesUrl; seriesDataType: AppDataType; dataViewCache: Record; dataViewsService: ObservabilityPublicPluginsStart['dataViews']; @@ -56,10 +59,11 @@ export const useAppDataView = ({ ); useEffect(() => { - if (seriesDataType) { + if (seriesDataType && !loading && !dataViews[seriesDataType]) { loadIndexPattern({ dataType: seriesDataType }); } - }, [seriesDataType, loadIndexPattern]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [seriesDataType, loadIndexPattern, JSON.stringify(series)]); return { dataViews, loading }; }; diff --git a/x-pack/plugins/observability/public/utils/observability_data_views/observability_data_views.ts b/x-pack/plugins/observability/public/utils/observability_data_views/observability_data_views.ts index 00552c0adc6f1..0c9ad40784ec8 100644 --- a/x-pack/plugins/observability/public/utils/observability_data_views/observability_data_views.ts +++ b/x-pack/plugins/observability/public/utils/observability_data_views/observability_data_views.ts @@ -13,6 +13,7 @@ import type { DataViewSpec, } from '@kbn/data-views-plugin/public'; import { RuntimeField } from '@kbn/data-views-plugin/public'; +import { DataViewMissingIndices } from '@kbn/data-views-plugin/common'; import { DataTypesLabels } from '../../components/shared/exploratory_view/labels'; import { syntheticsRuntimeFields } from '../../components/shared/exploratory_view/configurations/synthetics/runtime_fields'; import { getApmDataViewTitle } from '../../components/shared/exploratory_view/utils/utils'; @@ -121,25 +122,33 @@ export class ObservabilityDataViews { const { runtimeFields } = getFieldFormatsForApp(app); - const dataView = await this.dataViews.create( - { - title: appIndicesPattern, - id: getAppDataViewId(app, indices), - timeFieldName: '@timestamp', - fieldFormats: this.getFieldFormats(app), - name: DataTypesLabels[app], - }, - false, - false - ); - - if (runtimeFields !== null) { - runtimeFields.forEach(({ name, field }) => { - dataView.addRuntimeField(name, field); - }); - } + const id = getAppDataViewId(app, indices); + + try { + const dataView = await this.dataViews.create( + { + id, + title: appIndicesPattern, + timeFieldName: '@timestamp', + fieldFormats: this.getFieldFormats(app), + name: DataTypesLabels[app], + }, + false, + false + ); - return dataView; + if (runtimeFields !== null) { + runtimeFields.forEach(({ name, field }) => { + dataView.addRuntimeField(name, field); + }); + } + + return dataView; + } catch (e) { + if (e instanceof DataViewMissingIndices) { + this.dataViews.clearInstanceCache(id); + } + } } async createAndSavedDataView(app: AppDataType, indices: string) {