Skip to content

Commit 082120b

Browse files
committed
Use new event log utility functions
1 parent ea053d6 commit 082120b

12 files changed

Lines changed: 36 additions & 46 deletions

File tree

x-pack/plugins/alerting/server/lib/alert_summary_from_event_log.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { random, mean } from 'lodash';
99
import { SanitizedRule, AlertSummary } from '../types';
10-
import { IValidatedEvent } from '@kbn/event-log-plugin/server';
10+
import { IValidatedEvent, millisToNanos, nanosToMillis } from '@kbn/event-log-plugin/server';
1111
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';
1212
import { alertSummaryFromEventLog } from './alert_summary_from_event_log';
1313

@@ -643,7 +643,7 @@ export class EventsFactory {
643643
event: {
644644
provider: EVENT_LOG_PROVIDER,
645645
action: EVENT_LOG_ACTIONS.execute,
646-
duration: `${random(2000, 180000)}000000`,
646+
duration: millisToNanos(random(2000, 180000)),
647647
},
648648
};
649649

@@ -710,7 +710,7 @@ export class EventsFactory {
710710
return this.events
711711
.filter((ev) => ev?.event?.action === 'execute' && ev?.event?.duration !== undefined)
712712
.reduce((res: Record<string, number>, ev) => {
713-
res[ev?.['@timestamp']!] = Number(BigInt(ev?.event?.duration!) / BigInt(1000 * 1000));
713+
res[ev?.['@timestamp']!] = nanosToMillis(ev?.event?.duration!);
714714
return res;
715715
}, {});
716716
}

x-pack/plugins/alerting/server/lib/alert_summary_from_event_log.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
*/
77

88
import { mean } from 'lodash';
9-
import { IEvent } from '@kbn/event-log-plugin/server';
9+
import { IEvent, nanosToMillis } from '@kbn/event-log-plugin/server';
1010
import { SanitizedRule, AlertSummary, AlertStatus } from '../types';
1111
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';
1212

13-
const Millis2Nanos = BigInt(1000 * 1000);
14-
1513
export interface AlertSummaryFromEventLogParams {
1614
rule: SanitizedRule<{ bar: boolean }>;
1715
events: IEvent[];
@@ -111,7 +109,7 @@ export function alertSummaryFromEventLog(params: AlertSummaryFromEventLogParams)
111109
}
112110

113111
if (event?.event?.duration) {
114-
const eventDirationMillis = Number(BigInt(event.event.duration) / Millis2Nanos);
112+
const eventDirationMillis = nanosToMillis(event.event.duration);
115113
eventDurations.push(eventDirationMillis);
116114
eventDurationsWithTimestamp[event['@timestamp']!] = eventDirationMillis;
117115
}

x-pack/plugins/alerting/server/task_runner/task_runner.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import uuid from 'uuid';
1212
import { addSpaceIdToPath } from '@kbn/spaces-plugin/server';
1313
import { KibanaRequest, Logger } from '@kbn/core/server';
1414
import { ConcreteTaskInstance, throwUnrecoverableError } from '@kbn/task-manager-plugin/server';
15-
import { IEvent, SAVED_OBJECT_REL_PRIMARY } from '@kbn/event-log-plugin/server';
15+
import {
16+
IEvent,
17+
SAVED_OBJECT_REL_PRIMARY,
18+
millisToNanos,
19+
nanosToMillis,
20+
} from '@kbn/event-log-plugin/server';
1621
import { TaskRunnerContext } from './task_runner_factory';
1722
import { createExecutionHandler, ExecutionHandler } from './create_execution_handler';
1823
import { Alert, createAlertFactory } from '../alert';
@@ -803,9 +808,7 @@ export class TaskRunner<
803808

804809
// Copy duration into execution status if available
805810
if (null != event.event?.duration) {
806-
executionStatus.lastDuration = Math.round(
807-
Number(BigInt(event.event?.duration) / BigInt(Millis2Nanos))
808-
);
811+
executionStatus.lastDuration = nanosToMillis(event.event?.duration);
809812
monitoringHistory.duration = executionStatus.lastDuration;
810813
}
811814

@@ -1066,8 +1069,7 @@ function trackAlertDurations<
10661069
: currentAlerts[id].getState();
10671070
const durationInMs =
10681071
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
1069-
const durationInNanoStr = durationInMs !== 0 ? `${durationInMs}000000` : '0';
1070-
const duration = state.start ? durationInNanoStr : undefined;
1072+
const duration = state.start ? millisToNanos(durationInMs) : undefined;
10711073
currentAlerts[id].replaceState({
10721074
...state,
10731075
...(state.start ? { start: state.start } : {}),
@@ -1080,8 +1082,7 @@ function trackAlertDurations<
10801082
const state = recoveredAlerts[id].getState();
10811083
const durationInMs =
10821084
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
1083-
const durationInNanoStr = durationInMs !== 0 ? `${durationInMs}000000` : '0';
1084-
const duration = state.start ? durationInNanoStr : undefined;
1085+
const duration = state.start ? millisToNanos(durationInMs) : undefined;
10851086
recoveredAlerts[id].replaceState({
10861087
...state,
10871088
...(duration ? { duration } : {}),

x-pack/plugins/event_log/common/lib/nanos_to_millis.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ describe('nanosToMillis', () => {
2323
test('should return 9007199254740991 (Number.MAX_SAFE_INTEGER) when passing in "9007199254740991000000" nanos', () => {
2424
expect(nanosToMillis('9007199254740991000000')).toEqual(9007199254740991);
2525
});
26+
27+
test('should work when numbers are passed in', () => {
28+
expect(nanosToMillis(0)).toEqual(0);
29+
expect(nanosToMillis(1)).toEqual(0);
30+
expect(nanosToMillis(1000001)).toEqual(1);
31+
});
2632
});

x-pack/plugins/event_log/common/lib/nanos_to_millis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
const ONE_MILLION = BigInt(1000 * 1000);
99

10-
export function nanosToMillis(nanos: string): number {
10+
export function nanosToMillis(nanos: string | number): number {
1111
return Number(BigInt(nanos) / ONE_MILLION);
1212
}

x-pack/plugins/event_log/server/event_logger.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 2.0.
66
*/
77

8+
import { nanosToMillis } from '../common';
89
import { IEvent, IEventLogger, IEventLogService } from '.';
910
import { ECS_VERSION } from './types';
1011
import { EventLogService } from './event_log_service';
@@ -137,9 +138,9 @@ describe('EventLogger', () => {
137138

138139
expect(timeStopValue).toBeGreaterThanOrEqual(timeStartValue);
139140

140-
const duration = BigInt(event.event!.duration!);
141+
const duration = Number(event.event!.duration!);
141142
expect(duration).toBeGreaterThan(0.95 * delayMS * 1000 * 1000);
142-
expect(Number(duration / BigInt(1000 * 1000))).toBeCloseTo(timeStopValue - timeStartValue);
143+
expect(nanosToMillis(duration)).toBeCloseTo(timeStopValue - timeStartValue);
143144
});
144145

145146
test('timing method endTiming() method works when startTiming() is not called', async () => {

x-pack/plugins/event_log/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { PluginInitializerContext, PluginConfigDescriptor } from '@kbn/core/serv
99
import { ConfigSchema, IEventLogConfig } from './types';
1010
import { Plugin } from './plugin';
1111

12+
export { millisToNanos, nanosToMillis } from '../common';
13+
1214
export type {
1315
IEventLogService,
1416
IEventLogger,

x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/execute.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import expect from '@kbn/expect';
9-
import { IValidatedEvent } from '@kbn/event-log-plugin/server';
9+
import { IValidatedEvent, nanosToMillis } from '@kbn/event-log-plugin/server';
1010
import { UserAtSpaceScenarios } from '../../scenarios';
1111
import {
1212
ESTestIndexTool,
@@ -17,8 +17,6 @@ import {
1717
} from '../../../common/lib';
1818
import { FtrProviderContext } from '../../../common/ftr_provider_context';
1919

20-
const NANOS_IN_MILLIS = BigInt(1000 * 1000);
21-
2220
// eslint-disable-next-line import/no-default-export
2321
export default function ({ getService }: FtrProviderContext) {
2422
const supertest = getService('supertest');
@@ -543,10 +541,7 @@ export default function ({ getService }: FtrProviderContext) {
543541
expect(startExecuteEventStart).to.equal(executeEventStart);
544542
expect(executeEventEnd).to.be.ok();
545543

546-
const durationDiff = Math.abs(
547-
Math.round(Number(BigInt(duration!) / NANOS_IN_MILLIS)) -
548-
(executeEventEnd - executeEventStart)
549-
);
544+
const durationDiff = Math.abs(nanosToMillis(duration!) - (executeEventEnd - executeEventStart));
550545

551546
// account for rounding errors
552547
expect(durationDiff < 1).to.equal(true);

x-pack/test/alerting_api_integration/security_and_spaces/tests/alerting/alerts.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import expect from '@kbn/expect';
99
import { omit } from 'lodash';
1010
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
11-
import { IValidatedEvent } from '@kbn/event-log-plugin/server';
11+
import { IValidatedEvent, nanosToMillis } from '@kbn/event-log-plugin/server';
1212
import { TaskRunning, TaskRunningStage } from '@kbn/task-manager-plugin/server/task_running';
1313
import { ConcreteTaskInstance } from '@kbn/task-manager-plugin/server';
1414
import { UserAtSpaceScenarios, Superuser } from '../../scenarios';
@@ -25,8 +25,6 @@ import {
2525
getEventLog,
2626
} from '../../../common/lib';
2727

28-
const NANOS_IN_MILLIS = BigInt(1000 * 1000);
29-
3028
// eslint-disable-next-line import/no-default-export
3129
export default function alertTests({ getService }: FtrProviderContext) {
3230
const supertest = getService('supertest');
@@ -1307,9 +1305,7 @@ instanceStateValue: true
13071305
expect(eventStart).to.be.ok();
13081306
expect(eventEnd).to.be.ok();
13091307

1310-
const durationDiff = Math.abs(
1311-
Math.round(Number(BigInt(duration!) / NANOS_IN_MILLIS)) - (eventEnd - eventStart)
1312-
);
1308+
const durationDiff = Math.abs(nanosToMillis(duration!) - (eventEnd - eventStart));
13131309

13141310
// account for rounding errors
13151311
expect(durationDiff < 1).to.equal(true);

x-pack/test/alerting_api_integration/spaces_only/tests/actions/execute.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import type { Client } from '@elastic/elasticsearch';
88
import expect from '@kbn/expect';
9-
import { IValidatedEvent } from '@kbn/event-log-plugin/server';
9+
import { IValidatedEvent, nanosToMillis } from '@kbn/event-log-plugin/server';
1010
import { Spaces } from '../../scenarios';
1111
import {
1212
ESTestIndexTool,
@@ -17,8 +17,6 @@ import {
1717
} from '../../../common/lib';
1818
import { FtrProviderContext } from '../../../common/ftr_provider_context';
1919

20-
const NANOS_IN_MILLIS = BigInt(1000 * 1000);
21-
2220
// eslint-disable-next-line import/no-default-export
2321
export default function ({ getService }: FtrProviderContext) {
2422
const supertest = getService('supertest');
@@ -377,10 +375,7 @@ export default function ({ getService }: FtrProviderContext) {
377375
expect(startExecuteEventStart).to.equal(executeEventStart);
378376
expect(executeEventEnd).to.be.ok();
379377

380-
const durationDiff = Math.abs(
381-
Math.round(Number(BigInt(duration!) / NANOS_IN_MILLIS)) -
382-
(executeEventEnd - executeEventStart)
383-
);
378+
const durationDiff = Math.abs(nanosToMillis(duration!) - (executeEventEnd - executeEventStart));
384379

385380
// account for rounding errors
386381
expect(durationDiff < 1).to.equal(true);

0 commit comments

Comments
 (0)