From 956b18d145c916a9e2c04855acda1f8d8aeb1cdd Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Wed, 4 Sep 2019 21:14:10 +0300 Subject: [PATCH] =?UTF-8?q?Move=20timefilter=20code=20=E2=87=92=20data=20p?= =?UTF-8?q?lugin=20(#44607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * import TimeRange from NP * Update imports * Update RefreshInterval imports to NP * Some more imports * Moved timefilter to data plugin (didn't rewire service!) * Fixed eslint error * remove virtual mock * kibana context import * Fix 'Monitoring is turned off' test * fix import --- .../lib/embeddable/dashboard_container.tsx | 2 +- .../data/public/search/search_bar/index.tsx | 2 +- .../data}/public/timefilter/get_time.test.ts | 0 .../data}/public/timefilter/get_time.ts | 0 .../data/public/timefilter/index.ts | 22 +++++ .../lib/diff_time_picker_vals.test.ts | 0 .../timefilter/lib/diff_time_picker_vals.ts | 0 .../timefilter/lib/parse_querystring.ts | 0 .../data}/public/timefilter/time_history.ts | 4 +- .../timefilter/timefilter.test.mocks.ts | 2 +- .../public/timefilter/timefilter.test.ts | 64 ++++++------- .../data}/public/timefilter/timefilter.ts | 83 +++-------------- .../kibana/public/dashboard/dashboard_app.tsx | 2 +- .../public/dashboard/dashboard_state.test.ts | 3 +- .../dashboard/lib/update_saved_dashboard.ts | 3 +- .../saved_dashboard/saved_dashboard.d.ts | 2 +- .../discover/embeddable/search_embeddable.ts | 3 +- .../embeddable/search_embeddable_factory.ts | 2 +- .../public/discover/embeddable/types.ts | 2 +- .../embeddable/visualize_embeddable.ts | 2 +- .../public/vega_request_handler.ts | 3 +- src/legacy/ui/public/timefilter/index.ts | 16 +++- .../ui/public/timefilter/setup_router.ts | 89 +++++++++++++++++++ src/legacy/ui/public/vis/agg_configs.ts | 2 +- .../ui/public/vis/request_handlers/courier.js | 2 +- .../request_handlers/request_handlers.d.ts | 2 +- .../ui/public/visualize/loader/types.ts | 2 +- .../expression_types/kibana_context.ts | 2 +- .../navigation_menu/top_nav/top_nav.tsx | 3 +- .../public/components/embeddables/types.ts | 2 +- 30 files changed, 188 insertions(+), 133 deletions(-) rename src/legacy/{ui => core_plugins/data}/public/timefilter/get_time.test.ts (100%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/get_time.ts (100%) create mode 100644 src/legacy/core_plugins/data/public/timefilter/index.ts rename src/legacy/{ui => core_plugins/data}/public/timefilter/lib/diff_time_picker_vals.test.ts (100%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/lib/diff_time_picker_vals.ts (100%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/lib/parse_querystring.ts (100%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/time_history.ts (94%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/timefilter.test.mocks.ts (93%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/timefilter.test.ts (91%) rename src/legacy/{ui => core_plugins/data}/public/timefilter/timefilter.ts (69%) create mode 100644 src/legacy/ui/public/timefilter/setup_router.ts diff --git a/src/legacy/core_plugins/dashboard_embeddable_container/public/np_ready/public/lib/embeddable/dashboard_container.tsx b/src/legacy/core_plugins/dashboard_embeddable_container/public/np_ready/public/lib/embeddable/dashboard_container.tsx index 8dda752ba0b48..dc7aee514ab26 100644 --- a/src/legacy/core_plugins/dashboard_embeddable_container/public/np_ready/public/lib/embeddable/dashboard_container.tsx +++ b/src/legacy/core_plugins/dashboard_embeddable_container/public/np_ready/public/lib/embeddable/dashboard_container.tsx @@ -21,7 +21,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; import { Filter } from '@kbn/es-query'; -import { RefreshInterval, TimeRange } from '../../../../../../../../plugins/data/public'; +import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; import { Container, ContainerInput, diff --git a/src/legacy/core_plugins/data/public/search/search_bar/index.tsx b/src/legacy/core_plugins/data/public/search/search_bar/index.tsx index 8d98e4d2f9b7a..3fbfec364fad9 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/index.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/index.tsx @@ -18,7 +18,7 @@ */ import { Filter } from '@kbn/es-query'; -import { RefreshInterval, TimeRange } from 'ui/timefilter'; +import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; import { Query } from '../../query/query_bar'; export * from './components'; diff --git a/src/legacy/ui/public/timefilter/get_time.test.ts b/src/legacy/core_plugins/data/public/timefilter/get_time.test.ts similarity index 100% rename from src/legacy/ui/public/timefilter/get_time.test.ts rename to src/legacy/core_plugins/data/public/timefilter/get_time.test.ts diff --git a/src/legacy/ui/public/timefilter/get_time.ts b/src/legacy/core_plugins/data/public/timefilter/get_time.ts similarity index 100% rename from src/legacy/ui/public/timefilter/get_time.ts rename to src/legacy/core_plugins/data/public/timefilter/get_time.ts diff --git a/src/legacy/core_plugins/data/public/timefilter/index.ts b/src/legacy/core_plugins/data/public/timefilter/index.ts new file mode 100644 index 0000000000000..da57bca126b0f --- /dev/null +++ b/src/legacy/core_plugins/data/public/timefilter/index.ts @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export { Timefilter } from './timefilter'; +export { TimeHistory } from './time_history'; +export { getTime } from './get_time'; diff --git a/src/legacy/ui/public/timefilter/lib/diff_time_picker_vals.test.ts b/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.test.ts similarity index 100% rename from src/legacy/ui/public/timefilter/lib/diff_time_picker_vals.test.ts rename to src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.test.ts diff --git a/src/legacy/ui/public/timefilter/lib/diff_time_picker_vals.ts b/src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts similarity index 100% rename from src/legacy/ui/public/timefilter/lib/diff_time_picker_vals.ts rename to src/legacy/core_plugins/data/public/timefilter/lib/diff_time_picker_vals.ts diff --git a/src/legacy/ui/public/timefilter/lib/parse_querystring.ts b/src/legacy/core_plugins/data/public/timefilter/lib/parse_querystring.ts similarity index 100% rename from src/legacy/ui/public/timefilter/lib/parse_querystring.ts rename to src/legacy/core_plugins/data/public/timefilter/lib/parse_querystring.ts diff --git a/src/legacy/ui/public/timefilter/time_history.ts b/src/legacy/core_plugins/data/public/timefilter/time_history.ts similarity index 94% rename from src/legacy/ui/public/timefilter/time_history.ts rename to src/legacy/core_plugins/data/public/timefilter/time_history.ts index 66957ec5987c9..98e759fb27a9d 100644 --- a/src/legacy/ui/public/timefilter/time_history.ts +++ b/src/legacy/core_plugins/data/public/timefilter/time_history.ts @@ -19,7 +19,7 @@ import moment from 'moment'; import { TimeRange } from 'src/plugins/data/public'; -import { PersistedLog } from '../persisted_log'; +import { PersistedLog } from 'ui/persisted_log'; export class TimeHistory { private history: PersistedLog; @@ -52,5 +52,3 @@ export class TimeHistory { return this.history.get(); } } - -export const timeHistory = new TimeHistory(); diff --git a/src/legacy/ui/public/timefilter/timefilter.test.mocks.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.mocks.ts similarity index 93% rename from src/legacy/ui/public/timefilter/timefilter.test.mocks.ts rename to src/legacy/core_plugins/data/public/timefilter/timefilter.test.mocks.ts index f6d50167e45db..7354916c3fc35 100644 --- a/src/legacy/ui/public/timefilter/timefilter.test.mocks.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.mocks.ts @@ -17,7 +17,7 @@ * under the License. */ -import { chromeServiceMock } from '../../../../core/public/mocks'; +import { chromeServiceMock } from '../../../../../core/public/mocks'; jest.doMock('ui/new_platform', () => ({ npStart: { diff --git a/src/legacy/ui/public/timefilter/timefilter.test.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts similarity index 91% rename from src/legacy/ui/public/timefilter/timefilter.test.ts rename to src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts index f8885d842ef69..47896189aa080 100644 --- a/src/legacy/ui/public/timefilter/timefilter.test.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.test.ts @@ -19,46 +19,38 @@ import './timefilter.test.mocks'; -jest.mock( - 'ui/chrome', - () => ({ - getBasePath: () => `/some/base/path`, - getUiSettingsClient: () => { - return { - get: (key: string) => { - switch (key) { - case 'timepicker:timeDefaults': - return { from: 'now-15m', to: 'now' }; - case 'timepicker:refreshIntervalDefaults': - return { pause: false, value: 0 }; - default: - throw new Error(`Unexpected config key: ${key}`); - } - }, - }; - }, - }), - { virtual: true } -); - -jest.mock( - 'ui/timefilter/lib/parse_querystring', - () => ({ - parseQueryString: () => { - return { - // Can not access local variable from within a mock - // @ts-ignore - forceNow: global.nowTime, - }; - }, - }), - { virtual: true } -); +jest.mock('ui/chrome', () => ({ + getBasePath: () => `/some/base/path`, + getUiSettingsClient: () => { + return { + get: (key: string) => { + switch (key) { + case 'timepicker:timeDefaults': + return { from: 'now-15m', to: 'now' }; + case 'timepicker:refreshIntervalDefaults': + return { pause: false, value: 0 }; + default: + throw new Error(`Unexpected config key: ${key}`); + } + }, + }; + }, +})); + +jest.mock('./lib/parse_querystring', () => ({ + parseQueryString: () => { + return { + // Can not access local variable from within a mock + // @ts-ignore + forceNow: global.nowTime, + }; + }, +})); import sinon from 'sinon'; import expect from '@kbn/expect'; import moment from 'moment'; -import { timefilter } from './timefilter'; +import { timefilter } from 'ui/timefilter'; import { Subscription } from 'rxjs'; import { TimeRange, RefreshInterval } from 'src/plugins/data/public'; diff --git a/src/legacy/ui/public/timefilter/timefilter.ts b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts similarity index 69% rename from src/legacy/ui/public/timefilter/timefilter.ts rename to src/legacy/core_plugins/data/public/timefilter/timefilter.ts index 729575a833e37..b07060018f9d7 100644 --- a/src/legacy/ui/public/timefilter/timefilter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/timefilter.ts @@ -20,18 +20,18 @@ import _ from 'lodash'; import { Subject, BehaviorSubject } from 'rxjs'; import moment, { Moment } from 'moment'; -import { subscribeWithScope } from 'ui/utils/subscribe_with_scope'; -import chrome from 'ui/chrome'; -import { UiSettingsClientContract } from 'src/core/public'; import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; import { IndexPattern } from 'src/legacy/core_plugins/data/public'; -import { IScope } from 'angular'; -import { timeHistory } from './time_history'; +import { TimeHistory } from './time_history'; import { areRefreshIntervalsDifferent, areTimeRangesDifferent } from './lib/diff_time_picker_vals'; -import uiRoutes from '../routes'; import { parseQueryString } from './lib/parse_querystring'; import { calculateBounds, getTime } from './get_time'; +export interface TimefilterConfig { + timeDefaults: TimeRange; + refreshIntervalDefaults: RefreshInterval; +} + // Timefilter accepts moment input but always returns string output export type InputTimeRange = | TimeRange @@ -53,13 +53,15 @@ export class Timefilter { private _time: TimeRange; private _refreshInterval!: RefreshInterval; + private _history: TimeHistory; public isTimeRangeSelectorEnabled: boolean = false; public isAutoRefreshSelectorEnabled: boolean = false; - constructor(uiSettings: UiSettingsClientContract) { - this._time = uiSettings.get('timepicker:timeDefaults'); - this.setRefreshInterval(uiSettings.get('timepicker:refreshIntervalDefaults')); + constructor(config: TimefilterConfig, timeHistory: TimeHistory) { + this._history = timeHistory; + this._time = config.timeDefaults; + this.setRefreshInterval(config.refreshIntervalDefaults); } getEnabledUpdated$ = () => { @@ -106,7 +108,7 @@ export class Timefilter { from: newTime.from, to: newTime.to, }; - timeHistory.add(this._time); + this._history.add(this._time); this.timeUpdate$.next(); this.fetch$.next(); } @@ -221,64 +223,3 @@ export class Timefilter { this.autoRefreshFetch$.next(); }; } - -export const timefilter = new Timefilter(chrome.getUiSettingsClient()); - -// TODO -// remove everything underneath once globalState is no longer an angular service -// and listener can be registered without angular. -function convertISO8601(stringTime: string): string { - const obj = moment(stringTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ', true); - return obj.isValid() ? obj.toString() : stringTime; -} - -// Currently some parts of Kibana (index patterns, timefilter) rely on addSetupWork in the uiRouter -// and require it to be executed to properly function. -// This function is exposed for applications that do not use uiRoutes like APM -// Kibana issue https://github.com/elastic/kibana/issues/19110 tracks the removal of this dependency on uiRouter -export const registerTimefilterWithGlobalState = _.once((globalState: any, $rootScope: IScope) => { - const uiSettings = chrome.getUiSettingsClient(); - const timeDefaults = uiSettings.get('timepicker:timeDefaults'); - const refreshIntervalDefaults = uiSettings.get('timepicker:refreshIntervalDefaults'); - - timefilter.setTime(_.defaults(globalState.time || {}, timeDefaults)); - timefilter.setRefreshInterval( - _.defaults(globalState.refreshInterval || {}, refreshIntervalDefaults) - ); - - globalState.on('fetch_with_changes', () => { - // clone and default to {} in one - const newTime: TimeRange = _.defaults({}, globalState.time, timeDefaults); - const newRefreshInterval: RefreshInterval = _.defaults( - {}, - globalState.refreshInterval, - refreshIntervalDefaults - ); - - if (newTime) { - if (newTime.to) newTime.to = convertISO8601(newTime.to); - if (newTime.from) newTime.from = convertISO8601(newTime.from); - } - - timefilter.setTime(newTime); - timefilter.setRefreshInterval(newRefreshInterval); - }); - - const updateGlobalStateWithTime = () => { - globalState.time = timefilter.getTime(); - globalState.refreshInterval = timefilter.getRefreshInterval(); - globalState.save(); - }; - - subscribeWithScope($rootScope, timefilter.getRefreshIntervalUpdate$(), { - next: updateGlobalStateWithTime, - }); - - subscribeWithScope($rootScope, timefilter.getTimeUpdate$(), { - next: updateGlobalStateWithTime, - }); -}); - -uiRoutes.addSetupWork((globalState, $rootScope) => { - return registerTimefilterWithGlobalState(globalState, $rootScope); -}); diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx index a0fe853484eee..1f65ccebb67d9 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx @@ -36,7 +36,7 @@ import { import { KbnUrl } from 'ui/url/kbn_url'; import { Filter } from '@kbn/es-query'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { IndexPattern } from 'ui/index_patterns'; import { IPrivate } from 'ui/private'; import { StaticIndexPattern, Query, SavedQuery } from 'plugins/data'; diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts index 40e65f5683ee2..be76f498c8911 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state.test.ts @@ -23,7 +23,8 @@ import { DashboardStateManager } from './dashboard_state_manager'; import { getAppStateMock, getSavedDashboardMock } from './__tests__'; import { AppStateClass } from 'ui/state_management/app_state'; import { DashboardAppState } from './types'; -import { Timefilter, TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; +import { Timefilter } from 'ui/timefilter'; import { ViewMode } from '../../../embeddable_api/public/np_ready/public'; describe('DashboardState', function() { diff --git a/src/legacy/core_plugins/kibana/public/dashboard/lib/update_saved_dashboard.ts b/src/legacy/core_plugins/kibana/public/dashboard/lib/update_saved_dashboard.ts index 93f487ba6d502..707b5a0f5f5f5 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/lib/update_saved_dashboard.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/lib/update_saved_dashboard.ts @@ -19,7 +19,8 @@ import _ from 'lodash'; import { AppState } from 'ui/state_management/app_state'; -import { Timefilter, RefreshInterval } from 'ui/timefilter'; +import { Timefilter } from 'ui/timefilter'; +import { RefreshInterval } from 'src/plugins/data/public'; import { FilterUtils } from './filter_utils'; import { SavedObjectDashboard } from '../saved_dashboard/saved_dashboard'; diff --git a/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts b/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts index 68fd8f0a5a976..1231ca28ed014 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts @@ -20,7 +20,7 @@ import { SearchSource } from 'ui/courier'; import { SavedObject } from 'ui/saved_objects/saved_object'; import moment from 'moment'; -import { RefreshInterval } from 'ui/timefilter'; +import { RefreshInterval } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; diff --git a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts index 92fad1713177a..c7a17146e6d18 100644 --- a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts +++ b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts @@ -35,7 +35,8 @@ import { Filter, FilterStateStore } from '@kbn/es-query'; import chrome from 'ui/chrome'; import { i18n } from '@kbn/i18n'; import { toastNotifications } from 'ui/notify'; -import { timefilter, getTime, TimeRange } from 'ui/timefilter'; +import { timefilter, getTime } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query, onlyDisabledFiltersChanged } from '../../../../data/public'; import { APPLY_FILTER_TRIGGER, diff --git a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable_factory.ts b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable_factory.ts index 88dafaacf7357..982e56731bacb 100644 --- a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable_factory.ts +++ b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable_factory.ts @@ -22,7 +22,7 @@ import { capabilities } from 'ui/capabilities'; import { i18n } from '@kbn/i18n'; import chrome from 'ui/chrome'; import { IPrivate } from 'ui/private'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter'; import { EmbeddableFactory, diff --git a/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts b/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts index 981580079ae18..a0e15e99b742d 100644 --- a/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts +++ b/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts @@ -18,7 +18,7 @@ */ import { StaticIndexPattern } from 'ui/index_patterns'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { SavedSearch } from '../types'; diff --git a/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts b/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts index d750010a132d6..6aa0a08d88cbb 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts @@ -29,7 +29,7 @@ import { } from 'ui/visualize/loader/types'; import { Subscription } from 'rxjs'; import * as Rx from 'rxjs'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Filter } from '@kbn/es-query'; import { EmbeddableInput, diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts index a930e369c3e1c..22a71bd999d54 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts @@ -17,7 +17,8 @@ * under the License. */ import { Filter } from '@kbn/es-query'; -import { timefilter, TimeRange } from 'ui/timefilter'; +import { timefilter } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; // @ts-ignore diff --git a/src/legacy/ui/public/timefilter/index.ts b/src/legacy/ui/public/timefilter/index.ts index 5f5fcf6b19c7f..34f2a367a217c 100644 --- a/src/legacy/ui/public/timefilter/index.ts +++ b/src/legacy/ui/public/timefilter/index.ts @@ -17,8 +17,16 @@ * under the License. */ -export { TimeRange, RefreshInterval } from '../../../../plugins/data/public'; +import uiRoutes from 'ui/routes'; +import { registerTimefilterWithGlobalState, getTimefilterConfig } from './setup_router'; +import { Timefilter, TimeHistory } from '../../../core_plugins/data/public/timefilter'; -export { timefilter, Timefilter, registerTimefilterWithGlobalState } from './timefilter'; -export { timeHistory, TimeHistory } from './time_history'; -export { getTime } from './get_time'; +const config = getTimefilterConfig(); + +export { Timefilter, TimeHistory, getTime } from '../../../core_plugins/data/public/timefilter'; +export const timeHistory = new TimeHistory(); +export const timefilter = new Timefilter(config, timeHistory); + +uiRoutes.addSetupWork((globalState, $rootScope) => { + return registerTimefilterWithGlobalState(timefilter, globalState, $rootScope); +}); diff --git a/src/legacy/ui/public/timefilter/setup_router.ts b/src/legacy/ui/public/timefilter/setup_router.ts new file mode 100644 index 0000000000000..cbd03df455da3 --- /dev/null +++ b/src/legacy/ui/public/timefilter/setup_router.ts @@ -0,0 +1,89 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import _ from 'lodash'; +import { IScope } from 'angular'; +import moment from 'moment'; +import { subscribeWithScope } from 'ui/utils/subscribe_with_scope'; +import chrome from 'ui/chrome'; +import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; +import { Timefilter } from '../../../core_plugins/data/public/timefilter'; + +// TODO +// remove everything underneath once globalState is no longer an angular service +// and listener can be registered without angular. +function convertISO8601(stringTime: string): string { + const obj = moment(stringTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ', true); + return obj.isValid() ? obj.toString() : stringTime; +} + +export function getTimefilterConfig() { + const settings = chrome.getUiSettingsClient(); + return { + timeDefaults: settings.get('timepicker:timeDefaults'), + refreshIntervalDefaults: settings.get('timepicker:refreshIntervalDefaults'), + }; +} + +// Currently some parts of Kibana (index patterns, timefilter) rely on addSetupWork in the uiRouter +// and require it to be executed to properly function. +// This function is exposed for applications that do not use uiRoutes like APM +// Kibana issue https://github.com/elastic/kibana/issues/19110 tracks the removal of this dependency on uiRouter +export const registerTimefilterWithGlobalState = _.once( + (timefilter: Timefilter, globalState: any, $rootScope: IScope) => { + // settings have to be re-fetched here, to make sure that settings changed by overrideLocalDefault are taken into account. + const config = getTimefilterConfig(); + timefilter.setTime(_.defaults(globalState.time || {}, config.timeDefaults)); + timefilter.setRefreshInterval( + _.defaults(globalState.refreshInterval || {}, config.refreshIntervalDefaults) + ); + + globalState.on('fetch_with_changes', () => { + // clone and default to {} in one + const newTime: TimeRange = _.defaults({}, globalState.time, config.timeDefaults); + const newRefreshInterval: RefreshInterval = _.defaults( + {}, + globalState.refreshInterval, + config.refreshIntervalDefaults + ); + + if (newTime) { + if (newTime.to) newTime.to = convertISO8601(newTime.to); + if (newTime.from) newTime.from = convertISO8601(newTime.from); + } + + timefilter.setTime(newTime); + timefilter.setRefreshInterval(newRefreshInterval); + }); + + const updateGlobalStateWithTime = () => { + globalState.time = timefilter.getTime(); + globalState.refreshInterval = timefilter.getRefreshInterval(); + globalState.save(); + }; + + subscribeWithScope($rootScope, timefilter.getRefreshIntervalUpdate$(), { + next: updateGlobalStateWithTime, + }); + + subscribeWithScope($rootScope, timefilter.getTimeUpdate$(), { + next: updateGlobalStateWithTime, + }); + } +); diff --git a/src/legacy/ui/public/vis/agg_configs.ts b/src/legacy/ui/public/vis/agg_configs.ts index e3edbef40c654..e16784bb6e2f2 100644 --- a/src/legacy/ui/public/vis/agg_configs.ts +++ b/src/legacy/ui/public/vis/agg_configs.ts @@ -27,8 +27,8 @@ */ import _ from 'lodash'; +import { TimeRange } from 'src/plugins/data/public'; import { Schemas } from '../visualize/loader/pipeline_helpers/build_pipeline'; -import { TimeRange } from '../timefilter'; import { Schema } from '../vis/editors/default/schemas'; import { AggConfig, AggConfigOptions } from './agg_config'; import { AggGroupNames } from './editors/default/agg_groups'; diff --git a/src/legacy/ui/public/vis/request_handlers/courier.js b/src/legacy/ui/public/vis/request_handlers/courier.js index cdd5158c5701d..b6f05f6228ea3 100644 --- a/src/legacy/ui/public/vis/request_handlers/courier.js +++ b/src/legacy/ui/public/vis/request_handlers/courier.js @@ -24,7 +24,7 @@ import { calculateObjectHash } from '../lib/calculate_object_hash'; import { getRequestInspectorStats, getResponseInspectorStats } from '../../courier/utils/courier_inspector_utils'; import { tabifyAggResponse } from '../../agg_response/tabify/tabify'; import { buildTabularInspectorData } from '../../inspector/build_tabular_inspector_data'; -import { getTime } from '../../timefilter/get_time'; +import { getTime } from '../../timefilter'; const CourierRequestHandlerProvider = function () { diff --git a/src/legacy/ui/public/vis/request_handlers/request_handlers.d.ts b/src/legacy/ui/public/vis/request_handlers/request_handlers.d.ts index 213f1e6252a6d..03751e189210a 100644 --- a/src/legacy/ui/public/vis/request_handlers/request_handlers.d.ts +++ b/src/legacy/ui/public/vis/request_handlers/request_handlers.d.ts @@ -17,7 +17,7 @@ * under the License. */ -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { AggConfigs } from 'ui/vis/agg_configs'; diff --git a/src/legacy/ui/public/visualize/loader/types.ts b/src/legacy/ui/public/visualize/loader/types.ts index 595a0649ec5ef..bb1113d212261 100644 --- a/src/legacy/ui/public/visualize/loader/types.ts +++ b/src/legacy/ui/public/visualize/loader/types.ts @@ -18,7 +18,7 @@ */ import { Filter } from '@kbn/es-query'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { SavedObject } from 'ui/saved_objects/saved_object'; diff --git a/src/plugins/data/common/expressions/expression_types/kibana_context.ts b/src/plugins/data/common/expressions/expression_types/kibana_context.ts index 5ee3f7abbbdb0..0a3e8a4db87f6 100644 --- a/src/plugins/data/common/expressions/expression_types/kibana_context.ts +++ b/src/plugins/data/common/expressions/expression_types/kibana_context.ts @@ -18,8 +18,8 @@ */ import { Filter } from '@kbn/es-query'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from '../../query/types'; -import { TimeRange } from '../../timefilter/types'; const name = 'kibana_context'; diff --git a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx index 6e38b37c33a24..0596df80fa449 100644 --- a/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx +++ b/x-pack/legacy/plugins/ml/public/components/navigation_menu/top_nav/top_nav.tsx @@ -7,7 +7,8 @@ import React, { FC, Fragment, useState, useEffect } from 'react'; import { Subscription } from 'rxjs'; import { EuiSuperDatePicker } from '@elastic/eui'; -import { TimeHistory, TimeRange } from 'ui/timefilter'; +import { TimeHistory } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { mlTimefilterRefresh$ } from '../../../services/timefilter_refresh_service'; import { useUiContext } from '../../../contexts/ui/use_ui_context'; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts index 62f99688f9f8b..2701250c609fa 100644 --- a/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts @@ -5,7 +5,7 @@ */ import { Filter as ESFilterType } from '@kbn/es-query'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { EmbeddableInput } from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; export interface MapEmbeddableInput extends EmbeddableInput {