From eb05a569d09796e79e2e8565fa71c0543b5674d1 Mon Sep 17 00:00:00 2001 From: Davis Plumlee <56367316+dplumlee@users.noreply.github.com> Date: Wed, 21 Oct 2020 21:03:38 -0600 Subject: [PATCH] [Security Solution][Detections] Look-back time logic fix (#81383) (#81426) --- .../detection_engine/rules/helpers.test.tsx | 24 ++++++++++++++----- .../pages/detection_engine/rules/helpers.tsx | 18 +++++++++----- 2 files changed, 30 insertions(+), 12 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 0cd75506fa9f50..a327f8498f7c0b 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,40 @@ 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 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 hours if from duration is more than 60 minutes', () => { - const result = getHumanizedDuration('now-7400s', '5m'); + 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'); - 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 4dbcffbc807ec1..ffcf25d2537985 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,21 @@ 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) { - return `${Math.floor(fromDuration.asSeconds())}s`; - } else if (fromDuration.asMinutes() < 60) { - return `${Math.floor(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`; } - return fromHumanize; + if (intervalDuration % 3600 === 0) { + return `${intervalDuration / 3600}h`; + } else if (intervalDuration % 60 === 0) { + return `${intervalDuration / 60}m`; + } else { + return `${intervalDuration}s`; + } }; export const getAboutStepsData = (rule: Rule, detailsView: boolean): AboutStepRule => {