Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { random, mean } from 'lodash';
import { SanitizedAlert, AlertInstanceSummary } from '../types';
import { IValidatedEvent } from '../../../event_log/server';
import { IValidatedEvent, millisToNanos, nanosToMillis } from '../../../event_log/server';
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';
import { alertInstanceSummaryFromEventLog } from './alert_instance_summary_from_event_log';

Expand Down Expand Up @@ -617,7 +617,7 @@ export class EventsFactory {
event: {
provider: EVENT_LOG_PROVIDER,
action: EVENT_LOG_ACTIONS.execute,
duration: random(2000, 180000) * 1000 * 1000,
duration: millisToNanos(random(2000, 180000)),
},
};

Expand Down Expand Up @@ -684,7 +684,7 @@ export class EventsFactory {
return this.events
.filter((ev) => ev?.event?.action === 'execute' && ev?.event?.duration !== undefined)
.reduce((res: Record<string, number>, ev) => {
res[ev?.['@timestamp']!] = ev?.event?.duration! / (1000 * 1000);
res[ev?.['@timestamp']!] = nanosToMillis(ev?.event?.duration!);
return res;
}, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import { mean } from 'lodash';
import { SanitizedAlert, AlertInstanceSummary, AlertInstanceStatus } from '../types';
import { IEvent } from '../../../event_log/server';
import { IEvent, nanosToMillis } from '../../../event_log/server';
import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER, LEGACY_EVENT_LOG_ACTIONS } from '../plugin';

const Millis2Nanos = 1000 * 1000;

export interface AlertInstanceSummaryFromEventLogParams {
alert: SanitizedAlert<{ bar: boolean }>;
events: IEvent[];
Expand Down Expand Up @@ -75,7 +73,7 @@ export function alertInstanceSummaryFromEventLog(
}

if (event?.event?.duration) {
const eventDirationMillis = event?.event?.duration / Millis2Nanos;
const eventDirationMillis = nanosToMillis(event.event.duration);
if (event?.['@timestamp']) {
eventDurationsWithTimestamp[event?.['@timestamp']] = eventDirationMillis;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function createAlertEventLogRecordObject(params: CreateAlertEventLogRecor
category: [ruleType.producer],
...(state?.start ? { start: state.start as string } : {}),
...(state?.end ? { end: state.end as string } : {}),
...(state?.duration !== undefined ? { duration: state.duration as number } : {}),
...(state?.duration !== undefined ? { duration: state.duration as string } : {}),
},
kibana: {
...(alerting ? alerting : {}),
Expand Down
50 changes: 25 additions & 25 deletions x-pack/plugins/alerting/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ describe('Task Runner', () => {
action: 'new-instance',
category: ['alerts'],
kind: 'alert',
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
kibana: {
Expand Down Expand Up @@ -519,7 +519,7 @@ describe('Task Runner', () => {
event: {
action: 'active-instance',
category: ['alerts'],
duration: 0,
duration: '0',
kind: 'alert',
start: '1970-01-01T00:00:00.000Z',
},
Expand Down Expand Up @@ -706,7 +706,7 @@ describe('Task Runner', () => {
action: 'new-instance',
category: ['alerts'],
kind: 'alert',
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
kibana: {
Expand Down Expand Up @@ -739,7 +739,7 @@ describe('Task Runner', () => {
action: 'active-instance',
category: ['alerts'],
kind: 'alert',
duration: 0,
duration: '0',
start: '1970-01-01T00:00:00.000Z',
},
kibana: {
Expand Down Expand Up @@ -1041,7 +1041,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 86400000000000,
"duration": "86400000000000",
"kind": "alert",
"start": "1969-12-31T00:00:00.000Z",
},
Expand Down Expand Up @@ -1379,7 +1379,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -1415,7 +1415,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -1606,7 +1606,7 @@ describe('Task Runner', () => {
},
"state": Object {
"bar": false,
"duration": 86400000000000,
"duration": "86400000000000",
"start": "1969-12-31T00:00:00.000Z",
},
},
Expand Down Expand Up @@ -1675,7 +1675,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 64800000000000,
"duration": "64800000000000",
"end": "1970-01-01T00:00:00.000Z",
"kind": "alert",
"start": "1969-12-31T06:00:00.000Z",
Expand Down Expand Up @@ -1711,7 +1711,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 86400000000000,
"duration": "86400000000000",
"kind": "alert",
"start": "1969-12-31T00:00:00.000Z",
},
Expand Down Expand Up @@ -2189,7 +2189,7 @@ describe('Task Runner', () => {
},
"state": Object {
"bar": false,
"duration": 86400000000000,
"duration": "86400000000000",
"start": "1969-12-31T00:00:00.000Z",
},
},
Expand Down Expand Up @@ -2242,7 +2242,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 64800000000000,
"duration": "64800000000000",
"end": "1970-01-01T00:00:00.000Z",
"kind": "alert",
"start": "1969-12-31T06:00:00.000Z",
Expand Down Expand Up @@ -2279,7 +2279,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 86400000000000,
"duration": "86400000000000",
"kind": "alert",
"start": "1969-12-31T00:00:00.000Z",
},
Expand Down Expand Up @@ -3327,7 +3327,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -3363,7 +3363,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -3399,7 +3399,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -3435,7 +3435,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 0,
"duration": "0",
"kind": "alert",
"start": "1970-01-01T00:00:00.000Z",
},
Expand Down Expand Up @@ -3536,15 +3536,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: '1969-12-31T00:00:00.000Z',
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: {},
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -3613,7 +3613,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 86400000000000,
"duration": "86400000000000",
"kind": "alert",
"start": "1969-12-31T00:00:00.000Z",
},
Expand Down Expand Up @@ -3649,7 +3649,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 64800000000000,
"duration": "64800000000000",
"kind": "alert",
"start": "1969-12-31T06:00:00.000Z",
},
Expand Down Expand Up @@ -3939,15 +3939,15 @@ describe('Task Runner', () => {
state: {
bar: false,
start: '1969-12-31T00:00:00.000Z',
duration: 80000000000,
duration: '80000000000',
},
},
'2': {
meta: {},
state: {
bar: false,
start: '1969-12-31T06:00:00.000Z',
duration: 70000000000,
duration: '70000000000',
},
},
},
Expand Down Expand Up @@ -4016,7 +4016,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 86400000000000,
"duration": "86400000000000",
"end": "1970-01-01T00:00:00.000Z",
"kind": "alert",
"start": "1969-12-31T00:00:00.000Z",
Expand Down Expand Up @@ -4052,7 +4052,7 @@ describe('Task Runner', () => {
"category": Array [
"alerts",
],
"duration": 64800000000000,
"duration": "64800000000000",
"end": "1970-01-01T00:00:00.000Z",
"kind": "alert",
"start": "1969-12-31T06:00:00.000Z",
Expand Down
24 changes: 15 additions & 9 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ import {
import { promiseResult, map, Resultable, asOk, asErr, resolveErr } from '../lib/result_type';
import { taskInstanceToAlertTaskInstance } from './alert_task_instance';
import { EVENT_LOG_ACTIONS } from '../plugin';
import { IEvent, IEventLogger, SAVED_OBJECT_REL_PRIMARY } from '../../../event_log/server';
import {
IEvent,
IEventLogger,
SAVED_OBJECT_REL_PRIMARY,
millisToNanos,
nanosToMillis,
} from '../../../event_log/server';
import { isAlertSavedObjectNotFoundError } from '../lib/is_alert_not_found_error';
import { RulesClient } from '../rules_client';
import { partiallyUpdateAlert } from '../saved_objects';
Expand Down Expand Up @@ -594,7 +600,7 @@ export class TaskRunner<

// Copy duration into execution status if available
if (null != event.event?.duration) {
executionStatus.lastDuration = Math.round(event.event?.duration / Millis2Nanos);
executionStatus.lastDuration = nanosToMillis(event.event?.duration);
}

// if executionStatus indicates an error, fill in fields in
Expand Down Expand Up @@ -695,9 +701,9 @@ function trackAlertDurations<
const state = originalAlertIds.includes(id)
? originalAlerts[id].getState()
: currentAlerts[id].getState();
const duration = state.start
? (new Date(currentTime).valueOf() - new Date(state.start as string).valueOf()) * 1000 * 1000 // nanoseconds
: undefined;
const durationInMs =
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
const duration = state.start ? millisToNanos(durationInMs) : undefined;
currentAlerts[id].replaceState({
...state,
...(state.start ? { start: state.start } : {}),
Expand All @@ -708,9 +714,9 @@ function trackAlertDurations<
// Inject end time into instance state of recovered instances
for (const id of recoveredAlertIds) {
const state = recoveredAlerts[id].getState();
const duration = state.start
? (new Date(currentTime).valueOf() - new Date(state.start as string).valueOf()) * 1000 * 1000 // nanoseconds
: undefined;
const durationInMs =
new Date(currentTime).valueOf() - new Date(state.start as string).valueOf();
const duration = state.start ? millisToNanos(durationInMs) : undefined;
recoveredAlerts[id].replaceState({
...state,
...(duration ? { duration } : {}),
Expand Down Expand Up @@ -835,7 +841,7 @@ function generateNewAndRecoveredInstanceEvents<
category: [ruleType.producer],
...(state?.start ? { start: state.start as string } : {}),
...(state?.end ? { end: state.end as string } : {}),
...(state?.duration !== undefined ? { duration: state.duration as number } : {}),
...(state?.duration !== undefined ? { duration: state.duration as string } : {}),
},
kibana: {
alerting: {
Expand Down
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 = '/api/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';
26 changes: 26 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,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 { 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');
});

test('should round to "1000000" wheen passing in 0.75 millis', () => {
expect(millisToNanos(0.75)).toEqual('1000000');
});
});
14 changes: 14 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,14 @@
/*
* 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 {
const roundedMillis = Math.round(millis);
if (roundedMillis === 0) {
return '0';
}
return `${roundedMillis}000000`;
}
Loading