-
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.
Add utility functions to event log plugin
- Loading branch information
Showing
7 changed files
with
84 additions
and
1 deletion.
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,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { millisToNanos } from './millis_to_nanos'; | ||
export { nanosToMillis } from './nanos_to_millis'; |
22 changes: 22 additions & 0 deletions
22
x-pack/plugins/event_log/common/lib/millis_to_nanos.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,22 @@ | ||
/* | ||
* 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 { millisToNanos } from './millis_to_nanos'; | ||
|
||
describe('millisToNanos', () => { | ||
test('should return "0" when passing 0 millis', () => { | ||
expect(millisToNanos(0)).toEqual('0'); | ||
}); | ||
|
||
test('should return "1000000" when passing in 1 millis', () => { | ||
expect(millisToNanos(1)).toEqual('1000000'); | ||
}); | ||
|
||
test('should return "9007199254740991000000" when passing in 9007199254740991 (Number.MAX_SAFE_INTEGER)', () => { | ||
expect(millisToNanos(9007199254740991)).toEqual('9007199254740991000000'); | ||
}); | ||
}); |
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,13 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export function millisToNanos(millis: number): string { | ||
if (millis === 0) { | ||
return '0'; | ||
} | ||
return `${millis}000000`; | ||
} |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/event_log/common/lib/nanos_to_millis.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,26 @@ | ||
/* | ||
* 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 { nanosToMillis } from './nanos_to_millis'; | ||
|
||
describe('nanosToMillis', () => { | ||
test('should return 0 when passing in "0" nanos', () => { | ||
expect(nanosToMillis('0')).toEqual(0); | ||
}); | ||
|
||
test('should drop decimals when passing in "1" nanos', () => { | ||
expect(nanosToMillis('1')).toEqual(0); | ||
}); | ||
|
||
test('should drop decimals when passing in "1000001" nanos', () => { | ||
expect(nanosToMillis('1000001')).toEqual(1); | ||
}); | ||
|
||
test('should return 9007199254740991 (Number.MAX_SAFE_INTEGER) when passing in "9007199254740991000000" nanos', () => { | ||
expect(nanosToMillis('9007199254740991000000')).toEqual(9007199254740991); | ||
}); | ||
}); |
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,12 @@ | ||
/* | ||
* 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 ONE_MILLION = BigInt(1000 * 1000); | ||
|
||
export function nanosToMillis(nanos: string): number { | ||
return Number(BigInt(nanos) / ONE_MILLION); | ||
} |
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