Skip to content

Commit

Permalink
Move types to endpoint/types.ts, address PR comments
Browse files Browse the repository at this point in the history
blah 2
  • Loading branch information
peluja1012 committed Jan 28, 2020
1 parent 1063eda commit 49312ef
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 71 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/endpoint/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface EndpointMetadata {
};
}

export type AlertData = Immutable<{
export interface AlertData {
value: {
source: {
endgame: {
Expand All @@ -90,7 +90,7 @@ export type AlertData = Immutable<{
};
};
};
}>;
}

/**
* The PageId type is used for the payload when firing userNavigatedToPage actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { AnyAction, Dispatch, Middleware, MiddlewareAPI } from 'redux';
import { GlobalState } from '../store';
import { GlobalState } from '../types';

interface QueuedAction<TAction = AnyAction> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AlertData } from '../../../../../common/types';
import { AlertData, Immutable } from '../../../../../common/types';

interface ServerReturnedAlertsData {
readonly type: 'serverReturnedAlertsData';

readonly payload: AlertData[];
}
type ServerReturnedAlertsData = Immutable<{
type: 'serverReturnedAlertsData';
payload: AlertData[];
}>;

export type AlertAction = ServerReturnedAlertsData;
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

export { alertListReducer } from './reducer';
export { AlertAction } from './action';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Dispatch, MiddlewareAPI } from 'redux';
import { CoreStart } from 'kibana/public';
import { AlertData } from '../../../../../common/types';
import { GlobalState } from '../reducer';
import { AlertData, ImmutableArray } from '../../../../../common/types';
import { AppAction } from '../action';

// TODO, move this somewhere
type MiddlewareFactory = (
coreStart: CoreStart
) => (
api: MiddlewareAPI<Dispatch<AppAction>, GlobalState>
) => (next: Dispatch<AppAction>) => (action: AppAction) => unknown;
import { MiddlewareFactory } from '../../types';

export const alertMiddlewareFactory: MiddlewareFactory = coreStart => {
return store => next => async (action: AppAction) => {
return api => next => async (action: AppAction) => {
next(action);
if (action.type === 'userNavigatedToPage' && action.payload === 'alertsPage') {
const response: AlertData[] = await coreStart.http.get('/api/endpoint/alerts');
store.dispatch({ type: 'serverReturnedAlertsData', payload: response });
const response: ImmutableArray<AlertData> = await coreStart.http.get('/api/endpoint/alerts');
api.dispatch({ type: 'serverReturnedAlertsData', payload: response });
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Reducer } from 'redux';
import { AlertListState } from './types';
import { AlertListState } from '../../types';
import { AppAction } from '../action';

const initialState = (): AlertListState => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AlertListState } from './types';
import { AlertListState } from '../../types';

export const alertListData = (state: AlertListState) => state.alerts;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { appSagaFactory } from './saga';
import { appReducer } from './reducer';
import { alertMiddlewareFactory } from './alerts/middleware';

export { GlobalState } from './reducer';

const composeWithReduxDevTools = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ name: 'EndpointApp' })
: compose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { combineReducers, Reducer } from 'redux';
import { endpointListReducer, EndpointListState } from './endpoint_list';
import { endpointListReducer } from './endpoint_list';
import { AppAction } from './action';
import { AlertListState, alertListReducer } from './alerts';

export interface GlobalState {
readonly endpointList: EndpointListState;
readonly alertList: AlertListState;
}
import { alertListReducer } from './alerts';
import { GlobalState } from '../types';

export const appReducer: Reducer<GlobalState, AppAction> = combineReducers({
endpointList: endpointListReducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { GlobalState } from './reducer';
import { GlobalState } from '../types';
import * as alertListSelectors from './alerts/selectors';

export const alertListData = composeSelectors(
Expand Down
26 changes: 26 additions & 0 deletions x-pack/plugins/endpoint/public/applications/endpoint/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Dispatch, MiddlewareAPI } from 'redux';
import { CoreStart } from 'kibana/public';
import { Immutable, AlertData } from '../../../common/types';
import { EndpointListState } from './store/endpoint_list';
import { AppAction } from './store/action';

export type MiddlewareFactory = (
coreStart: CoreStart
) => (
api: MiddlewareAPI<Dispatch<AppAction>, GlobalState>
) => (next: Dispatch<AppAction>) => (action: AppAction) => unknown;

export type AlertListState = Immutable<{
alerts: AlertData[];
}>;

export interface GlobalState {
readonly endpointList: EndpointListState;
readonly alertList: AlertListState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,47 @@ import { usePageId } from '../use_page_id';
export const AlertIndex = memo(() => {
usePageId('alertsPage');

const columns: Array<{ id: string }> = [
{ id: 'alert_type' },
{ id: 'event_type' },
{ id: 'os' },
{ id: 'ip_address' },
{ id: 'host_name' },
{ id: 'timestamp' },
{ id: 'archived' },
{ id: 'malware_score' },
];
const columns: Array<{ id: string }> = useMemo(() => {
return [
{ id: 'alert_type' },
{ id: 'event_type' },
{ id: 'os' },
{ id: 'ip_address' },
{ id: 'host_name' },
{ id: 'timestamp' },
{ id: 'archived' },
{ id: 'malware_score' },
];
}, []);

const [visibleColumns, setVisibleColumns] = useState(() => columns.map(({ id }) => id));

const json = useSelector(selectors.alertListData);

const renderCellValue = useMemo(() => {
return ({ rowIndex, columnId }: { rowIndex: number; columnId: string }) => {
if (json.length === 0) {
if (rowIndex > json.length) {
return null;
}

const row = json[rowIndex];

if (columnId === 'alert_type') {
return json[rowIndex].value.source.endgame.metadata.key;
return row.value.source.endgame.metadata.key;
} else if (columnId === 'event_type') {
return json[rowIndex].value.source.endgame.data.file_operation;
return row.value.source.endgame.data.file_operation;
} else if (columnId === 'os') {
return json[rowIndex].value.source.host.os.name;
return row.value.source.host.os.name;
} else if (columnId === 'ip_address') {
return json[rowIndex].value.source.host.ip;
return row.value.source.host.ip;
} else if (columnId === 'host_name') {
return json[rowIndex].value.source.host.hostname;
return row.value.source.host.hostname;
} else if (columnId === 'timestamp') {
return json[rowIndex].value.source.endgame.timestamp_utc;
return row.value.source.endgame.timestamp_utc;
} else if (columnId === 'archived') {
return null; // TODO change this once its available in backend
return null;
} else if (columnId === 'malware_score') {
return json[rowIndex].value.source.endgame.data.malware_classification.score;
return row.value.source.endgame.data.malware_classification.score;
}
return null;
};
Expand Down
5 changes: 0 additions & 5 deletions x-pack/plugins/endpoint/server/routes/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ export function registerAlertRoutes(router: IRouter) {
},
async (context, req, res) => {
try {
// const queryParams = await kibanaRequestToEndpointListQuery(req, endpointAppContext);
// const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
// 'search',
// queryParams
// )) as SearchResponse<AlertData>;
return res.ok({
body: json,
headers: {
Expand Down

0 comments on commit 49312ef

Please sign in to comment.