From 8a49726f93cddbabe6a79182e495ce6c06f488c7 Mon Sep 17 00:00:00 2001 From: Davis Plumlee Date: Wed, 21 Oct 2020 12:13:40 -0600 Subject: [PATCH 1/3] changes look-back logic --- .../pages/detection_engine/rules/helpers.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx index 4dbcffbc807ec..cf35676c8e091 100644 --- a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx +++ b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx @@ -118,15 +118,14 @@ export const getHumanizedDuration = (from: string, interval: string): string => const intervalValue = dateMath.parse(`now-${interval}`) ?? moment(); const fromDuration = moment.duration(intervalValue.diff(fromValue)); - const fromHumanize = `${Math.floor(fromDuration.asHours())}h`; - if (fromDuration.asSeconds() < 60) { + if (Number.isInteger(fromDuration.asHours())) { + return `${fromDuration.asHours()}h`; + } else if (Number.isInteger(fromDuration.asMinutes())) { + return `${fromDuration.asMinutes()}m`; + } else { return `${Math.floor(fromDuration.asSeconds())}s`; - } else if (fromDuration.asMinutes() < 60) { - return `${Math.floor(fromDuration.asMinutes())}m`; } - - return fromHumanize; }; export const getAboutStepsData = (rule: Rule, detailsView: boolean): AboutStepRule => { From 0280095304f3d20837cd0c23ebff2af373c22d86 Mon Sep 17 00:00:00 2001 From: Davis Plumlee Date: Wed, 21 Oct 2020 13:11:51 -0600 Subject: [PATCH 2/3] updates tests --- .../detection_engine/rules/helpers.test.tsx | 12 ++++++------ .../pages/detection_engine/rules/helpers.tsx | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx index 0cd75506fa9f5..df1ab47771867 100644 --- a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx @@ -300,28 +300,28 @@ describe('rule helpers', () => { }); describe('getHumanizedDuration', () => { - test('returns from as seconds if from duration is less than a minute', () => { + test('returns from as seconds if from duration is specified in seconds', () => { const result = getHumanizedDuration('now-62s', '1m'); expect(result).toEqual('2s'); }); - test('returns from as minutes if from duration is less than an hour', () => { + test('returns from as minutes if from duration is specified in minutes', () => { const result = getHumanizedDuration('now-660s', '5m'); expect(result).toEqual('6m'); }); - test('returns from as hours if from duration is more than 60 minutes', () => { - const result = getHumanizedDuration('now-7400s', '5m'); + test('returns from as hours if from duration is specified in hours', () => { + const result = getHumanizedDuration('now-7500s', '5m'); - expect(result).toEqual('1h'); + expect(result).toEqual('2h'); }); test('returns from as if from is not parsable as dateMath', () => { const result = getHumanizedDuration('randomstring', '5m'); - expect(result).toEqual('NaNh'); + expect(result).toEqual('NaNs'); }); test('returns from as 5m if interval is not parsable as dateMath', () => { diff --git a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx index cf35676c8e091..ffcf25d253798 100644 --- a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx +++ b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx @@ -119,12 +119,19 @@ export const getHumanizedDuration = (from: string, interval: string): string => const fromDuration = moment.duration(intervalValue.diff(fromValue)); - if (Number.isInteger(fromDuration.asHours())) { - return `${fromDuration.asHours()}h`; - } else if (Number.isInteger(fromDuration.asMinutes())) { - return `${fromDuration.asMinutes()}m`; + // Basing calculations off floored seconds count as moment durations weren't precise + const intervalDuration = Math.floor(fromDuration.asSeconds()); + // For consistency of display value + if (intervalDuration === 0) { + return `0s`; + } + + if (intervalDuration % 3600 === 0) { + return `${intervalDuration / 3600}h`; + } else if (intervalDuration % 60 === 0) { + return `${intervalDuration / 60}m`; } else { - return `${Math.floor(fromDuration.asSeconds())}s`; + return `${intervalDuration}s`; } }; From 44595aa2f4a2cd5690686ba87ee7d70ef0de5bb3 Mon Sep 17 00:00:00 2001 From: Davis Plumlee Date: Wed, 21 Oct 2020 17:28:15 -0600 Subject: [PATCH 3/3] adds new tests i guess --- .../pages/detection_engine/rules/helpers.test.tsx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx index df1ab47771867..a327f8498f7c0 100644 --- a/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.test.tsx @@ -306,12 +306,24 @@ describe('rule helpers', () => { expect(result).toEqual('2s'); }); + test('returns from as seconds if from duration is specified in seconds greater than 60', () => { + const result = getHumanizedDuration('now-122s', '1m'); + + expect(result).toEqual('62s'); + }); + test('returns from as minutes if from duration is specified in minutes', () => { const result = getHumanizedDuration('now-660s', '5m'); expect(result).toEqual('6m'); }); + test('returns from as minutes if from duration is specified in minutes greater than 60', () => { + const result = getHumanizedDuration('now-6600s', '5m'); + + expect(result).toEqual('105m'); + }); + test('returns from as hours if from duration is specified in hours', () => { const result = getHumanizedDuration('now-7500s', '5m');