Skip to content

Commit

Permalink
rework logic and fall back to default time range
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed Jun 12, 2023
1 parent 6a3afdc commit 3504c10
Showing 1 changed file with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import {
CONTROL_GROUP_TYPE,
getDefaultControlGroupInput,
} from '@kbn/controls-plugin/common';
import { GlobalQueryStateFromUrl, syncGlobalQueryStateWithUrl } from '@kbn/data-plugin/public';
import { TimeRange } from '@kbn/es-query';
import { isErrorEmbeddable, ViewMode } from '@kbn/embeddable-plugin/public';
import { lazyLoadReduxToolsPackage } from '@kbn/presentation-util-plugin/public';
import { type ControlGroupContainer, ControlGroupOutput } from '@kbn/controls-plugin/public';
import { GlobalQueryStateFromUrl, syncGlobalQueryStateWithUrl } from '@kbn/data-plugin/public';

import { DashboardContainerInput } from '../../../../common';
import { DashboardContainer } from '../dashboard_container';
import { pluginServices } from '../../../services/plugin_services';
import { DEFAULT_DASHBOARD_INPUT, GLOBAL_STATE_STORAGE_KEY } from '../../../dashboard_constants';
import { DashboardCreationOptions } from '../dashboard_container_factory';
import { startSyncingDashboardDataViews } from './data_views/sync_dashboard_data_views';
import { syncUnifiedSearchState } from './unified_search/sync_dashboard_unified_search_state';
import { DEFAULT_DASHBOARD_INPUT, GLOBAL_STATE_STORAGE_KEY } from '../../../dashboard_constants';
import { startSyncingDashboardControlGroup } from './controls/dashboard_control_group_integration';
import { startDashboardSearchSessionIntegration } from './search_sessions/start_dashboard_search_session_integration';
import { LoadDashboardFromSavedObjectReturn } from '../../../services/dashboard_saved_object/lib/load_dashboard_state_from_saved_object';
Expand Down Expand Up @@ -174,25 +175,38 @@ export const initializeDashboard = async ({
// Set up unified search integration.
// --------------------------------------------------------------------------------------
if (useUnifiedSearchIntegration && unifiedSearchSettings?.kbnUrlStateStorage) {
const { filters, query, timeRestore, timeRange, refreshInterval } = initialInput;
const {
query,
filters,
timeRestore,
timeRange: savedTimeRange,
refreshInterval: savedRefreshInterval,
} = initialInput;
const { kbnUrlStateStorage } = unifiedSearchSettings;

// apply filters and query to the query service
filterManager.setAppFilters(cloneDeep(filters ?? []));
queryString.setQuery(query ?? queryString.getDefaultQuery());

/**
* If a global time range is not set explicitly and the time range was saved with the dashboard, apply
* time range and refresh interval to the query service. Otherwise, set the current dashboard time range
* from the query service. The order of the following lines is very important.
* Get initial time range, and set up dashboard time restore if applicable
*/
const inheritedTimeRange =
kbnUrlStateStorage.get<GlobalQueryStateFromUrl>(GLOBAL_STATE_STORAGE_KEY)?.time;
if (inheritedTimeRange || !timeRestore) {
initialInput.timeRange = inheritedTimeRange;
} else if (timeRestore) {
if (timeRange) timefilterService.setTime(timeRange);
if (refreshInterval) timefilterService.setRefreshInterval(refreshInterval);
const initialTimeRange: TimeRange = (() => {
// if there is an explicit time range in the URL it always takes precedence.
const urlOverrideTimeRange =
kbnUrlStateStorage.get<GlobalQueryStateFromUrl>(GLOBAL_STATE_STORAGE_KEY)?.time;
if (urlOverrideTimeRange) return urlOverrideTimeRange;

// if this Dashboard has timeRestore return the time range that was saved with the dashboard.
if (timeRestore && savedTimeRange) return savedTimeRange;

// otherwise fall back to the time range from the timefilterService.
return timefilterService.getTime();
})();
initialInput.timeRange = initialTimeRange;
if (timeRestore) {
if (savedTimeRange) timefilterService.setTime(savedTimeRange);
if (savedRefreshInterval) timefilterService.setRefreshInterval(savedRefreshInterval);
}

// start syncing global query state with the URL.
Expand Down

0 comments on commit 3504c10

Please sign in to comment.