-
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] Validate timerange before submitting query to ES (#69363)
- Loading branch information
Showing
5 changed files
with
120 additions
and
4 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
47 changes: 47 additions & 0 deletions
47
src/plugins/discover/public/application/helpers/validate_time_range.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,47 @@ | ||
/* | ||
* 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 { validateTimeRange } from './validate_time_range'; | ||
import { notificationServiceMock } from '../../../../../core/public/mocks'; | ||
|
||
describe('Discover validateTimeRange', () => { | ||
test('validates given time ranges correctly', async () => { | ||
const { toasts } = notificationServiceMock.createStartContract(); | ||
[ | ||
{ from: '', to: '', result: false }, | ||
{ from: 'now', to: 'now+1h', result: true }, | ||
{ from: 'now', to: 'lala+1h', result: false }, | ||
{ from: '', to: 'now', result: false }, | ||
{ from: 'now', to: '', result: false }, | ||
{ from: ' 2020-06-02T13:36:13.689Z', to: 'now', result: true }, | ||
{ from: ' 2020-06-02T13:36:13.689Z', to: '2020-06-02T13:36:13.690Z', result: true }, | ||
].map((test) => { | ||
expect(validateTimeRange({ from: test.from, to: test.to }, toasts)).toEqual(test.result); | ||
}); | ||
}); | ||
|
||
test('displays a toast when invalid data is entered', async () => { | ||
const { toasts } = notificationServiceMock.createStartContract(); | ||
expect(validateTimeRange({ from: 'now', to: 'null' }, toasts)).toEqual(false); | ||
expect(toasts.addDanger).toHaveBeenCalledWith({ | ||
title: 'Invalid time range', | ||
text: "The provided time range is invalid. (from: 'now', to: 'null')", | ||
}); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
src/plugins/discover/public/application/helpers/validate_time_range.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,49 @@ | ||
/* | ||
* 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 dateMath from '@elastic/datemath'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { ToastsStart } from 'kibana/public'; | ||
|
||
/** | ||
* Validates a given time filter range, provided by URL or UI | ||
* Unless valid, it returns false and displays a notification | ||
*/ | ||
export function validateTimeRange( | ||
{ from, to }: { from: string; to: string }, | ||
toastNotifications: ToastsStart | ||
): boolean { | ||
const fromMoment = dateMath.parse(from); | ||
const toMoment = dateMath.parse(to); | ||
if (!fromMoment || !toMoment || !fromMoment.isValid() || !toMoment.isValid()) { | ||
toastNotifications.addDanger({ | ||
title: i18n.translate('discover.notifications.invalidTimeRangeTitle', { | ||
defaultMessage: `Invalid time range`, | ||
}), | ||
text: i18n.translate('discover.notifications.invalidTimeRangeText', { | ||
defaultMessage: `The provided time range is invalid. (from: '{from}', to: '{to}')`, | ||
values: { | ||
from, | ||
to, | ||
}, | ||
}), | ||
}); | ||
return false; | ||
} | ||
return true; | ||
} |
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