-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Add dateHistogramInterval utility #39091
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df6f942
Add dateHistogramInterval utility
timroes bfefb51
Fix editor bug
timroes 19048a2
Fix tests
timroes 3bdc700
FIx imports to temporary solution
timroes 1af4102
Merge branch 'master' into date-histogram-interval
timroes 08d7d27
Remove wrongly merged translations
timroes 7059852
Remove old static.ts file
timroes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 50 additions & 0 deletions
50
src/legacy/core_plugins/data/common/date_histogram_interval.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,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 { dateHistogramInterval } from './date_histogram_interval'; | ||
|
||
describe('dateHistogramInterval', () => { | ||
it('should return calender_interval key for calendar intervals', () => { | ||
expect(dateHistogramInterval('1m')).toEqual({ calendar_interval: '1m' }); | ||
expect(dateHistogramInterval('1h')).toEqual({ calendar_interval: '1h' }); | ||
expect(dateHistogramInterval('1d')).toEqual({ calendar_interval: '1d' }); | ||
expect(dateHistogramInterval('1w')).toEqual({ calendar_interval: '1w' }); | ||
expect(dateHistogramInterval('1M')).toEqual({ calendar_interval: '1M' }); | ||
expect(dateHistogramInterval('1y')).toEqual({ calendar_interval: '1y' }); | ||
}); | ||
|
||
it('should return fixed_interval key for fixed intervals', () => { | ||
expect(dateHistogramInterval('1ms')).toEqual({ fixed_interval: '1ms' }); | ||
expect(dateHistogramInterval('42ms')).toEqual({ fixed_interval: '42ms' }); | ||
expect(dateHistogramInterval('1s')).toEqual({ fixed_interval: '1s' }); | ||
expect(dateHistogramInterval('42s')).toEqual({ fixed_interval: '42s' }); | ||
expect(dateHistogramInterval('42m')).toEqual({ fixed_interval: '42m' }); | ||
expect(dateHistogramInterval('42h')).toEqual({ fixed_interval: '42h' }); | ||
expect(dateHistogramInterval('42d')).toEqual({ fixed_interval: '42d' }); | ||
}); | ||
|
||
it('should throw an error on invalid intervals', () => { | ||
expect(() => dateHistogramInterval('2w')).toThrow(); | ||
expect(() => dateHistogramInterval('2M')).toThrow(); | ||
expect(() => dateHistogramInterval('2y')).toThrow(); | ||
expect(() => dateHistogramInterval('2')).toThrow(); | ||
expect(() => dateHistogramInterval('y')).toThrow(); | ||
expect(() => dateHistogramInterval('0.5h')).toThrow(); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
src/legacy/core_plugins/data/common/date_histogram_interval.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 { parseEsInterval } from './parse_es_interval'; | ||
|
||
type Interval = { fixed_interval: string } | { calendar_interval: string }; | ||
|
||
/** | ||
* Checks whether a given Elasticsearch interval is a calendar or fixed interval | ||
* and returns an object containing the appropriate date_histogram property for that | ||
* interval. So it will return either an object containing the fixed_interval key for | ||
* that interval or a calendar_interval. If the specified interval was not a valid Elasticsearch | ||
* interval this method will throw an error. | ||
* | ||
* You can simply spread the returned value of this method into your date_histogram. | ||
* @example | ||
* const aggregation = { | ||
* date_histogram: { | ||
* field: 'date', | ||
* ...dateHistogramInterval('24h'), | ||
* } | ||
* }; | ||
* | ||
* @param interval The interval string to return the appropriate date_histogram key for. | ||
*/ | ||
export function dateHistogramInterval(interval: string): Interval { | ||
const { type } = parseEsInterval(interval); | ||
if (type === 'calendar') { | ||
return { calendar_interval: interval }; | ||
} else { | ||
return { fixed_interval: interval }; | ||
} | ||
} |
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,29 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** @public static code */ | ||
export { dateHistogramInterval } from './date_histogram_interval'; | ||
/** @public static code */ | ||
export { | ||
isValidEsInterval, | ||
InvalidEsCalendarIntervalError, | ||
InvalidEsIntervalFormatError, | ||
parseEsInterval, | ||
ParsedInterval, | ||
} from './parse_es_interval'; |
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
38 changes: 38 additions & 0 deletions
38
src/legacy/core_plugins/data/common/parse_es_interval/is_valid_es_interval.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,38 @@ | ||
/* | ||
* 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 { parseEsInterval } from './parse_es_interval'; | ||
|
||
/** | ||
* Checks whether a given interval string (e.g. 1w, 24h, ...) is a valid Elasticsearch interval. | ||
* Will return false if the interval is not valid in Elasticsearch, otherwise true. | ||
* Invalid intervals might be: 2f, since there is no unit 'f'; 2w, since weeks and month intervals | ||
* are only allowed with a value of 1, etc. | ||
* | ||
* @param interval The interval string like 1w, 24h | ||
* @returns True if the interval is valid for Elasticsearch | ||
*/ | ||
export function isValidEsInterval(interval: string): boolean { | ||
try { | ||
parseEsInterval(interval); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** @public static code */ | ||
export { dateHistogramInterval } from '../common/date_histogram_interval'; | ||
/** @public static code */ | ||
export { | ||
isValidEsInterval, | ||
InvalidEsCalendarIntervalError, | ||
InvalidEsIntervalFormatError, | ||
parseEsInterval, | ||
ParsedInterval, | ||
} from '../common/parse_es_interval'; |
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,30 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** @public static code */ | ||
export { dateHistogramInterval } from '../common/date_histogram_interval'; | ||
|
||
/** @public static code */ | ||
export { | ||
isValidEsInterval, | ||
InvalidEsCalendarIntervalError, | ||
InvalidEsIntervalFormatError, | ||
parseEsInterval, | ||
ParsedInterval, | ||
} from '../common/parse_es_interval'; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
context is to allow nested folders?
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.
rather the opposite, to allow importing from the plugin directly, like
plugin/data
, but to be honest that's not needed anymore so I can actually revert that change here.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.
Well but since we might hit that problem another time I might just leave it, since you anyway already approved it :)