Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution][Detections] Look-back time logic fix #81383

Merged
merged 3 commits into from
Oct 22, 2020
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 @@ -300,28 +300,40 @@ describe('rule helpers', () => {
});

marshallmain marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stopped using the moment duration in these calculations because they weren't consistent/precise and made the tests flaky.

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