Skip to content

Commit

Permalink
Add utility functions to event log plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Apr 29, 2022
1 parent 4a0dbdb commit ea053d6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/plugins/event_log/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export const BASE_EVENT_LOG_API_PATH = '/internal/event_log';
export { millisToNanos, nanosToMillis } from './lib';
9 changes: 9 additions & 0 deletions x-pack/plugins/event_log/common/lib/index.ts
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 x-pack/plugins/event_log/common/lib/millis_to_nanos.test.ts
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');
});
});
13 changes: 13 additions & 0 deletions x-pack/plugins/event_log/common/lib/millis_to_nanos.ts
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 x-pack/plugins/event_log/common/lib/nanos_to_millis.test.ts
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);
});
});
12 changes: 12 additions & 0 deletions x-pack/plugins/event_log/common/lib/nanos_to_millis.ts
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);
}
2 changes: 1 addition & 1 deletion x-pack/plugins/event_log/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"generated/*",
// have to declare *.json explicitly due to https://github.com/microsoft/TypeScript/issues/25636
"generated/*.json",
"common/*"
"common/**/*"
],
"references": [
{ "path": "../../../src/core/tsconfig.json" },
Expand Down

0 comments on commit ea053d6

Please sign in to comment.