diff --git a/src/plugins/vis_type_timeseries/public/application/lib/fetch_fields.ts b/src/plugins/vis_type_timeseries/public/application/lib/fetch_fields.ts index a9a5f8a0b47c..af3ddd643cac 100644 --- a/src/plugins/vis_type_timeseries/public/application/lib/fetch_fields.ts +++ b/src/plugins/vis_type_timeseries/public/application/lib/fetch_fields.ts @@ -50,9 +50,10 @@ export async function fetchFields( {} ); - if (defaultIndexPattern?.title && patterns.includes(defaultIndexPattern.title)) { - fields[''] = fields[defaultIndexPattern.title]; + if (defaultIndexPattern) { + fields[''] = toSanitizedFieldType(await defaultIndexPattern.getNonScriptedFields()); } + return fields; } catch (error) { if (error.name !== 'AbortError') { diff --git a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/controls_references.ts b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/controls_references.ts index 9419efad41b8..e584270425eb 100644 --- a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/controls_references.ts +++ b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/controls_references.ts @@ -7,12 +7,13 @@ */ import { SavedObjectReference } from '../../../../../core/types'; +import { VisParams } from '../../../common'; export const extractControlsReferences = ( - visState: Record, + visParams: VisParams, references: SavedObjectReference[] = [] ) => { - const controls = visState.params?.controls ?? []; + const controls = visParams?.controls ?? []; controls.forEach((control: Record, i: number) => { if (!control.indexPattern) { @@ -29,10 +30,10 @@ export const extractControlsReferences = ( }; export const injectControlsReferences = ( - visState: Record, + visParams: VisParams, references: SavedObjectReference[] ) => { - const controls = visState.params?.controls ?? []; + const controls = visParams.controls ?? []; controls.forEach((control: Record) => { if (!control.indexPatternRefName) { diff --git a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/index.ts b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/index.ts index 727005b7f183..0acda1c0a0f8 100644 --- a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/index.ts +++ b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/index.ts @@ -6,4 +6,7 @@ * Side Public License, v 1. */ +export { extractControlsReferences, injectControlsReferences } from './controls_references'; +export { extractTimeSeriesReferences, injectTimeSeriesReferences } from './timeseries_references'; + export { extractReferences, injectReferences } from './saved_visualization_references'; diff --git a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/saved_visualization_references.ts b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/saved_visualization_references.ts index 4afd1bf5980a..66ef9a1d7d2d 100644 --- a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/saved_visualization_references.ts +++ b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/saved_visualization_references.ts @@ -11,7 +11,7 @@ import { SavedObjectAttributes, SavedObjectReference, } from '../../../../../core/public'; -import { VisSavedObject } from '../../types'; +import { SavedVisState, VisSavedObject } from '../../types'; import { extractSearchSourceReferences, injectSearchSourceReferences, @@ -52,10 +52,13 @@ export function extractReferences({ // Extract index patterns from controls if (updatedAttributes.visState) { - const visState = JSON.parse(String(updatedAttributes.visState)); + const visState = JSON.parse(String(updatedAttributes.visState)) as SavedVisState; - extractControlsReferences(visState, updatedReferences); - extractTimeSeriesReferences(visState, updatedReferences); + extractControlsReferences(visState.params, updatedReferences); + + if (visState.type === 'metrics') { + extractTimeSeriesReferences(visState.params, updatedReferences); + } updatedAttributes.visState = JSON.stringify(visState); } @@ -85,7 +88,10 @@ export function injectReferences(savedObject: VisSavedObject, references: SavedO } if (savedObject.visState?.params) { - injectControlsReferences(savedObject.visState, references); - injectTimeSeriesReferences(savedObject.visState, references); + injectControlsReferences(savedObject.visState.params, references); + + if (savedObject.visState.type === 'metrics') { + injectTimeSeriesReferences(savedObject.visState.params, references); + } } } diff --git a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/timeseries_references.ts b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/timeseries_references.ts index 2a3fccb79779..845da0d011ed 100644 --- a/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/timeseries_references.ts +++ b/src/plugins/visualizations/public/saved_visualizations/saved_visualization_references/timeseries_references.ts @@ -7,6 +7,7 @@ */ import { SavedObjectReference } from '../../../../../core/types'; +import { VisParams } from '../../../common'; /** @internal **/ const REF_NAME_POSTFIX = '_ref_name'; @@ -17,28 +18,24 @@ const INDEX_PATTERN_REF_TYPE = 'index_pattern'; /** @internal **/ type Action = (object: Record, key: string) => void; -const doForExtractedIndices = (action: Action, visState: Record) => { - if (visState.type !== 'metrics') { - return; - } - - action(visState.params, 'index_pattern'); +const doForExtractedIndices = (action: Action, visParams: VisParams) => { + action(visParams, 'index_pattern'); - visState.params.series.forEach((series: any) => { + visParams.series.forEach((series: any) => { if (series.override_index_pattern) { action(series, 'series_index_pattern'); } }); - if (visState.params.annotations) { - visState.params.annotations.forEach((annotation: any) => { + if (visParams.annotations) { + visParams.annotations.forEach((annotation: any) => { action(annotation, 'index_pattern'); }); } }; export const extractTimeSeriesReferences = ( - visState: Record, + visParams: VisParams, references: SavedObjectReference[] = [] ) => { let i = 0; @@ -55,11 +52,11 @@ export const extractTimeSeriesReferences = ( }); delete object[key]; } - }, visState); + }, visParams); }; export const injectTimeSeriesReferences = ( - visState: Record, + visParams: VisParams, references: SavedObjectReference[] ) => { doForExtractedIndices((object, key) => { @@ -74,5 +71,5 @@ export const injectTimeSeriesReferences = ( delete object[refKey]; } - }, visState); + }, visParams); };