Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
peluja1012 committed Mar 12, 2020
1 parent 7a8ab62 commit c76d52e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
6 changes: 6 additions & 0 deletions x-pack/plugins/endpoint/public/applications/endpoint/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ type DataMock = Omit<DataPublicStartMock, 'indexPatterns' | 'query'> & {
};
};

/**
* Type for our app's depsStart (plugin start dependencies)
*/
export interface DepsStartMock {
data: DataMock;
}

/**
* Returns a mock of our app's depsStart (plugin start dependencies)
*/
export const depsStartMock: () => DepsStartMock = () => {
const dataMock: DataMock = (dataPluginMock.createStartContract() as unknown) as DataMock;
dataMock.indexPatterns.getFieldsForWildcard = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { EndpointAppConstants } from '../../../../../common/types';
export const alertMiddlewareFactory: MiddlewareFactory<AlertListState> = (coreStart, depsStart) => {
async function fetchIndexPatterns(): Promise<IIndexPattern[]> {
const { indexPatterns } = depsStart.data;
// TODO: what's the best way to get the index pattern?
// const pattern = await indexPatterns.get('287e7b60-4394-11ea-aaac-c76376668d76');
const indexName = EndpointAppConstants.ALERT_INDEX_NAME;
const fields = await indexPatterns.getFieldsForWildcard({ pattern: indexName });
const indexPattern: IIndexPattern = {
Expand All @@ -31,7 +29,6 @@ export const alertMiddlewareFactory: MiddlewareFactory<AlertListState> = (coreSt
next(action);
const state = api.getState();
if (action.type === 'userChangedUrl' && isOnAlertPage(state)) {
// TODO: only fetch index pattern once when the page loads
const patterns = await fetchIndexPatterns();
api.dispatch({ type: 'serverReturnedSearchBarIndexPatterns', payload: patterns });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export const uiQueryParams: (
}
);

/**
* Parses the ui query params and returns a object that represents the query used by the SearchBar component.
* If the query url param is undefined, a default is returned.
*/
export const searchBarQuery: (state: AlertListState) => Query = createSelector(
uiQueryParams,
({ query }) => {
Expand All @@ -89,24 +93,35 @@ export const searchBarQuery: (state: AlertListState) => Query = createSelector(
}
);

/**
* Parses the ui query params and returns a rison encoded string that represents the search bar's date range.
* A default is provided if 'date_range' is not present in the url params.
*/
export const encodedSearchBarDateRange: (state: AlertListState) => string = createSelector(
uiQueryParams,
({ date_range: dateRange }) => {
if (dateRange === undefined) {
return encode({ from: 'now-15m', to: 'now' });
return encode({ from: 'now-24h', to: 'now' });
} else {
return dateRange;
}
}
);

/**
* Parses the ui query params and returns a object that represents the dateRange used by the SearchBar component.
*/
export const searchBarDateRange: (state: AlertListState) => TimeRange = createSelector(
encodedSearchBarDateRange,
encodedDateRange => {
return (decode(encodedDateRange) as unknown) as TimeRange;
}
);

/**
* Parses the ui query params and returns an array of filters used by the SearchBar component.
* If the 'filters' param is not present, a default is returned.
*/
export const searchBarFilters: (state: AlertListState) => Filter[] = createSelector(
uiQueryParams,
({ filters }) => {
Expand All @@ -118,6 +133,11 @@ export const searchBarFilters: (state: AlertListState) => Filter[] = createSelec
}
);

/**
* Returns the indexPatterns used by the SearchBar component
*/
export const searchBarIndexPatterns = (state: AlertListState) => state.searchBar.patterns;

/**
* query params to use when requesting alert data.
*/
Expand Down Expand Up @@ -157,5 +177,3 @@ export const selectedAlertIsLegacyEndpointEvent: (
}
return 'endgame' in event;
});

export const searchBarIndexPatterns = (state: AlertListState) => state.searchBar.patterns;
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ export const substateMiddlewareFactory = <Substate>(
};
};

/**
* @param middlewareDeps Optionally create the store without any middleware. This is useful for testing the store w/o side effects.
*/
export const appStoreFactory: (middlewareDeps?: {
/**
* Allow middleware to communicate with Kibana core.
*/
coreStart: CoreStart;
/**
* Give middleware access to plugin start dependencies.
*/
depsStart: EndpointPluginStartDependencies;
}) => Store = middlewareDeps => {
let middleware;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export interface AlertListState {
/** Specific Alert data to be shown in the details view */
readonly alertDetails?: Immutable<AlertData>;

/** Search bar filters, query, dateRange, and index */
/** Search bar state including indexPatterns */
readonly searchBar: AlertsSearchBarState;
}

Expand Down

0 comments on commit c76d52e

Please sign in to comment.