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 Solutions] Sets our default date time to be "today" instead of "Last 24 hours" to enable cachability and fixes one date math bug in the URL #93548

Merged
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
4 changes: 2 additions & 2 deletions x-pack/plugins/security_solution/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const DEFAULT_SEARCH_AFTER_PAGE_SIZE = 100;
export const DEFAULT_ANOMALY_SCORE = 'securitySolution:defaultAnomalyScore';
export const DEFAULT_MAX_TABLE_QUERY_SIZE = 10000;
export const DEFAULT_SCALE_DATE_FORMAT = 'dateFormat:scaled';
export const DEFAULT_FROM = 'now-24h';
export const DEFAULT_TO = 'now';
export const DEFAULT_FROM = 'now/d';
export const DEFAULT_TO = 'now/d';
export const DEFAULT_INTERVAL_PAUSE = true;
export const DEFAULT_INTERVAL_TYPE = 'manual';
export const DEFAULT_INTERVAL_VALUE = 300000; // ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ describe('QueryBar ', () => {

expect(searchBarProps).toEqual({
dataTestSubj: undefined,
dateRangeFrom: 'now-24h',
dateRangeTo: 'now',
dateRangeFrom: 'now/d',
dateRangeTo: 'now/d',
filters: [],
indexPatterns: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ describe('SIEM Super Date Picker', () => {
expect(store.getState().inputs.global.timerange.kind).toBe('relative');
});

test('Make Sure it is last 24 hours date', () => {
expect(store.getState().inputs.global.timerange.fromStr).toBe('now-24h');
test('Make Sure it is last "now-${x}h" where ${x} is in hours date', () => {
expect(store.getState().inputs.global.timerange.fromStr).toMatch(/^now-[0-9]+h/);
expect(store.getState().inputs.global.timerange.toStr).toBe('now');
});

Expand Down Expand Up @@ -206,7 +206,7 @@ describe('SIEM Super Date Picker', () => {
expect(wrapper.find('div.euiQuickSelectPopover__section').at(1).text()).toBe('Today');
});

test('Today and Last 24 hours are in Recently used date ranges', () => {
test('Today and "Last ${x} hours" where ${x} is in hours are in Recently used date ranges', () => {
wrapper
.find('[data-test-subj="superDatePickerToggleQuickMenuButton"]')
.first()
Expand All @@ -216,8 +216,8 @@ describe('SIEM Super Date Picker', () => {
wrapper.find('button.euiQuickSelect__applyButton').first().simulate('click');
wrapper.update();

expect(wrapper.find('div.euiQuickSelectPopover__section').at(1).text()).toBe(
'Last 24 hoursToday'
expect(wrapper.find('div.euiQuickSelectPopover__section').at(1).text()).toMatch(
/^Last\s[0-9]+\shoursToday/
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ describe('Inputs', () => {
],
timerange: {
from: '2020-07-07T08:20:18.966Z',
fromStr: 'now-24h',
fromStr: 'now/d',
kind: 'relative',
to: '2020-07-08T08:20:18.966Z',
toStr: 'now',
toStr: 'now/d',
},
query: { query: '', language: 'kuery' },
filters: [],
Expand All @@ -293,10 +293,10 @@ describe('Inputs', () => {
queries: [],
timerange: {
from: '2020-07-07T08:20:18.966Z',
fromStr: 'now-24h',
fromStr: 'now/d',
kind: 'relative',
to: '2020-07-08T08:20:18.966Z',
toStr: 'now',
toStr: 'now/d',
},
query: { query: '', language: 'kuery' },
filters: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ describe('getTimeRangeSettings', () => {
expect(to).toBe(new Date(DEFAULT_TO_DATE).toISOString());
});

test('should round up "to" when from and to are both "now/d"', () => {
const mockTo = 'now/d';
const mockFrom = 'now/d';
mockTimeRange({ from: mockFrom, to: mockTo });
const { to } = getTimeRangeSettings();
expect(to).toContain('59:59.999Z');
});

test('should round up "to" when from and to are different date maths', () => {
const mockTo = 'now/d';
const mockFrom = 'now/d+1';
mockTimeRange({ from: mockFrom, to: mockTo });
const { to } = getTimeRangeSettings();
expect(to).toContain('59:59.999Z');
});

test('should return the DEFAULT_TO_DATE when the from value is malformed', () => {
const malformedTimeRange = { from: true };
if (isMalformedTimeRange(malformedTimeRange)) {
Expand Down Expand Up @@ -506,5 +522,10 @@ describe('getIntervalSettings', () => {
const value = parseDateWithDefault('trashed string', moment('1950-05-31T13:03:54.234Z'));
expect(value.toISOString()).toBe(new Date('1950-05-31T13:03:54.234Z').toISOString());
});

test('should round up a valid date string and end with 59:59.999Z', () => {
const value = parseDateWithDefault('now/d', moment('1950-05-31T13:03:54.234Z'), true);
expect(value.toISOString()).toContain('59:59.999Z');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export const getTimeRangeSettings = (uiSettings = true) => {
const fromStr = (isString(timeRange?.from) && timeRange?.from) || DEFAULT_FROM;
const toStr = (isString(timeRange?.to) && timeRange?.to) || DEFAULT_TO;
const from = parseDateWithDefault(fromStr, DEFAULT_FROM_MOMENT).toISOString();
const to = parseDateWithDefault(toStr, DEFAULT_TO_MOMENT).toISOString();

const to = parseDateWithDefault(toStr, DEFAULT_TO_MOMENT, true).toISOString();
return { from, fromStr, to, toStr };
};

Expand All @@ -72,11 +71,18 @@ export const getIntervalSettings = (uiSettings = true): Policy => {
return { kind, duration };
};

/**
* Parses a date and returns the default if the date string is not valid.
* @param dateString The date string to parse
* @param defaultDate The defaultDate if we cannot parse the dateMath
* @returns The moment of the date time parsed
*/
export const parseDateWithDefault = (
dateString: string,
defaultDate: moment.Moment
defaultDate: moment.Moment,
roundUp: boolean = false
): moment.Moment => {
const date = dateMath.parse(dateString);
const date = dateMath.parse(dateString, { roundUp });
if (date != null && date.isValid()) {
return date;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('RecentCases', () => {
wrapper.find(`[data-test-subj="no-cases-create-case"]`).first().simulate('click');
expect(navigateToApp).toHaveBeenCalledWith('securitySolution:case', {
path:
"/create?sourcerer=(default:!('apm-*-transaction*','auditbeat-*','endgame-*','filebeat-*','logs-*','packetbeat-*','winlogbeat-*'))&timerange=(global:(linkTo:!(timeline),timerange:(from:'2020-07-07T08:20:18.966Z',fromStr:now-24h,kind:relative,to:'2020-07-08T08:20:18.966Z',toStr:now)),timeline:(linkTo:!(global),timerange:(from:'2020-07-07T08:20:18.966Z',fromStr:now-24h,kind:relative,to:'2020-07-08T08:20:18.966Z',toStr:now)))",
"/create?sourcerer=(default:!('apm-*-transaction*','auditbeat-*','endgame-*','filebeat-*','logs-*','packetbeat-*','winlogbeat-*'))&timerange=(global:(linkTo:!(timeline),timerange:(from:'2020-07-07T08:20:18.966Z',fromStr:now%2Fd,kind:relative,to:'2020-07-08T08:20:18.966Z',toStr:now%2Fd)),timeline:(linkTo:!(global),timerange:(from:'2020-07-07T08:20:18.966Z',fromStr:now%2Fd,kind:relative,to:'2020-07-08T08:20:18.966Z',toStr:now%2Fd)))",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ describe('Timeline QueryBar ', () => {
);
const queryBarProps = wrapper.find(QueryBar).props();

expect(queryBarProps.dateRangeFrom).toEqual('now-24h');
expect(queryBarProps.dateRangeTo).toEqual('now');
expect(queryBarProps.dateRangeFrom).toEqual('now/d');
expect(queryBarProps.dateRangeTo).toEqual('now/d');
expect(queryBarProps.filterQuery).toEqual({ query: 'here: query', language: 'kuery' });
expect(queryBarProps.savedQuery).toEqual(undefined);
expect(queryBarProps.filters).toHaveLength(1);
Expand Down