Skip to content

Commit

Permalink
[SIEM] Fix auto save for template timeline (elastic#65001) (elastic#6…
Browse files Browse the repository at this point in the history
…5237)

* update save timeline

* fix types

* allow template timeline to be updated via import

* fix unit tests

* fix for review

* handle update timeline

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
angorayc and elasticmachine authored May 5, 2020
1 parent 2525d57 commit 02ddfda
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 34 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/siem/common/types/timeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export const TimelineTypeLiteralRt = runtimeTypes.union([
runtimeTypes.literal(TimelineType.default),
]);

const TimelineTypeLiteralWithNullRt = unionWithNullType(TimelineTypeLiteralRt);

export type TimelineTypeLiteral = runtimeTypes.TypeOf<typeof TimelineTypeLiteralRt>;
export type TimelineTypeLiteralWithNull = runtimeTypes.TypeOf<typeof TimelineTypeLiteralWithNullRt>;

export const SavedTimelineRuntimeType = runtimeTypes.partial({
columns: unionWithNullType(runtimeTypes.array(SavedColumnHeaderRuntimeType)),
dataProviders: unionWithNullType(runtimeTypes.array(SavedDataProviderRuntimeType)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { KueryFilterQueryKind } from '../../store/model';
import { Note } from '../../lib/note';
import moment from 'moment';
import sinon from 'sinon';
import { TimelineType } from '../../../common/types/timeline';

jest.mock('../../store/inputs/actions');
jest.mock('../../store/timeline/actions');
Expand Down Expand Up @@ -299,6 +300,9 @@ describe('helpers', () => {
sortDirection: 'desc',
},
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
version: '1',
width: 1100,
});
Expand Down Expand Up @@ -393,6 +397,9 @@ describe('helpers', () => {
sortDirection: 'desc',
},
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
version: '1',
width: 1100,
});
Expand Down Expand Up @@ -467,6 +474,9 @@ describe('helpers', () => {
},
loadingEventIds: [],
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
noteIds: [],
pinnedEventIds: {},
pinnedEventsSaveObject: {},
Expand Down Expand Up @@ -632,6 +642,9 @@ describe('helpers', () => {
},
loadingEventIds: [],
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
noteIds: [],
pinnedEventIds: {},
pinnedEventsSaveObject: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import ApolloClient from 'apollo-client';
import { getOr, set, isEmpty } from 'lodash/fp';
import { Action } from 'typescript-fsa';
import uuid from 'uuid';

import { Dispatch } from 'redux';

import { oneTimelineQuery } from '../../containers/timeline/one/index.gql_query';
import { TimelineResult, GetOneTimeline, NoteResult } from '../../graphql/types';
import {
Expand Down Expand Up @@ -169,6 +169,8 @@ export const defaultTimelineToTimelineModel = (
savedObjectId: duplicate ? null : timeline.savedObjectId,
version: duplicate ? null : timeline.version,
title: duplicate ? '' : timeline.title || '',
templateTimelineId: duplicate ? null : timeline.templateTimelineId,
templateTimelineVersion: duplicate ? null : timeline.templateTimelineVersion,
}).reduce((acc: TimelineModel, [key, value]) => (value != null ? set(key, value, acc) : acc), {
...timelineDefaults,
id: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export const oneTimelineQuery = gql`
version
}
title
timelineType
templateTimelineId
templateTimelineVersion
savedQueryId
sort {
columnId
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/siem/public/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5112,6 +5112,12 @@ export namespace GetOneTimeline {

title: Maybe<string>;

timelineType: Maybe<TimelineType>;

templateTimelineId: Maybe<string>;

templateTimelineVersion: Maybe<number>;

savedQueryId: Maybe<string>;

sort: Maybe<Sort>;
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/siem/public/mock/global_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
DEFAULT_INTERVAL_TYPE,
DEFAULT_INTERVAL_VALUE,
} from '../../common/constants';
import { TimelineType } from '../../common/types/timeline';

export const mockGlobalState: State = {
app: {
Expand Down Expand Up @@ -201,6 +202,9 @@ export const mockGlobalState: State = {
kqlQuery: { filterQuery: null, filterQueryDraft: null },
loadingEventIds: [],
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
noteIds: [],
dateRange: {
start: 0,
Expand Down
39 changes: 25 additions & 14 deletions x-pack/plugins/siem/public/mock/timeline_results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { FilterStateStore } from '../../../../../src/plugins/data/common/es_query/filters/meta_filter';

import { TimelineType } from '../../common/types/timeline';

import { OpenTimelineResult } from '../components/open_timeline/types';
import { GetAllTimeline, SortFieldTimeline, TimelineResult, Direction } from '../graphql/types';
import { allTimelinesQuery } from '../containers/timeline/all/index.gql_query';
import { CreateTimelineProps } from '../pages/detection_engine/components/signals/types';
import { TimelineModel } from '../store/timeline/model';
import { timelineDefaults } from '../store/timeline/defaults';
import { FilterStateStore } from '../../../../../src/plugins/data/common/es_query/filters/meta_filter';
export interface MockedProvidedQuery {
request: {
query: GetAllTimeline.Query;
Expand Down Expand Up @@ -168,7 +170,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 1',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -297,7 +299,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 2',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -426,7 +428,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 2',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -555,7 +557,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 3',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -684,7 +686,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 4',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -813,7 +815,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 5',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -942,7 +944,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 6',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1071,7 +1073,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1200,7 +1202,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1329,7 +1331,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1458,7 +1460,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1587,7 +1589,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -1716,7 +1718,7 @@ export const mockOpenTimelineQueryResults: MockedProvidedQuery[] = [
'ZF0W12oB9v5HJNSHwY6L',
],
title: 'test 7',
timelineType: null,
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
created: 1558386787614,
Expand Down Expand Up @@ -2141,6 +2143,9 @@ export const mockTimelineModel: TimelineModel = {
sortDirection: Direction.desc,
},
title: 'Test rule',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
version: '1',
width: 1100,
};
Expand All @@ -2164,6 +2169,9 @@ export const mockTimelineResult: TimelineResult = {
],
kqlMode: 'filter',
title: 'Test rule',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
savedQueryId: null,
sort: { columnId: '@timestamp', sortDirection: 'desc' },
version: '1',
Expand Down Expand Up @@ -2235,6 +2243,9 @@ export const defaultTimelineProps: CreateTimelineProps = {
showRowRenderers: true,
sort: { columnId: '@timestamp', sortDirection: Direction.desc },
title: '',
timelineType: TimelineType.default,
templateTimelineVersion: null,
templateTimelineId: null,
version: null,
width: 1100,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../../../mock/';
import { CreateTimeline, UpdateTimelineLoading } from './types';
import { Ecs } from '../../../../graphql/types';
import { TimelineType } from '../../../../../common/types/timeline';

jest.mock('apollo-client');

Expand Down Expand Up @@ -215,6 +216,9 @@ describe('signals actions', () => {
sortDirection: 'desc',
},
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
version: null,
width: 1100,
},
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/siem/public/store/timeline/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TimelineType } from '../../../common/types/timeline';

import { Direction } from '../../graphql/types';
import { DEFAULT_TIMELINE_WIDTH } from '../../components/timeline/body/constants';
import { defaultHeaders } from '../../components/timeline/body/column_headers/default_headers';
Expand Down Expand Up @@ -33,6 +35,9 @@ export const timelineDefaults: SubsetTimelineModel & Pick<TimelineModel, 'filter
},
loadingEventIds: [],
title: '',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
noteIds: [],
pinnedEventIds: {},
pinnedEventsSaveObject: {},
Expand Down
15 changes: 12 additions & 3 deletions x-pack/plugins/siem/public/store/timeline/epic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TimelineModel } from './model';
import { Filter, esFilters } from '../../../../../../src/plugins/data/public';

import { TimelineType } from '../../../common/types/timeline';

import { Direction } from '../../graphql/types';
import { convertTimelineAsInput } from './epic';

import { Filter, esFilters } from '../../../../../../src/plugins/data/public';
import { TimelineModel } from './model';
import { convertTimelineAsInput } from './epic';

describe('Epic Timeline', () => {
describe('#convertTimelineAsInput ', () => {
Expand Down Expand Up @@ -135,6 +138,9 @@ describe('Epic Timeline', () => {
},
loadingEventIds: [],
title: 'saved',
timelineType: TimelineType.default,
templateTimelineId: null,
templateTimelineVersion: null,
noteIds: [],
pinnedEventIds: {},
pinnedEventsSaveObject: {},
Expand Down Expand Up @@ -283,6 +289,9 @@ describe('Epic Timeline', () => {
columnId: '@timestamp',
sortDirection: 'desc',
},
templateTimelineId: null,
templateTimelineVersion: null,
timelineType: TimelineType.default,
title: 'saved',
});
});
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/siem/public/store/timeline/epic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from 'rxjs/operators';

import { esFilters, Filter, MatchAllFilter } from '../../../../../../src/plugins/data/public';
import { TimelineType } from '../../../common/types/timeline';
import { TimelineInput, ResponseTimeline, TimelineResult } from '../../graphql/types';
import { AppApolloClient } from '../../lib/lib';
import { addError } from '../app/actions';
Expand Down Expand Up @@ -236,6 +237,9 @@ export const createTimelineEpic = <State>(): Epic<
...savedTimeline,
savedObjectId: response.timeline.savedObjectId,
version: response.timeline.version,
timelineType: response.timeline.timelineType ?? TimelineType.default,
templateTimelineId: response.timeline.templateTimelineId ?? null,
templateTimelineVersion: response.timeline.templateTimelineVersion ?? null,
isSaving: false,
},
}),
Expand Down Expand Up @@ -283,6 +287,9 @@ const timelineInput: TimelineInput = {
kqlMode: null,
kqlQuery: null,
title: null,
timelineType: TimelineType.default,
templateTimelineVersion: null,
templateTimelineId: null,
dateRange: null,
savedQueryId: null,
sort: null,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/siem/public/store/timeline/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { getOr, omit, uniq, isEmpty, isEqualWith, union } from 'lodash/fp';

import { Filter } from '../../../../../../src/plugins/data/public';

import { getColumnWidthFromType } from '../../components/timeline/body/column_headers/helpers';
import { Sort } from '../../components/timeline/body/sort';
import {
Expand Down
Loading

0 comments on commit 02ddfda

Please sign in to comment.