-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] Support storing time with saved searches (#138377)
* [Discover] Implement UI for storing time with a saved search * [Discover] Save time range data with a saved search * [Discover] Improve updating of values * [Discover] Restore time range after loading a saved search * [Discover] Add time range validation * [Discover] Add refresh interval validation * [Discover] Update how saved search gets restored * [Discover] Improve tests * [Discover] Update tests * [Discover] Improve type imports * [Discover] Update copy * [Discover] Fix types after the merge * [Discover] Update test name * [Discover] Fix types * [Discover] Update mapping * [Discover] Update mapping * Explicitly set field limit for .kibana_ esArchives Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Rudolf Meijering <[email protected]>
- Loading branch information
1 parent
ac0688b
commit 1a70f6f
Showing
22 changed files
with
351 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/plugins/discover/public/services/saved_searches/restore_from_saved_search.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { TimefilterContract } from '@kbn/data-plugin/public'; | ||
import type { TimeRange, RefreshInterval } from '@kbn/data-plugin/common'; | ||
import type { SavedSearch } from '@kbn/saved-search-plugin/public'; | ||
import { restoreStateFromSavedSearch } from './restore_from_saved_search'; | ||
|
||
describe('discover restore state from saved search', () => { | ||
let timefilterMock: TimefilterContract; | ||
const timeRange: TimeRange = { | ||
from: 'now-30m', | ||
to: 'now', | ||
}; | ||
const refreshInterval: RefreshInterval = { | ||
value: 5000, | ||
pause: false, | ||
}; | ||
|
||
beforeEach(() => { | ||
timefilterMock = { | ||
setTime: jest.fn(), | ||
setRefreshInterval: jest.fn(), | ||
} as unknown as TimefilterContract; | ||
}); | ||
|
||
test('should not update timefilter if attributes are not set', async () => { | ||
restoreStateFromSavedSearch({ | ||
savedSearch: {} as SavedSearch, | ||
timefilter: timefilterMock, | ||
}); | ||
|
||
expect(timefilterMock.setTime).not.toHaveBeenCalled(); | ||
expect(timefilterMock.setRefreshInterval).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should not update timefilter if timeRestore is disabled', async () => { | ||
restoreStateFromSavedSearch({ | ||
savedSearch: { | ||
timeRestore: false, | ||
timeRange, | ||
refreshInterval, | ||
} as SavedSearch, | ||
timefilter: timefilterMock, | ||
}); | ||
|
||
expect(timefilterMock.setTime).not.toHaveBeenCalled(); | ||
expect(timefilterMock.setRefreshInterval).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should update timefilter if timeRestore is enabled', async () => { | ||
restoreStateFromSavedSearch({ | ||
savedSearch: { | ||
timeRestore: true, | ||
timeRange, | ||
refreshInterval, | ||
} as SavedSearch, | ||
timefilter: timefilterMock, | ||
}); | ||
|
||
expect(timefilterMock.setTime).toHaveBeenCalledWith(timeRange); | ||
expect(timefilterMock.setRefreshInterval).toHaveBeenCalledWith(refreshInterval); | ||
}); | ||
|
||
test('should not update timefilter if attributes are missing', async () => { | ||
restoreStateFromSavedSearch({ | ||
savedSearch: { | ||
timeRestore: true, | ||
} as SavedSearch, | ||
timefilter: timefilterMock, | ||
}); | ||
|
||
expect(timefilterMock.setTime).not.toHaveBeenCalled(); | ||
expect(timefilterMock.setRefreshInterval).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should not update timefilter if attributes are invalid', async () => { | ||
restoreStateFromSavedSearch({ | ||
savedSearch: { | ||
timeRestore: true, | ||
timeRange: { | ||
from: 'test', | ||
to: 'now', | ||
}, | ||
refreshInterval: { | ||
pause: false, | ||
value: -500, | ||
}, | ||
} as SavedSearch, | ||
timefilter: timefilterMock, | ||
}); | ||
|
||
expect(timefilterMock.setTime).not.toHaveBeenCalled(); | ||
expect(timefilterMock.setRefreshInterval).not.toHaveBeenCalled(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
src/plugins/discover/public/services/saved_searches/restore_from_saved_search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { TimefilterContract } from '@kbn/data-plugin/public'; | ||
import type { SavedSearch } from '@kbn/saved-search-plugin/public'; | ||
import { isRefreshIntervalValid, isTimeRangeValid } from '../../utils/validate_time'; | ||
|
||
export const restoreStateFromSavedSearch = ({ | ||
savedSearch, | ||
timefilter, | ||
}: { | ||
savedSearch: SavedSearch; | ||
timefilter: TimefilterContract; | ||
}) => { | ||
if (!savedSearch) { | ||
return; | ||
} | ||
|
||
if (savedSearch.timeRestore && savedSearch.timeRange && isTimeRangeValid(savedSearch.timeRange)) { | ||
timefilter.setTime(savedSearch.timeRange); | ||
} | ||
if ( | ||
savedSearch.timeRestore && | ||
savedSearch.refreshInterval && | ||
isRefreshIntervalValid(savedSearch.refreshInterval) | ||
) { | ||
timefilter.setRefreshInterval(savedSearch.refreshInterval); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { RefreshInterval, TimeRange } from '@kbn/data-plugin/common'; | ||
import { isTimeRangeValid, isRefreshIntervalValid } from './validate_time'; | ||
|
||
describe('discover validate time', () => { | ||
test('should validate time ranges correctly', async () => { | ||
expect(isTimeRangeValid({ from: '2020-06-02T13:36:13.689Z', to: 'now' })).toEqual(true); | ||
expect(isTimeRangeValid({ from: 'now', to: 'now+1h' })).toEqual(true); | ||
expect(isTimeRangeValid({ from: '', to: '' })).toEqual(false); | ||
expect(isTimeRangeValid({} as unknown as TimeRange)).toEqual(false); | ||
expect(isTimeRangeValid(undefined)).toEqual(false); | ||
}); | ||
|
||
test('should validate that refresh interval is valid', async () => { | ||
expect(isRefreshIntervalValid({ value: 5000, pause: false })).toEqual(true); | ||
expect(isRefreshIntervalValid({ value: 0, pause: false })).toEqual(true); | ||
expect(isRefreshIntervalValid({ value: 4000, pause: true })).toEqual(true); | ||
}); | ||
|
||
test('should validate that refresh interval is invalid', async () => { | ||
expect(isRefreshIntervalValid({ value: -5000, pause: false })).toEqual(false); | ||
expect( | ||
isRefreshIntervalValid({ value: 'test', pause: false } as unknown as RefreshInterval) | ||
).toEqual(false); | ||
expect( | ||
isRefreshIntervalValid({ value: 4000, pause: 'test' } as unknown as RefreshInterval) | ||
).toEqual(false); | ||
expect(isRefreshIntervalValid({} as unknown as RefreshInterval)).toEqual(false); | ||
expect(isRefreshIntervalValid(undefined)).toEqual(false); | ||
}); | ||
}); |
Oops, something went wrong.