From 30f807f4ae0d613056844cc05e3e3ce04b8af06f Mon Sep 17 00:00:00 2001 From: Uladzislau Lasitsa Date: Thu, 14 Oct 2021 18:54:47 +0300 Subject: [PATCH 1/4] Move logic about default time range mode inside index pattern component --- .../vis_types/timeseries/common/types/panel_model.ts | 1 + .../public/application/components/index_pattern.js | 10 +++++++++- .../public/application/components/vis_editor.tsx | 11 ++--------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/plugins/vis_types/timeseries/common/types/panel_model.ts b/src/plugins/vis_types/timeseries/common/types/panel_model.ts index f71602fdf0443..965f09b56f265 100644 --- a/src/plugins/vis_types/timeseries/common/types/panel_model.ts +++ b/src/plugins/vis_types/timeseries/common/types/panel_model.ts @@ -169,5 +169,6 @@ export interface Panel { time_range_mode?: string; tooltip_mode?: TOOLTIP_MODES; type: PANEL_TYPES; + isNew?: boolean; use_kibana_indexes?: boolean; } diff --git a/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js b/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js index e5d09c745b522..75560da07c25d 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js +++ b/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js @@ -28,6 +28,7 @@ import { YesNo } from './yes_no'; import { LastValueModePopover } from './last_value_mode_popover'; import { KBN_FIELD_TYPES } from '../../../../../data/public'; import { FormValidationContext } from '../contexts/form_validation_context'; +import { PanelModelContext } from '../contexts/panel_model_context'; import { isGteInterval, validateReInterval, isAutoInterval } from './lib/get_interval'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; @@ -76,6 +77,7 @@ export const IndexPattern = ({ const maxBarsName = `${prefix}max_bars`; const dropBucketName = `${prefix}drop_last_bucket`; const updateControlValidity = useContext(FormValidationContext); + const panelModel = useContext(PanelModelContext); const uiRestrictions = get(useContext(VisDataContext), 'uiRestrictions'); const maxBarsUiSettings = config.get(UI_SETTINGS.HISTOGRAM_MAX_BARS); @@ -116,7 +118,13 @@ export const IndexPattern = ({ [intervalName]: AUTO_INTERVAL, [dropBucketName]: 0, [maxBarsName]: config.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET), - [TIME_RANGE_MODE_KEY]: timeRangeOptions[0].value, + // we should set default value for 'time_range_mode' in model so that when user save visualization + // we set right mode in savedObject + // ternary operator needed because old visualization have 'time_range_mode' as undefined for 'last_value' + // but for creating new visaulization we should use 'entire_timerange' as default. + [TIME_RANGE_MODE_KEY]: panelModel.isNew + ? TIME_RANGE_DATA_MODES.ENTIRE_TIME_RANGE + : TIME_RANGE_DATA_MODES.LAST_VALUE, }; const model = { ...defaults, ..._model }; diff --git a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx index 9e46427e33c2e..a03d6252ee5d8 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx +++ b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx @@ -70,15 +70,8 @@ export class VisEditor extends Component Date: Thu, 14 Oct 2021 19:08:42 +0300 Subject: [PATCH 2/4] Fix lint --- .../timeseries/public/application/components/vis_editor.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx index a03d6252ee5d8..e29f64ce5e8eb 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx +++ b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx @@ -71,7 +71,7 @@ export class VisEditor extends Component Date: Thu, 14 Oct 2021 19:58:01 +0300 Subject: [PATCH 3/4] Fix types --- .../timeseries/public/application/components/vis_editor.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx index e29f64ce5e8eb..78abaa6397536 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx +++ b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx @@ -26,7 +26,6 @@ import type { IndexPatternValue, TimeseriesVisData } from '../../../common/types import { VisEditorVisualization } from './vis_editor_visualization'; import { PanelConfig } from './panel_config'; import { extractIndexPatternValues } from '../../../common/index_patterns_utils'; -import { TIME_RANGE_DATA_MODES, TIME_RANGE_MODE_KEY } from '../../../common/enums'; import { VisPicker } from './vis_picker'; import { fetchFields, VisFields } from '../lib/fetch_fields'; import { getDataStart, getCoreStart } from '../../services'; From cdb7a153fb46985e0c34e5252d2ca03f6b4e3644 Mon Sep 17 00:00:00 2001 From: Uladzislau Lasitsa Date: Thu, 14 Oct 2021 22:09:46 +0300 Subject: [PATCH 4/4] Cover case with override_index_pattern --- .../timeseries/common/types/panel_model.ts | 1 - .../application/components/index_pattern.js | 10 +--------- .../application/components/vis_editor.tsx | 19 ++++++++++++++++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/plugins/vis_types/timeseries/common/types/panel_model.ts b/src/plugins/vis_types/timeseries/common/types/panel_model.ts index 965f09b56f265..f71602fdf0443 100644 --- a/src/plugins/vis_types/timeseries/common/types/panel_model.ts +++ b/src/plugins/vis_types/timeseries/common/types/panel_model.ts @@ -169,6 +169,5 @@ export interface Panel { time_range_mode?: string; tooltip_mode?: TOOLTIP_MODES; type: PANEL_TYPES; - isNew?: boolean; use_kibana_indexes?: boolean; } diff --git a/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js b/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js index 75560da07c25d..e5d09c745b522 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js +++ b/src/plugins/vis_types/timeseries/public/application/components/index_pattern.js @@ -28,7 +28,6 @@ import { YesNo } from './yes_no'; import { LastValueModePopover } from './last_value_mode_popover'; import { KBN_FIELD_TYPES } from '../../../../../data/public'; import { FormValidationContext } from '../contexts/form_validation_context'; -import { PanelModelContext } from '../contexts/panel_model_context'; import { isGteInterval, validateReInterval, isAutoInterval } from './lib/get_interval'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; @@ -77,7 +76,6 @@ export const IndexPattern = ({ const maxBarsName = `${prefix}max_bars`; const dropBucketName = `${prefix}drop_last_bucket`; const updateControlValidity = useContext(FormValidationContext); - const panelModel = useContext(PanelModelContext); const uiRestrictions = get(useContext(VisDataContext), 'uiRestrictions'); const maxBarsUiSettings = config.get(UI_SETTINGS.HISTOGRAM_MAX_BARS); @@ -118,13 +116,7 @@ export const IndexPattern = ({ [intervalName]: AUTO_INTERVAL, [dropBucketName]: 0, [maxBarsName]: config.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET), - // we should set default value for 'time_range_mode' in model so that when user save visualization - // we set right mode in savedObject - // ternary operator needed because old visualization have 'time_range_mode' as undefined for 'last_value' - // but for creating new visaulization we should use 'entire_timerange' as default. - [TIME_RANGE_MODE_KEY]: panelModel.isNew - ? TIME_RANGE_DATA_MODES.ENTIRE_TIME_RANGE - : TIME_RANGE_DATA_MODES.LAST_VALUE, + [TIME_RANGE_MODE_KEY]: timeRangeOptions[0].value, }; const model = { ...defaults, ..._model }; diff --git a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx index 78abaa6397536..caf7ac638af78 100644 --- a/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx +++ b/src/plugins/vis_types/timeseries/public/application/components/vis_editor.tsx @@ -26,6 +26,7 @@ import type { IndexPatternValue, TimeseriesVisData } from '../../../common/types import { VisEditorVisualization } from './vis_editor_visualization'; import { PanelConfig } from './panel_config'; import { extractIndexPatternValues } from '../../../common/index_patterns_utils'; +import { TIME_RANGE_DATA_MODES, TIME_RANGE_MODE_KEY } from '../../../common/enums'; import { VisPicker } from './vis_picker'; import { fetchFields, VisFields } from '../lib/fetch_fields'; import { getDataStart, getCoreStart } from '../../services'; @@ -69,8 +70,24 @@ export class VisEditor extends Component ({ + [TIME_RANGE_MODE_KEY]: + this.props.vis.title && + this.props.vis.params.type !== 'timeseries' && + val.override_index_pattern + ? TIME_RANGE_DATA_MODES.LAST_VALUE + : TIME_RANGE_DATA_MODES.ENTIRE_TIME_RANGE, + ...val, + })), }, extractedIndexPatterns: [''], };