Skip to content

Commit

Permalink
Review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 13, 2020
1 parent f27e8e7 commit 3a92165
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('AlertsTableComponent', () => {
it('renders correctly', () => {
const wrapper = shallow(
<AlertsTableComponent
timelineId="test"
timelineId="hosts-page-events"
canUserCRUD
hasIndexWrite
from={0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ import {
UpdateAlertsStatusProps,
} from './types';
import { dispatchUpdateTimeline } from '../../../timelines/components/open_timeline/helpers';
import { TimelineId } from '../../../timelines/containers/local_storage/types';
import {
useStateToaster,
displaySuccessToast,
displayErrorToast,
} from '../../../common/components/toasters';

interface OwnProps {
timelineId: string;
timelineId: TimelineId;
canUserCRUD: boolean;
defaultFilters?: Filter[];
hasIndexWrite: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

export const SINGLE_RULE_ALERTS_TIMELINE_ID = 'alerts-page-single-rule';
export const ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID = 'alerts-rules-details-page';
export const ALERTS_TIMELINE_ID = 'alerts-page';
32 changes: 9 additions & 23 deletions x-pack/plugins/security_solution/public/alerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,26 @@
*/

import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { getTimelineInStorageById } from '../timelines/containers/local_storage';
import { getTimelinesInStorageByIds } from '../timelines/containers/local_storage';
import { TimelineId } from '../timelines/containers/local_storage/types';
import { getAlertsRoutes } from './routes';
import { SecuritySubPlugin } from '../app/types';
import { SINGLE_RULE_ALERTS_TIMELINE_ID, ALERTS_TIMELINE_ID } from './constants';
import { ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID, ALERTS_TIMELINE_ID } from './constants';

const ALERTS_TIMELINE_IDS: TimelineId[] = [SINGLE_RULE_ALERTS_TIMELINE_ID, ALERTS_TIMELINE_ID];
const ALERTS_TIMELINE_IDS: TimelineId[] = [
ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID,
ALERTS_TIMELINE_ID,
];

export class Alerts {
public setup() {}

public start(storage: Storage): SecuritySubPlugin {
const alertsTimelines = ALERTS_TIMELINE_IDS.reduce(
(acc, timelineId) => {
const timelineModel = getTimelineInStorageById(storage, timelineId);
if (!timelineModel) {
return {
...acc,
};
}

return {
...acc,
timelineById: {
...acc.timelineById,
[timelineId]: timelineModel,
},
};
},
{ timelineById: {} }
);
return {
routes: getAlertsRoutes(),
storageTimelines: alertsTimelines,
storageTimelines: {
timelineById: getTimelinesInStorageByIds(storage, ALERTS_TIMELINE_IDS),
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { useMlCapabilities } from '../../../../../common/components/ml_popover/h
import { hasMlAdminPermissions } from '../../../../../../common/machine_learning/has_ml_admin_permissions';
import { ExceptionsViewer } from '../../../../../common/components/exceptions/viewer';
import { ExceptionListType } from '../../../../../common/components/exceptions/types';
import { SINGLE_RULE_ALERTS_TIMELINE_ID } from '../../../../constants';
import { ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID } from '../../../../constants';

enum RuleDetailTabs {
alerts = 'alerts',
Expand Down Expand Up @@ -387,7 +387,7 @@ export const RuleDetailsPageComponent: FC<PropsFromRedux> = ({
<EuiSpacer />
{ruleId != null && (
<AlertsTable
timelineId={SINGLE_RULE_ALERTS_TIMELINE_ID}
timelineId={ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID}
canUserCRUD={canUserCRUD ?? false}
defaultFilters={alertDefaultFilters}
hasIndexWrite={hasIndexWrite ?? false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Filter } from '../../../../../../../src/plugins/data/public';
import { StatefulEventsViewer } from '../events_viewer';
import { alertsDefaultModel } from './default_headers';
import { useManageTimeline } from '../../../timelines/components/manage_timeline';
import { TimelineId } from '../../../timelines/containers/local_storage/types';
import * as i18n from './translations';
export interface OwnProps {
end: number;
Expand Down Expand Up @@ -51,7 +52,7 @@ const defaultAlertsFilters: Filter[] = [
];

interface Props {
timelineId: string;
timelineId: TimelineId;
endDate: number;
startDate: number;
pageFilters?: Filter[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Filter } from '../../../../../../../src/plugins/data/public';
import { HostsComponentsQueryProps } from '../../../hosts/pages/navigation/types';
import { NetworkComponentQueryProps } from '../../../network/pages/navigation/types';
import { TimelineId } from '../../../timelines/containers/local_storage/types';
import { MatrixHistogramOption } from '../matrix_histogram/types';

type CommonQueryProps = HostsComponentsQueryProps | NetworkComponentQueryProps;
Expand All @@ -16,7 +17,7 @@ export interface AlertsComponentsProps
CommonQueryProps,
'deleteQuery' | 'endDate' | 'filterQuery' | 'skip' | 'setQuery' | 'startDate' | 'type'
> {
timelineId: string;
timelineId: TimelineId;
pageFilters: Filter[];
stackByOptions?: MatrixHistogramOption[];
defaultFilters?: Filter[];
Expand Down
26 changes: 4 additions & 22 deletions x-pack/plugins/security_solution/public/hosts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { SecuritySubPluginWithStore } from '../app/types';
import { getTimelineInStorageById } from '../timelines/containers/local_storage';
import { getTimelinesInStorageByIds } from '../timelines/containers/local_storage';
import { TimelineId } from '../timelines/containers/local_storage/types';
import { getHostsRoutes } from './routes';
import { initialHostsState, hostsReducer, HostsState } from './store';
Expand All @@ -18,29 +18,11 @@ export class Hosts {
public setup() {}

public start(storage: Storage): SecuritySubPluginWithStore<'hosts', HostsState> {
const hostTimelines = HOST_TIMELINE_IDS.reduce(
(acc, timelineId) => {
const timelineModel = getTimelineInStorageById(storage, timelineId);
if (!timelineModel) {
return {
...acc,
};
}

return {
...acc,
timelineById: {
...acc.timelineById,
[timelineId]: timelineModel,
},
};
},
{ timelineById: {} }
);

return {
routes: getHostsRoutes(),
storageTimelines: hostTimelines,
storageTimelines: {
timelineById: getTimelinesInStorageByIds(storage, HOST_TIMELINE_IDS),
},
store: {
initialState: { hosts: initialHostsState },
reducer: { hosts: hostsReducer },
Expand Down
9 changes: 4 additions & 5 deletions x-pack/plugins/security_solution/public/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { SecuritySubPluginWithStore } from '../app/types';
import { getNetworkRoutes } from './routes';
import { initialNetworkState, networkReducer, NetworkState } from './store';
import { getTimelineInStorageById } from '../timelines/containers/local_storage';
import { getTimelinesInStorageByIds } from '../timelines/containers/local_storage';
import { NETWORK_PAGE_EXTERNAL_EVENTS_TIMELINE_ID } from './constants';

export class Network {
Expand All @@ -18,10 +18,9 @@ export class Network {
return {
routes: getNetworkRoutes(),
storageTimelines: {
timelineById: {
[NETWORK_PAGE_EXTERNAL_EVENTS_TIMELINE_ID]:
getTimelineInStorageById(storage, NETWORK_PAGE_EXTERNAL_EVENTS_TIMELINE_ID) ?? {},
},
timelineById: getTimelinesInStorageByIds(storage, [
NETWORK_PAGE_EXTERNAL_EVENTS_TIMELINE_ID,
]),
},
store: {
initialState: { network: initialNetworkState },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { timelineQuery } from './index.gql_query';
import { timelineActions } from '../../timelines/store/timeline';
import {
ALERTS_TIMELINE_ID as DETECTION_ENGINE_TIMELINE_ID,
SINGLE_RULE_ALERTS_TIMELINE_ID as DETECTION_ENGINE_RULES_TIMELINE_ID,
ALERTS_RULES_DETAILS_PAGE_TIMELINE_ID as DETECTION_ENGINE_RULES_TIMELINE_ID,
} from '../../alerts/constants';

const timelineIds = [DETECTION_ENGINE_TIMELINE_ID, DETECTION_ENGINE_RULES_TIMELINE_ID];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LOCAL_STORAGE_TIMELINE_KEY, useTimelinesStorage } from '.';
import {
LOCAL_STORAGE_TIMELINE_KEY,
useTimelinesStorage,
getTimelinesInStorageByIds,
getAllTimelinesInStorage,
addTimelineInStorage,
} from '.';
import { mockTimelineModel, createSecuritySolutionStorageMock } from '../../../common/mock';
import { useKibana } from '../../../common/lib/kibana';
import { createUseKibanaMock } from '../../../common/mock/kibana_react';
Expand All @@ -27,45 +33,134 @@ describe('SiemLocalStorage', () => {
localStorage.clear();
});

it('adds a timeline when storage is empty', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
describe('addTimeline', () => {
it('adds a timeline when storage is empty', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
});
});

it('adds a timeline when storage contains another timelines', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
});
});
});

it('adds a timeline when storage contains another timelines', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
describe('getAllTimelines', () => {
it('gets all timelines correctly', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
const timelines = timelineStorage.getAllTimelines();
expect(timelines).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
});
});

it('returns an empty object if there is no timelines', () => {
const timelineStorage = useTimelinesStorage();
const timelines = timelineStorage.getAllTimelines();
expect(timelines).toEqual({});
});
});

it('gets all timelines correctly', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
const timelines = timelineStorage.getAllTimelines();
expect(timelines).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
describe('getTimelineById', () => {
it('gets a timeline by id', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
const timeline = timelineStorage.getTimelineById('hosts-page-events');
expect(timeline).toEqual(mockTimelineModel);
});
});

it('returns null if there is no timelines', () => {
const timelineStorage = useTimelinesStorage();
const timelines = timelineStorage.getAllTimelines();
expect(timelines).toEqual(null);
describe('getTimelinesInStorageByIds', () => {
it('gets timelines correctly', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
const timelines = getTimelinesInStorageByIds(storage, [
'hosts-page-events',
'hosts-page-external-alerts',
]);
expect(timelines).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
});
});

it('gets an empty timelime when there is no timelines', () => {
const timelines = getTimelinesInStorageByIds(storage, ['hosts-page-events']);
expect(timelines).toEqual({});
});

it('returns empty timelime when there is no ids', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
const timelines = getTimelinesInStorageByIds(storage, []);
expect(timelines).toEqual({});
});

it('returns empty timelime when a specific timeline does not exists', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
const timelines = getTimelinesInStorageByIds(storage, ['hosts-page-external-alerts']);
expect(timelines).toEqual({});
});

it('returns timelines correctly when one exist and another not', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
const timelines = getTimelinesInStorageByIds(storage, [
'hosts-page-events',
'hosts-page-external-alerts',
]);
expect(timelines).toEqual({
'hosts-page-events': mockTimelineModel,
});
});
});

describe('getAllTimelinesInStorage', () => {
it('gets timelines correctly', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
timelineStorage.addTimeline('hosts-page-external-alerts', mockTimelineModel);
const timelines = getAllTimelinesInStorage(storage);
expect(timelines).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
});
});

it('gets an empty timeline when there is no timelines', () => {
const timelines = getAllTimelinesInStorage(storage);
expect(timelines).toEqual({});
});
});

it('gets a timeline by id', () => {
const timelineStorage = useTimelinesStorage();
timelineStorage.addTimeline('hosts-page-events', mockTimelineModel);
const timeline = timelineStorage.getTimelineById('hosts-page-events');
expect(timeline).toEqual(mockTimelineModel);
describe('addTimelineInStorage', () => {
it('adds a timeline when storage is empty', () => {
addTimelineInStorage(storage, 'hosts-page-events', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
});
});

it('adds a timeline when storage contains another timelines', () => {
addTimelineInStorage(storage, 'hosts-page-events', mockTimelineModel);
addTimelineInStorage(storage, 'hosts-page-external-alerts', mockTimelineModel);
expect(JSON.parse(localStorage.getItem(LOCAL_STORAGE_TIMELINE_KEY))).toEqual({
'hosts-page-events': mockTimelineModel,
'hosts-page-external-alerts': mockTimelineModel,
});
});
});
});
Loading

0 comments on commit 3a92165

Please sign in to comment.