Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move timefilter code ⇒ data plugin #44607

Merged
merged 12 commits into from
Sep 4, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
22 changes: 22 additions & 0 deletions src/legacy/core_plugins/data/public/timefilter/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,5 +52,3 @@ export class TimeHistory {
return this.history.get();
}
}

export const timeHistory = new TimeHistory();
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
},
};
},
}));
lizozom marked this conversation as resolved.
Show resolved Hide resolved

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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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$ = () => {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/legacy/ui/public/timefilter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
lizozom marked this conversation as resolved.
Show resolved Hide resolved

uiRoutes.addSetupWork((globalState, $rootScope) => {
return registerTimefilterWithGlobalState(timefilter, globalState, $rootScope);
});
Loading