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 and timehistory instantiation ⇒ data plugin #44783

Merged
merged 24 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ import { getFiltersArray } from './test_helpers/get_filters_array';
import { coreMock } from '../../../../../../core/public/mocks';
const setupMock = coreMock.createSetup();

import { timefilterServiceMock } from '../../timefilter/timefilter_service.mock';
const timefilterSetupMock = timefilterServiceMock.createSetupContract();

setupMock.uiSettings.get.mockImplementation((key: string) => {
return true;
});

jest.mock('ui/timefilter', () => {
return {
timefilter: {
setTime: jest.fn(),
},
};
});

describe('filter_manager', () => {
let appStateStub: StubState;
let globalStateStub: StubState;
Expand All @@ -66,7 +61,8 @@ describe('filter_manager', () => {
indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(
(indexPatterns as unknown) as IndexPatterns,
setupMock.uiSettings
setupMock.uiSettings,
timefilterSetupMock.timefilter
);
readyFilters = getFiltersArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,30 @@ import { compareFilters } from './lib/compare_filters';
import { mapAndFlattenFilters } from './lib/map_and_flatten_filters';
// @ts-ignore
import { uniqFilters } from './lib/uniq_filters';
// @ts-ignore

import { extractTimeFilter } from './lib/extract_time_filter';
// @ts-ignore
import { changeTimeFilter } from './lib/change_time_filter';

import { onlyDisabledFiltersChanged } from './lib/only_disabled';

import { PartitionedFilters } from './partitioned_filters';

import { IndexPatterns } from '../../index_patterns';
import { TimefilterContract } from '../../timefilter';

export class FilterManager {
private indexPatterns: IndexPatterns;
private filters: Filter[] = [];
private updated$: Subject<void> = new Subject();
private fetch$: Subject<void> = new Subject();
private uiSettings: UiSettingsClientContract;
private timefilter: TimefilterContract;

constructor(indexPatterns: IndexPatterns, uiSettings: UiSettingsClientContract) {
constructor(
indexPatterns: IndexPatterns,
uiSettings: UiSettingsClientContract,
timefilter: TimefilterContract
) {
this.indexPatterns = indexPatterns;
this.uiSettings = uiSettings;
this.timefilter = timefilter;
}

private mergeIncomingFilters(partitionedFilters: PartitionedFilters): Filter[] {
Expand Down Expand Up @@ -191,7 +194,7 @@ export class FilterManager {

public async addFiltersAndChangeTimeFilter(filters: Filter[]) {
const timeFilter = await extractTimeFilter(this.indexPatterns, filters);
if (timeFilter) changeTimeFilter(timeFilter);
if (timeFilter) changeTimeFilter(this.timefilter, timeFilter);
return this.addFilters(filters.filter(filter => filter !== timeFilter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ import { StubIndexPatterns } from './test_helpers/stub_index_pattern';
import { coreMock } from '../../../../../../core/public/mocks';
const setupMock = coreMock.createSetup();

import { timefilterServiceMock } from '../../timefilter/timefilter_service.mock';
const timefilterSetupMock = timefilterServiceMock.createSetupContract();

setupMock.uiSettings.get.mockImplementation((key: string) => {
return true;
});

jest.mock('ui/timefilter', () => {
return {
timefilter: {
setTime: jest.fn(),
},
};
});

describe('filter_state_manager', () => {
let appStateStub: StubState;
let globalStateStub: StubState;
Expand All @@ -55,7 +50,8 @@ describe('filter_state_manager', () => {
const indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(
(indexPatterns as unknown) as IndexPatterns,
setupMock.uiSettings
setupMock.uiSettings,
timefilterSetupMock.timefilter
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,42 @@
* specific language governing permissions and limitations
* under the License.
*/

import './change_time_filter.test.mocks';

jest.mock('ui/chrome',
() => ({
getBasePath: () => `/some/base/path`,
getUiSettingsClient: () => {
return {
get: (key) => {
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 });

import expect from '@kbn/expect';
import { changeTimeFilter } from '../change_time_filter';
import { timefilter } from 'ui/timefilter';
import { TimeRange } from 'src/plugins/data/public';

import { timefilterServiceMock } from '../../../../timefilter/timefilter_service.mock';
const timefilterMock = timefilterServiceMock.createSetupContract();
const timefilter = timefilterMock.timefilter;

describe('changeTimeFilter()', function () {
let _time: TimeRange | undefined;
timefilter.setTime.mockImplementation((time: any) => {
_time = {
from: time.from.toISOString(),
to: time.to.toISOString(),
};
});
timefilter.getTime.mockImplementation(() => {
return _time!;
});

describe('changeTimeFilter()', function() {
const gt = 1388559600000;
const lt = 1388646000000;

test('should change the timefilter to match the range gt/lt', function () {
test('should change the timefilter to match the range gt/lt', function() {
const filter = { range: { '@timestamp': { gt, lt } } };
changeTimeFilter(filter);
changeTimeFilter(timefilter, filter);
const { to, from } = timefilter.getTime();
expect(to).to.be(new Date(lt).toISOString());
expect(from).to.be(new Date(gt).toISOString());
});

test('should change the timefilter to match the range gte/lte', function () {
test('should change the timefilter to match the range gte/lte', function() {
const filter = { range: { '@timestamp': { gte: gt, lte: lt } } };
changeTimeFilter(filter);
changeTimeFilter(timefilter, filter);
const { to, from } = timefilter.getTime();
expect(to).to.be(new Date(lt).toISOString());
expect(from).to.be(new Date(gt).toISOString());
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import moment from 'moment';
import _ from 'lodash';
import { timefilter } from 'ui/timefilter';
import { TimefilterContract } from '../../../timefilter';

export function changeTimeFilter(filter) {
export function changeTimeFilter(timefilter: TimefilterContract, filter: any) {
const key = _.keys(filter.range)[0];
const values = filter.range[key];
timefilter.setTime({
from: moment(values.gt || values.gte),
to: moment(values.lt || values.lte),
mode: 'absolute',
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
*/

import _ from 'lodash';
import { IndexPatterns } from '../../../index_patterns';

export async function extractTimeFilter(indexPatterns, filters) {
export async function extractTimeFilter(indexPatterns: IndexPatterns, filters: any) {
lizozom marked this conversation as resolved.
Show resolved Hide resolved
// Assume all the index patterns are the same since they will be added
// from the same visualization.
const id = _.get(filters, '[0].meta.index');
const id: string = _.get(filters, '[0].meta.index');
if (id == null) return;

const indexPattern = await indexPatterns.get(id);

const filter = _.find(filters, function (obj) {
const filter = _.find(filters, function(obj: any) {
const key = _.keys(obj.range)[0];
return key === indexPattern.timeFieldName;
});
Expand Down
6 changes: 4 additions & 2 deletions src/legacy/core_plugins/data/public/filter/filter_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { UiSettingsClientContract } from 'src/core/public';
import { IndexPatterns } from '../index_patterns';
import { FilterManager } from './filter_manager';
import { TimefilterContract } from '../timefilter';

/**
* Filter Service
Expand All @@ -29,12 +30,13 @@ import { FilterManager } from './filter_manager';
export interface FilterServiceDependencies {
indexPatterns: IndexPatterns;
uiSettings: UiSettingsClientContract;
timefilter: TimefilterContract;
}

export class FilterService {
public setup({ indexPatterns, uiSettings }: FilterServiceDependencies) {
public setup({ indexPatterns, uiSettings, timefilter }: FilterServiceDependencies) {
return {
filterManager: new FilterManager(indexPatterns, uiSettings),
filterManager: new FilterManager(indexPatterns, uiSettings, timefilter),
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export {
mockFields,
mockIndexPattern,
} from './index_patterns';

export { TimeHistoryContract, TimefilterContract, getTime, InputTimeRange } from './timefilter';
7 changes: 3 additions & 4 deletions src/legacy/core_plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
* under the License.
*/

import { DataSetup } from '.';
import { filterServiceMock } from './filter/filter_service.mock';
import { indexPatternsServiceMock } from './index_patterns/index_patterns_service.mock';
import { queryServiceMock } from './query/query_service.mock';
import { timefilterServiceMock } from './timefilter/timefilter_service.mock';

function createDataSetupMock() {
const mock: MockedKeys<Partial<DataSetup>> = {
return {
filter: filterServiceMock.createSetupContract(),
indexPatterns: indexPatternsServiceMock.createSetupContract(),
query: queryServiceMock.createSetupContract(),
timefilter: timefilterServiceMock.createSetupContract(),
};

return mock;
}

function createDataStartMock() {
Expand Down
9 changes: 9 additions & 0 deletions src/legacy/core_plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CoreSetup, CoreStart, Plugin } from '../../../../core/public';
import { SearchService, SearchSetup } from './search';
import { QueryService, QuerySetup } from './query';
import { FilterService, FilterSetup } from './filter';
import { TimefilterService, TimefilterSetup } from './timefilter';
import { IndexPatternsService, IndexPatternsSetup } from './index_patterns';
import { LegacyDependenciesPluginSetup } from './shim/legacy_dependencies_plugin';

Expand All @@ -43,6 +44,7 @@ export interface DataSetup {
filter: FilterSetup;
query: QuerySetup;
search: SearchSetup;
timefilter: TimefilterSetup;
}

/**
Expand All @@ -62,6 +64,7 @@ export class DataPlugin implements Plugin<DataSetup, {}, DataPluginSetupDependen
private readonly indexPatterns: IndexPatternsService = new IndexPatternsService();
private readonly query: QueryService = new QueryService();
private readonly search: SearchService = new SearchService();
private readonly timefilter: TimefilterService = new TimefilterService();

private setupApi!: DataSetup;

Expand All @@ -75,14 +78,19 @@ export class DataPlugin implements Plugin<DataSetup, {}, DataPluginSetupDependen
http,
});

const timefilterService = this.timefilter.setup({
uiSettings,
});
this.setupApi = {
indexPatterns: indexPatternsService,
filter: this.filter.setup({
uiSettings,
indexPatterns: indexPatternsService.indexPatterns,
timefilter: timefilterService.timefilter,
}),
query: this.query.setup(),
search: this.search.setup(savedObjectsClient),
timefilter: timefilterService,
};

return this.setupApi;
Expand All @@ -99,5 +107,6 @@ export class DataPlugin implements Plugin<DataSetup, {}, DataPluginSetupDependen
this.filter.stop();
this.query.stop();
this.search.stop();
this.timefilter.stop();
}
}
Loading