-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Search] Search Sessions with relative time range #84405
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
Changes from 4 commits
05b927f
5bdfb7a
8526dba
29c330d
6d9f18b
543203e
d001f1b
8da16d0
2a7f482
10deea5
ed491c5
3014cbf
5f2a1b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * 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 { | ||
| NowProvider, | ||
| NowProviderInternalContract, | ||
| NowProviderPublicContract, | ||
| } from './now_provider'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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 { getForceNowFromUrl } from './get_force_now_from_url'; | ||
| const originalLocation = window.location; | ||
| afterAll(() => { | ||
| window.location = originalLocation; | ||
| }); | ||
|
|
||
| function mockLocation(url: string) { | ||
| // @ts-ignore | ||
| delete window.location; | ||
| // @ts-ignore | ||
| window.location = new URL(url); | ||
| } | ||
|
|
||
| test('should get force now from URL', () => { | ||
| const dateString = '1999-01-01T00:00:00.000Z'; | ||
| mockLocation(`https://elastic.co/?forceNow=${dateString}`); | ||
|
|
||
| expect(getForceNowFromUrl()).toEqual(new Date(dateString)); | ||
| }); | ||
|
|
||
| test('should throw if force now is invalid', () => { | ||
| const dateString = 'invalid-date'; | ||
| mockLocation(`https://elastic.co/?forceNow=${dateString}`); | ||
|
|
||
| expect(() => getForceNowFromUrl()).toThrowError(); | ||
| }); | ||
|
|
||
| test('should return undefined if no forceNow', () => { | ||
| mockLocation(`https://elastic.co/`); | ||
| expect(getForceNowFromUrl()).toBe(undefined); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,10 +16,25 @@ | |
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { parse } from 'query-string'; | ||
|
|
||
| /** @internal */ | ||
| export function parseQueryString() { | ||
| export function getForceNowFromUrl(): Date | undefined { | ||
|
||
| const forceNow = parseQueryString().forceNow as string; | ||
| if (!forceNow) { | ||
| return; | ||
| } | ||
|
|
||
| const ts = Date.parse(forceNow); | ||
| if (isNaN(ts)) { | ||
| throw new Error(`forceNow query parameter, ${forceNow}, can't be parsed by Date.parse`); | ||
| } | ||
| return new Date(ts); | ||
| } | ||
|
|
||
| /** @internal */ | ||
| function parseQueryString() { | ||
lizozom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // window.location.search is an empty string | ||
| // get search from href | ||
| const hrefSplit = window.location.href.split('?'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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 { getForceNowFromUrl } from './get_force_now_from_url'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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 { NowProvider, NowProviderInternalContract } from './now_provider'; | ||
|
|
||
| let mockDateFromUrl: undefined | Date; | ||
| let nowProvider: NowProviderInternalContract; | ||
|
|
||
| jest.mock('./lib', () => ({ | ||
| // @ts-ignore | ||
| ...jest.requireActual('./lib'), | ||
| getForceNowFromUrl: () => mockDateFromUrl, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| nowProvider = new NowProvider(); | ||
| }); | ||
| afterEach(() => { | ||
| mockDateFromUrl = undefined; | ||
| }); | ||
|
|
||
| test('should return Date.now() by default', async () => { | ||
| const now = Date.now(); | ||
| await new Promise((r) => setTimeout(r, 10)); | ||
| expect(nowProvider.get().getTime()).toBeGreaterThan(now); | ||
| }); | ||
|
|
||
| test('should forceNow from URL', async () => { | ||
| mockDateFromUrl = new Date('1999-01-01T00:00:00.000Z'); | ||
| nowProvider = new NowProvider(); | ||
| expect(nowProvider.get()).toEqual(mockDateFromUrl); | ||
| }); | ||
|
|
||
| test('should forceNow from URL if custom now is set', async () => { | ||
| mockDateFromUrl = new Date('1999-01-01T00:00:00.000Z'); | ||
| nowProvider = new NowProvider(); | ||
| nowProvider.set(new Date()); | ||
| expect(nowProvider.get()).toEqual(mockDateFromUrl); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this make more sense inside of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is more of a service, then an util because:
|
||
| * 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 { PublicMethodsOf } from '@kbn/utility-types'; | ||
| import { getForceNowFromUrl } from './lib'; | ||
|
|
||
| export type NowProviderInternalContract = PublicMethodsOf<NowProvider>; | ||
| export type NowProviderPublicContract = Pick<NowProviderInternalContract, 'get'>; | ||
|
|
||
| /** | ||
| * Used to synchronize time between parallel searches with relative time range that rely on `now`. | ||
| */ | ||
| export class NowProvider { | ||
|
||
| // TODO: service shouldn't access params in the URL | ||
| // instead it should be handled by apps | ||
| private readonly nowFromUrl = getForceNowFromUrl(); | ||
| private now?: Date; | ||
|
|
||
| constructor() {} | ||
|
|
||
| get(): Date { | ||
| if (this.nowFromUrl) return this.nowFromUrl; // now forced from URL always takes precedence | ||
| if (this.now) return this.now; | ||
| return new Date(); | ||
| } | ||
|
|
||
| set(now: Date) { | ||
| this.now = now; | ||
| } | ||
|
|
||
| reset() { | ||
| this.now = undefined; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We already have a shortcut const for timefilter declared above. It also might be a bit cleaner to merge the two subs that do the same thing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved 👍