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 solution] add type and value to index patterns passed to unified search #146245

Closed
wants to merge 8 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isEmpty, isEqual, keyBy, pick } from 'lodash/fp';
import memoizeOne from 'memoize-one';
import { useCallback, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import type { DataViewBase } from '@kbn/es-query';
import { Subscription } from 'rxjs';

import type {
Expand All @@ -24,6 +23,8 @@ import { useKibana } from '../../lib/kibana';
import * as i18n from './translations';
import { useAppToasts } from '../../hooks/use_app_toasts';
import { getDataViewStateFromIndexFields } from './use_data_view';
import type { KibanaDataViewBase } from '../../types';
import { DataViewType } from '../../types';

export type { BrowserField, BrowserFields };

Expand All @@ -43,16 +44,13 @@ export const getAllFieldsByName = (
keyBy('name', getAllBrowserFields(browserFields));

export const getIndexFields = memoizeOne(
(title: string, fields: IndexField[]): DataViewBase =>
(fields: IndexField[]): KibanaDataViewBase['fields'] =>
fields && fields.length > 0
? {
fields: fields.map((field) =>
pick(['name', 'searchable', 'type', 'aggregatable', 'esTypes', 'subType'], field)
),
title,
}
: { fields: [], title },
(newArgs, lastArgs) => newArgs[0] === lastArgs[0] && newArgs[1].length === lastArgs[1].length
? fields.map((field) =>
pick(['name', 'searchable', 'type', 'aggregatable', 'esTypes', 'subType'], field)
)
: [],
(newArgs, lastArgs) => newArgs.length === lastArgs.length
);

/**
Expand Down Expand Up @@ -84,12 +82,12 @@ export const getBrowserFields = memoizeOne(
);

const DEFAULT_BROWSER_FIELDS = {};
const DEFAULT_INDEX_PATTERNS = { fields: [], title: '' };
const DEFAULT_INDEX_PATTERNS = { fields: [], title: '', type: DataViewType.title, value: '' };
interface FetchIndexReturn {
browserFields: BrowserFields;
indexes: string[];
indexExists: boolean;
indexPatterns: DataViewBase;
indexPatterns: KibanaDataViewBase;
}

/**
Expand Down Expand Up @@ -144,7 +142,13 @@ export const useFetchIndex = (
browserFields,
indexes: response.indicesExist,
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
indexPatterns: {
fields: getIndexFields(response.indexFields),
title: stringifyIndices,
// need these properties for unified search
type: DataViewType.title,
value: stringifyIndices,
},
});

searchSubscription$.current.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useDispatch } from 'react-redux';
import { i18n } from '@kbn/i18n';
import { matchPath } from 'react-router-dom';
import { DataViewType } from '../../types';
import { sourcererActions, sourcererSelectors } from '../../store/sourcerer';
import type {
SelectedDataView,
Expand Down Expand Up @@ -426,6 +427,16 @@ export const useSourcererDataView = (
indexPattern: {
fields: sourcererDataView.indexFields,
title: selectedPatterns.join(','),
// need these properties for unified search
...(sourcererDataView.id != null
? {
type: DataViewType.id,
value: sourcererDataView.id,
}
: {
type: DataViewType.title,
value: selectedPatterns.join(','),
}),
},
indicesExist,
loading: loading || sourcererDataView.loading,
Expand Down
13 changes: 12 additions & 1 deletion x-pack/plugins/security_solution/public/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ export interface SecuritySolutionUiConfigType {
enableExperimental: string[];
}

export enum DataViewType {
id = 'id',
title = 'title',
}

export interface KibanaDataViewBase extends DataViewBase {
// need these properties for unified search
type: DataViewType;
value: string;
}

/**
* DataViewBase with enhanced index fields used in timelines
*/
export interface SecuritySolutionDataViewBase extends DataViewBase {
export interface SecuritySolutionDataViewBase extends KibanaDataViewBase {
fields: FieldSpec[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ jest.mock('../../../../common/containers/sourcerer', () => {
const actual = jest.requireActual('../../../../common/containers/sourcerer');
return {
...actual,
useSourcererDataView: jest
.fn()
.mockReturnValue({ indexPattern: ['fakeindex'], loading: false }),
useSourcererDataView: jest.fn().mockReturnValue({
indexPattern: { fields: [], title: '', type: 'title', value: '' },
loading: false,
}),
};
});
jest.mock('react-router-dom', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { render } from '@testing-library/react';
import { DetectionResponse } from './detection_response';
import { TestProviders } from '../../common/mock';
import { noCasesPermissions, readCasesPermissions } from '../../cases_test_utils';
import { DataViewType } from '../../common/types';

jest.mock('../components/detection_response/alerts_by_status', () => ({
AlertsByStatus: () => <div data-test-subj="mock_AlertsByStatus" />,
Expand Down Expand Up @@ -43,7 +44,7 @@ jest.mock('../../common/components/search_bar', () => ({
const defaultUseSourcererReturn = {
indicesExist: true,
loading: false,
indexPattern: '',
indexPattern: { fields: [], title: '', type: DataViewType.title, value: '' },
};
const mockUseSourcererDataView = jest.fn(() => defaultUseSourcererReturn);
jest.mock('../../common/containers/sourcerer', () => ({
Expand Down
39 changes: 26 additions & 13 deletions x-pack/plugins/timelines/public/container/source/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ import {
} from '../../../common/search_strategy';
import { useAppToasts } from '../../hooks/use_app_toasts';

export enum DataViewType {
id = 'id',
title = 'title',
}

export interface SecuritySolutionDataViewBase extends DataViewBase {
type: DataViewType;
value: string;
}

const DEFAULT_BROWSER_FIELDS = {};
const DEFAULT_INDEX_PATTERNS = { fields: [], title: '' };
const DEFAULT_INDEX_PATTERNS = { fields: [], title: '', type: DataViewType.title, value: '' };
interface FetchIndexReturn {
browserFields: BrowserFields;
indexes: string[];
indexExists: boolean;
indexPatterns: DataViewBase;
indexPatterns: SecuritySolutionDataViewBase;
}

/**
Expand Down Expand Up @@ -64,17 +74,14 @@ export const getBrowserFields = memoizeOne(
(newArgs, lastArgs) => newArgs[0] === lastArgs[0]
);

export const getIndexFields = memoizeOne(
(title: string, fields: IndexField[]): DataViewBase =>
const getIndexFields = memoizeOne(
(fields: IndexField[]): DataViewBase['fields'] =>
fields && fields.length > 0
? {
fields: fields.map((field) =>
pick(['name', 'searchable', 'type', 'aggregatable', 'esTypes', 'subType'], field)
),
title,
}
: { fields: [], title },
(newArgs, lastArgs) => newArgs[0] === lastArgs[0] && newArgs[1].length === lastArgs[1].length
? fields.map((field) =>
pick(['name', 'searchable', 'type', 'aggregatable', 'esTypes', 'subType'], field)
)
: [],
(newArgs, lastArgs) => newArgs.length === lastArgs.length
);

export const useFetchIndex = (
Expand Down Expand Up @@ -120,7 +127,13 @@ export const useFetchIndex = (
browserFields: getBrowserFields(stringifyIndices, response.indexFields),
indexes: response.indicesExist,
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
indexPatterns: {
fields: getIndexFields(response.indexFields),
title: stringifyIndices,
// need these properties for unified search
type: DataViewType.title,
value: stringifyIndices,
},
});

searchSubscription$.current.unsubscribe();
Expand Down