-
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.
Merge branch 'main' into 172506-obsux-add-feedback-form-to-apm
- Loading branch information
Showing
23 changed files
with
816 additions
and
70 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
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/observability/common/utils/get_interval_in_seconds.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { getIntervalInSeconds } from './get_interval_in_seconds'; | ||
|
||
describe('getIntervalInSeconds', () => { | ||
const testData = [ | ||
{ interval: '5ms', result: 0.005 }, | ||
{ interval: '70s', result: 70 }, | ||
{ interval: '25m', result: 1500 }, | ||
{ interval: '10h', result: 36000 }, | ||
{ interval: '3d', result: 259200 }, | ||
{ interval: '1w', result: 604800 }, | ||
{ interval: '1y', result: 30758400 }, | ||
]; | ||
|
||
it.each(testData)('getIntervalInSeconds($interval) = $result', ({ interval, result }) => { | ||
expect(getIntervalInSeconds(interval)).toBe(result); | ||
}); | ||
|
||
it('Throws error if interval is not valid', () => { | ||
expect(() => getIntervalInSeconds('invalid')).toThrow('Invalid interval string format.'); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/observability/common/utils/get_interval_in_seconds.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,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
const intervalUnits = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms']; | ||
const INTERVAL_STRING_RE = new RegExp('^([0-9\\.]*)\\s*(' + intervalUnits.join('|') + ')$'); | ||
|
||
interface UnitsToSeconds { | ||
[unit: string]: number; | ||
} | ||
|
||
const units: UnitsToSeconds = { | ||
ms: 0.001, | ||
s: 1, | ||
m: 60, | ||
h: 3600, | ||
d: 86400, | ||
w: 86400 * 7, | ||
M: 86400 * 30, | ||
y: 86400 * 356, | ||
}; | ||
|
||
export const getIntervalInSeconds = (interval: string): number => { | ||
const matches = interval.match(INTERVAL_STRING_RE); | ||
if (matches) { | ||
return parseFloat(matches[1]) * units[matches[2]]; | ||
} | ||
throw new Error('Invalid interval string format.'); | ||
}; |
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
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
Oops, something went wrong.