diff --git a/src/plugins/vis_type_timeseries/common/extract_index_patterns.test.ts b/src/plugins/vis_type_timeseries/common/index_patterns_utils.test.ts similarity index 94% rename from src/plugins/vis_type_timeseries/common/extract_index_patterns.test.ts rename to src/plugins/vis_type_timeseries/common/index_patterns_utils.test.ts index c4da2085855e6..cd8ecd54a2e90 100644 --- a/src/plugins/vis_type_timeseries/common/extract_index_patterns.test.ts +++ b/src/plugins/vis_type_timeseries/common/index_patterns_utils.test.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { extractIndexPatterns } from './extract_index_patterns'; +import { extractIndexPatterns } from './index_patterns_utils'; import { PanelSchema } from './types'; describe('extractIndexPatterns(vis)', () => { diff --git a/src/plugins/vis_type_timeseries/common/extract_index_patterns.ts b/src/plugins/vis_type_timeseries/common/index_patterns_utils.ts similarity index 75% rename from src/plugins/vis_type_timeseries/common/extract_index_patterns.ts rename to src/plugins/vis_type_timeseries/common/index_patterns_utils.ts index 9c417e69f7608..6da8cb4252711 100644 --- a/src/plugins/vis_type_timeseries/common/extract_index_patterns.ts +++ b/src/plugins/vis_type_timeseries/common/index_patterns_utils.ts @@ -41,3 +41,11 @@ export function extractIndexPatterns( return uniq(patterns).sort(); } + +export const convertIndexPatternObjectToStringRepresentation = ( + indexPatternObject: IndexPatternObject +) => + typeof indexPatternObject === 'string' ? indexPatternObject : indexPatternObject?.title ?? ''; + +export const getIndexPatternObjectKey = (indexPatternObject: IndexPatternObject) => + typeof indexPatternObject === 'string' ? indexPatternObject : indexPatternObject?.id ?? ''; diff --git a/src/plugins/vis_type_timeseries/common/types.ts b/src/plugins/vis_type_timeseries/common/types.ts index 4139de8403c59..f13e9e1f7d97f 100644 --- a/src/plugins/vis_type_timeseries/common/types.ts +++ b/src/plugins/vis_type_timeseries/common/types.ts @@ -7,7 +7,14 @@ */ import { TypeOf } from '@kbn/config-schema'; -import { metricsItems, panel, seriesItems, visPayloadSchema, fieldObject, indexPattern } from './vis_schema'; +import { + metricsItems, + panel, + seriesItems, + visPayloadSchema, + fieldObject, + indexPattern, +} from './vis_schema'; import { PANEL_TYPES } from './panel_types'; import { TimeseriesUIRestrictions } from './ui_restrictions'; diff --git a/src/plugins/vis_type_timeseries/public/application/components/aggs/field_select.tsx b/src/plugins/vis_type_timeseries/public/application/components/aggs/field_select.tsx index 7d9d81a8a966e..dcdb706f1299b 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/aggs/field_select.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/aggs/field_select.tsx @@ -10,8 +10,8 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { EuiComboBox, EuiComboBoxProps, EuiComboBoxOptionOption } from '@elastic/eui'; import { METRIC_TYPES } from '../../../../common/metric_types'; - -import type { SanitizedFieldType } from '../../../../common/types'; +import { getIndexPatternObjectKey } from '../../../../common/index_patterns_utils'; +import type { SanitizedFieldType, IndexPatternObject } from '../../../../common/types'; import type { TimeseriesUIRestrictions } from '../../../../common/ui_restrictions'; // @ts-ignore @@ -20,7 +20,7 @@ import { isFieldEnabled } from '../../lib/check_ui_restrictions'; interface FieldSelectProps { type: string; fields: Record; - indexPattern: string; + indexPattern: IndexPatternObject; value: string; onChange: (options: Array>) => void; disabled?: boolean; @@ -62,8 +62,10 @@ export function FieldSelect({ const selectedOptions: Array> = []; let newPlaceholder = placeholder; + const fieldsSelector = getIndexPatternObjectKey(indexPattern); + const groupedOptions: EuiComboBoxProps['options'] = Object.values( - (fields[indexPattern] || []).reduce>>( + (fields[fieldsSelector] || []).reduce>>( (acc, field) => { if (placeholder === field?.name) { newPlaceholder = field.label ?? field.name; diff --git a/src/plugins/vis_type_timeseries/public/application/components/annotations_editor.js b/src/plugins/vis_type_timeseries/public/application/components/annotations_editor.js index f95eeb4816128..0130e636c28b5 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/annotations_editor.js +++ b/src/plugins/vis_type_timeseries/public/application/components/annotations_editor.js @@ -34,6 +34,7 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; +import { IndexPatternSelect } from './lib/index_pattern_select'; function newAnnotation() { return { @@ -125,11 +126,11 @@ export class AnnotationsEditor extends Component { } fullWidth > - diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern.js b/src/plugins/vis_type_timeseries/public/application/components/index_pattern.js index a7cb2e0bbbe64..a9bde15120dab 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern.js +++ b/src/plugins/vis_type_timeseries/public/application/components/index_pattern.js @@ -24,6 +24,7 @@ import { import { FieldSelect } from './aggs/field_select'; import { createSelectHandler } from './lib/create_select_handler'; import { createTextHandler } from './lib/create_text_handler'; +import { IndexPatternSelect } from './lib/index_pattern_select'; import { YesNo } from './yes_no'; import { KBN_FIELD_TYPES } from '../../../../../plugins/data/public'; import { FormValidationContext } from '../contexts/form_validation_context'; @@ -37,7 +38,6 @@ import { VisDataContext } from '../contexts/vis_data_context'; import { getUISettings } from '../../services'; import { AUTO_INTERVAL } from '../../../common/constants'; import { UI_SETTINGS } from '../../../../data/common'; -import { IndexPatternSelect } from './index_pattern_select'; const RESTRICT_FIELDS = [KBN_FIELD_TYPES.DATE]; const LEVEL_OF_DETAIL_STEPS = 10; diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/combo_box_select.tsx b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/combo_box_select.tsx similarity index 92% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/combo_box_select.tsx rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/combo_box_select.tsx index 4371ab6ad90b6..a564d5aacd573 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/combo_box_select.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/combo_box_select.tsx @@ -9,11 +9,11 @@ import React, { useCallback, useState, useEffect } from 'react'; import type { UnwrapPromise } from '@kbn/utility-types'; import { EuiComboBox, EuiComboBoxProps } from '@elastic/eui'; -import { getDataStart } from '../../../services'; +import { getDataStart } from '../../../../services'; import type { SelectIndexComponentProps } from './types'; -import type { IndexPatternObject } from '../../../../common/types'; -import type { IndexPatternsService } from '../../../../../data/public'; +import type { IndexPatternObject } from '../../../../../common/types'; +import type { IndexPatternsService } from '../../../../../../data/public'; import { SwitchModePopover } from './switch_mode_popover'; /** @internal **/ diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/field_text_select.tsx b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/field_text_select.tsx similarity index 86% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/field_text_select.tsx rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/field_text_select.tsx index 61d109e4bb822..22e2bb31d6d3d 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/field_text_select.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/field_text_select.tsx @@ -10,11 +10,9 @@ import React, { useCallback } from 'react'; import { EuiFieldText, EuiFieldTextProps } from '@elastic/eui'; import { LegacyModePopover } from './legacy_mode_popover'; import { SwitchModePopover } from './switch_mode_popover'; +import { convertIndexPatternObjectToStringRepresentation } from '../../../../../common/index_patterns_utils'; import type { SelectIndexComponentProps } from './types'; -import type { IndexPatternObject } from '../../../../common/types'; - -const toTextValue = (v: IndexPatternObject) => (typeof v === 'string' ? v : v?.title ?? ''); export const FieldTextSelect = ({ onIndexChange, @@ -24,7 +22,7 @@ export const FieldTextSelect = ({ onModeChange, allowSwitchUseKibanaIndexesMode, }: SelectIndexComponentProps) => { - const textualValue = toTextValue(value); + const textualValue = convertIndexPatternObjectToStringRepresentation(value); const append = []; const onFieldTextChange: EuiFieldTextProps['onChange'] = useCallback( diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/index.ts b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/index.ts similarity index 100% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/index.ts rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/index.ts diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/index_pattern_select.tsx b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/index_pattern_select.tsx similarity index 93% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/index_pattern_select.tsx rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/index_pattern_select.tsx index 003f8299c18c4..39069af85f134 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/index_pattern_select.tsx @@ -10,8 +10,8 @@ import React, { useState, useContext, useCallback } from 'react'; import useDebounce from 'react-use/lib/useDebounce'; import { FieldTextSelect } from './field_text_select'; import { ComboBoxSelect } from './combo_box_select'; -import { PanelModelContext } from '../../contexts/panel_model_context'; -import type { IndexPatternObject } from '../../../../common/types'; +import { PanelModelContext } from '../../../contexts/panel_model_context'; +import type { IndexPatternObject } from '../../../../../common/types'; const USE_KIBANA_INDEXES_KEY = 'use_kibana_indexes'; diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/legacy_mode_popover.tsx b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/legacy_mode_popover.tsx similarity index 98% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/legacy_mode_popover.tsx rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/legacy_mode_popover.tsx index f3948ba4374bc..e932f465a2207 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/legacy_mode_popover.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/legacy_mode_popover.tsx @@ -10,7 +10,7 @@ import React, { useState, useCallback, useEffect } from 'react'; import { EuiTextColor, EuiButtonIcon, EuiPopover, EuiButton, EuiCallOut } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; -import { getCoreStart, getDataStart } from '../../../services'; +import { getCoreStart, getDataStart } from '../../../../services'; interface LegacyModePopoverProps { index: string; diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/switch_mode_popover.tsx b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/switch_mode_popover.tsx similarity index 100% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/switch_mode_popover.tsx rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/switch_mode_popover.tsx diff --git a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/types.ts b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/types.ts similarity index 90% rename from src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/types.ts rename to src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/types.ts index ea231ef0d6788..6ed6f183d4e42 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/index_pattern_select/types.ts +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/index_pattern_select/types.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -import type { IndexPatternObject } from '../../../../common/types'; +import type { IndexPatternObject } from '../../../../../common/types'; export interface SelectIndexComponentProps { value: IndexPatternObject; diff --git a/src/plugins/vis_type_timeseries/public/application/components/panel_config/table.js b/src/plugins/vis_type_timeseries/public/application/components/panel_config/table.js index 07219e70692b8..65c4a86753da0 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/panel_config/table.js +++ b/src/plugins/vis_type_timeseries/public/application/components/panel_config/table.js @@ -36,6 +36,7 @@ import { QueryBarWrapper } from '../query_bar_wrapper'; import { getDefaultQueryLanguage } from '../lib/get_default_query_language'; import { VisDataContext } from '../../contexts/vis_data_context'; import { BUCKET_TYPES } from '../../../../common/metric_types'; +import { getIndexPatternObjectKey } from '../../../../common/index_patterns_utils'; export class TablePanelConfig extends Component { static contextType = VisDataContext; constructor(props) { @@ -59,7 +60,9 @@ export class TablePanelConfig extends Component { handlePivotChange = (selectedOption) => { const { fields, model } = this.props; const pivotId = get(selectedOption, '[0].value', null); - const field = fields[model.index_pattern].find((field) => field.name === pivotId); + const field = fields[getIndexPatternObjectKey(model.index_pattern)].find( + (field) => field.name === pivotId + ); const pivotType = get(field, 'type', model.pivot_type); this.props.onChange({ diff --git a/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.js b/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.js deleted file mode 100644 index af8404eb6da92..0000000000000 --- a/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import React, { useContext } from 'react'; -import { CoreStartContext } from '../contexts/query_input_bar_context'; -import { QueryStringInput } from '../../../../../plugins/data/public'; - -export function QueryBarWrapper(props) { - const coreStartContext = useContext(CoreStartContext); - - return ; -} diff --git a/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.tsx b/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.tsx new file mode 100644 index 0000000000000..c74f37ce58c84 --- /dev/null +++ b/src/plugins/vis_type_timeseries/public/application/components/query_bar_wrapper.tsx @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React, { useContext } from 'react'; + +import { CoreStartContext } from '../contexts/query_input_bar_context'; +import { IndexPatternObject } from '../../../common/types'; +import { convertIndexPatternObjectToStringRepresentation } from '../../../common/index_patterns_utils'; + +import { QueryStringInput, QueryStringInputProps } from '../../../../../plugins/data/public'; + +type QueryBarWrapperProps = Pick & { + indexPatterns: IndexPatternObject[]; +}; + +export function QueryBarWrapper({ query, onChange, indexPatterns }: QueryBarWrapperProps) { + const coreStartContext = useContext(CoreStartContext); + + return ( + + ); +} diff --git a/src/plugins/vis_type_timeseries/public/application/components/vis_editor.js b/src/plugins/vis_type_timeseries/public/application/components/vis_editor.js index 11586628ea005..cabe6798fbed0 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/vis_editor.js +++ b/src/plugins/vis_type_timeseries/public/application/components/vis_editor.js @@ -15,7 +15,7 @@ import { VisEditorVisualization } from './vis_editor_visualization'; import { VisPicker } from './vis_picker'; import { PanelConfig } from './panel_config'; import { fetchFields } from '../lib/fetch_fields'; -import { extractIndexPatterns } from '../../../common/extract_index_patterns'; +import { extractIndexPatterns } from '../../../common/index_patterns_utils'; import { getSavedObjectsClient, getUISettings, getDataStart, getCoreStart } from '../../services'; import { CoreStartContextProvider } from '../contexts/query_input_bar_context'; 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 4b41747f23c79..0f87069a08b7f 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 @@ -9,10 +9,11 @@ import { i18n } from '@kbn/i18n'; import { getCoreStart, getDataStart } from '../../services'; import { ROUTES } from '../../../common/constants'; -import { SanitizedFieldType } from '../../../common/types'; +import { SanitizedFieldType, IndexPatternObject } from '../../../common/types'; +import { getIndexPatternObjectKey } from '../../../common/index_patterns_utils'; export async function fetchFields( - indexes: string[] = [], + indexes: IndexPatternObject[] = [], signal?: AbortSignal ): Promise> { const patterns = Array.isArray(indexes) ? indexes : [indexes]; @@ -23,19 +24,23 @@ export async function fetchFields( const defaultIndexPattern = await dataStart.indexPatterns.getDefault(); const indexFields = await Promise.all( patterns.map(async (pattern) => { - return coreStart.http.get(ROUTES.FIELDS, { - query: { - index: pattern, - }, - signal, - }); + if (typeof pattern !== 'string' && pattern?.id) { + return (await dataStart.indexPatterns.get(pattern.id)).getNonScriptedFields(); + } else { + return coreStart.http.get(ROUTES.FIELDS, { + query: { + index: `${pattern ?? ''}`, + }, + signal, + }); + } }) ); const fields: Record = patterns.reduce( (cumulatedFields, currentPattern, index) => ({ ...cumulatedFields, - [currentPattern]: indexFields[index], + [getIndexPatternObjectKey(currentPattern)]: indexFields[index], }), {} ); diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategy_registry.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategy_registry.ts index 83688cded3776..c77c422c77687 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategy_registry.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/search_strategy_registry.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { extractIndexPatterns } from '../../../common/extract_index_patterns'; +import { extractIndexPatterns } from '../../../common/index_patterns_utils'; import { PanelSchema } from '../../../common/types'; import { AbstractSearchStrategy, ReqFacade } from './strategies';