Skip to content

Commit

Permalink
[Security Solution][Detections] Look-back time logic fix (#81383)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee authored Oct 22, 2020
1 parent b2fc892 commit bd94486
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit bd94486

Please sign in to comment.