-
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.
Browse files
Browse the repository at this point in the history
* Add dateHistogramInterval utility * Fix editor bug * Fix tests * FIx imports to temporary solution * Remove wrongly merged translations * Remove old static.ts file
- Loading branch information
Tim Roes
authored
Jun 25, 2019
1 parent
4d79f43
commit b2f028a
Showing
23 changed files
with
299 additions
and
45 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
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,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.