-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce date histogram time base configuration to EditorConfig (#22344
) * Add `default` and `baseInterval` configuration ability to date histogram EditorConfig * Change EditorConfig `warning` to `help`, show `help` below date histogram and histogram interval inputs
- Loading branch information
Showing
15 changed files
with
518 additions
and
42 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
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. | ||
*/ | ||
|
||
declare module '@kbn/datemath' { | ||
const dateMath: { | ||
parse: any; | ||
unitsMap: any; | ||
units: string[]; | ||
unitsAsc: string[]; | ||
unitsDesc: string[]; | ||
}; | ||
export default dateMath; | ||
} |
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,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "./target" | ||
}, | ||
"include": [ | ||
"./src/**/*.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
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,54 @@ | ||
/* | ||
* 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'; | ||
|
||
describe('parseEsInterval', () => { | ||
it('should correctly parse an interval containing unit and single value', () => { | ||
expect(parseEsInterval('1ms')).toEqual({ value: 1, unit: 'ms', type: 'fixed' }); | ||
expect(parseEsInterval('1s')).toEqual({ value: 1, unit: 's', type: 'fixed' }); | ||
expect(parseEsInterval('1m')).toEqual({ value: 1, unit: 'm', type: 'calendar' }); | ||
expect(parseEsInterval('1h')).toEqual({ value: 1, unit: 'h', type: 'calendar' }); | ||
expect(parseEsInterval('1d')).toEqual({ value: 1, unit: 'd', type: 'calendar' }); | ||
expect(parseEsInterval('1w')).toEqual({ value: 1, unit: 'w', type: 'calendar' }); | ||
expect(parseEsInterval('1M')).toEqual({ value: 1, unit: 'M', type: 'calendar' }); | ||
expect(parseEsInterval('1y')).toEqual({ value: 1, unit: 'y', type: 'calendar' }); | ||
}); | ||
|
||
it('should correctly parse an interval containing unit and multiple value', () => { | ||
expect(parseEsInterval('250ms')).toEqual({ value: 250, unit: 'ms', type: 'fixed' }); | ||
expect(parseEsInterval('90s')).toEqual({ value: 90, unit: 's', type: 'fixed' }); | ||
expect(parseEsInterval('60m')).toEqual({ value: 60, unit: 'm', type: 'fixed' }); | ||
expect(parseEsInterval('12h')).toEqual({ value: 12, unit: 'h', type: 'fixed' }); | ||
expect(parseEsInterval('7d')).toEqual({ value: 7, unit: 'd', type: 'fixed' }); | ||
}); | ||
|
||
it('should throw an error for intervals containing calendar unit and multiple value', () => { | ||
expect(() => parseEsInterval('4w')).toThrowError(); | ||
expect(() => parseEsInterval('12M')).toThrowError(); | ||
expect(() => parseEsInterval('10y')).toThrowError(); | ||
}); | ||
|
||
it('should throw an error for invalid interval formats', () => { | ||
expect(() => parseEsInterval('1')).toThrowError(); | ||
expect(() => parseEsInterval('h')).toThrowError(); | ||
expect(() => parseEsInterval('0m')).toThrowError(); | ||
expect(() => parseEsInterval('0.5h')).toThrowError(); | ||
}); | ||
}); |
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,66 @@ | ||
/* | ||
* 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 '@kbn/datemath'; | ||
|
||
const ES_INTERVAL_STRING_REGEX = new RegExp( | ||
'^([1-9][0-9]*)\\s*(' + dateMath.units.join('|') + ')$' | ||
); | ||
|
||
/** | ||
* Extracts interval properties from an ES interval string. Disallows unrecognized interval formats | ||
* and fractional values. Converts some intervals from "calendar" to "fixed" when the number of | ||
* units is larger than 1, and throws an error for others. | ||
* | ||
* Conversion rules: | ||
* | ||
* | Interval | Single unit type | Multiple units type | | ||
* | -------- | ---------------- | ------------------- | | ||
* | ms | fixed | fixed | | ||
* | s | fixed | fixed | | ||
* | m | fixed | fixed | | ||
* | h | calendar | fixed | | ||
* | d | calendar | fixed | | ||
* | w | calendar | N/A - disallowed | | ||
* | M | calendar | N/A - disallowed | | ||
* | y | calendar | N/A - disallowed | | ||
* | ||
*/ | ||
export function parseEsInterval(interval: string): { value: number; unit: string; type: string } { | ||
const matches = String(interval) | ||
.trim() | ||
.match(ES_INTERVAL_STRING_REGEX); | ||
|
||
if (!matches) { | ||
throw Error(`Invalid interval format: ${interval}`); | ||
} | ||
|
||
const value = matches && parseFloat(matches[1]); | ||
const unit = matches && matches[2]; | ||
const type = unit && dateMath.unitsMap[unit].type; | ||
|
||
if (type === 'calendar' && value !== 1) { | ||
throw Error(`Invalid calendar interval: ${interval}, value must be 1`); | ||
} | ||
|
||
return { | ||
value, | ||
unit, | ||
type: (type === 'mixed' && value === 1) || type === 'calendar' ? 'calendar' : 'fixed', | ||
}; | ||
} |
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
Oops, something went wrong.