Skip to content

Commit

Permalink
Merge branch 'main' into graphA11yTests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavyarm authored Jun 20, 2022
2 parents 92de90d + 3d5a1a7 commit e0c7b7e
Show file tree
Hide file tree
Showing 24 changed files with 439 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@kbn/es-query';

const INDEX_NAME = 'my-index';
const MOCKED_INDEX = { id: INDEX_NAME } as unknown as DataViewBase;
const EXISTS_FIELD_NAME = '_exists_';
const FIELD = {
name: 'my-field',
Expand All @@ -47,7 +48,7 @@ describe('Generate filters', () => {
EXISTS_FIELD_NAME,
FIELD.name,
'',
INDEX_NAME
MOCKED_INDEX
);
expect(filters).toHaveLength(1);
expect(filters[0].meta.index === INDEX_NAME);
Expand All @@ -61,7 +62,7 @@ describe('Generate filters', () => {
EXISTS_FIELD_NAME,
FIELD.name,
'-',
INDEX_NAME
MOCKED_INDEX
);
expect(filters).toHaveLength(1);
expect(filters[0].meta.index === INDEX_NAME);
Expand All @@ -74,7 +75,7 @@ describe('Generate filters', () => {
filter.meta.disabled = true;
filtersArray.push(filter);

const filters = generateFilters(mockFilterManager, '_exists_', FIELD.name, '-', INDEX_NAME);
const filters = generateFilters(mockFilterManager, '_exists_', FIELD.name, '-', MOCKED_INDEX);
expect(filters).toHaveLength(1);
expect(filters[0].meta.index === INDEX_NAME);
expect(filters[0].meta.negate).toBeTruthy();
Expand All @@ -83,7 +84,7 @@ describe('Generate filters', () => {
});

it('should create phrase filter', () => {
const filters = generateFilters(mockFilterManager, FIELD, PHRASE_VALUE, '', INDEX_NAME);
const filters = generateFilters(mockFilterManager, FIELD, PHRASE_VALUE, '', MOCKED_INDEX);
expect(filters).toHaveLength(1);

const [filter] = filters as PhraseFilter[];
Expand All @@ -96,7 +97,7 @@ describe('Generate filters', () => {
});

it('should create negated phrase filter', () => {
const filters = generateFilters(mockFilterManager, FIELD, PHRASE_VALUE, '-', INDEX_NAME);
const filters = generateFilters(mockFilterManager, FIELD, PHRASE_VALUE, '-', MOCKED_INDEX);
expect(filters).toHaveLength(1);
const [filter] = filters as PhraseFilter[];
expect(filter.meta.index === INDEX_NAME);
Expand All @@ -119,7 +120,7 @@ describe('Generate filters', () => {
lte: '192.168.255.255',
},
'+',
INDEX_NAME
MOCKED_INDEX
) as RangeFilter[];
expect(filters).toHaveLength(1);
const [filter] = filters;
Expand All @@ -143,7 +144,7 @@ describe('Generate filters', () => {
} as DataViewFieldBase,
10000,
'+',
INDEX_NAME
MOCKED_INDEX
);

expect(filters).toHaveLength(1);
Expand All @@ -164,7 +165,7 @@ describe('Generate filters', () => {
FIELD,
[PHRASE_VALUE, ANOTHER_PHRASE],
'',
INDEX_NAME
MOCKED_INDEX
);
expect(filters).toHaveLength(2);
expect(filters[0].meta.index === INDEX_NAME);
Expand All @@ -188,7 +189,7 @@ describe('Generate filters', () => {
FIELD,
[PHRASE_VALUE, ANOTHER_PHRASE, PHRASE_VALUE, ANOTHER_PHRASE],
'',
INDEX_NAME
MOCKED_INDEX
);
expect(filters).toHaveLength(2);
expect(filters[0].query?.match_phrase).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
FilterStateStore,
FILTERS,
DataViewFieldBase,
DataViewBase,
} from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/common';

import { FilterManager } from '../filter_manager';

Expand Down Expand Up @@ -72,7 +72,7 @@ export function generateFilters(
field: DataViewFieldBase | string,
values: any,
operation: string,
index: string
index: DataViewBase
): Filter[] {
values = Array.isArray(values) ? _.uniq(values) : [values];
const fieldObj = (
Expand All @@ -96,12 +96,8 @@ export function generateFilters(
updateExistingFilter(existing, negate);
filter = existing;
} else if (fieldObj.type?.includes('range') && value && typeof value === 'object') {
// When dealing with range fields, the filter type depends on the data passed in. If it's an
// object we assume that it's a min/max value
const tmpIndexPattern = { id: index } as DataView;

filter = buildFilter(
tmpIndexPattern,
index,
fieldObj,
FILTERS.RANGE_FROM_VALUE,
false,
Expand All @@ -111,7 +107,6 @@ export function generateFilters(
FilterStateStore.APP_STATE
);
} else {
const tmpIndexPattern = { id: index } as DataView;
// exists filter special case: fieldname = '_exists' and value = fieldname
const filterType = fieldName === '_exists_' ? FILTERS.EXISTS : FILTERS.PHRASE;
const actualFieldObj =
Expand All @@ -121,7 +116,7 @@ export function generateFilters(
const isNullFilter = value === null || value === undefined;

filter = buildFilter(
tmpIndexPattern,
index,
actualFieldObj,
isNullFilter ? FILTERS.EXISTS : filterType,
isNullFilter ? !negate : negate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const ContextApp = ({ indexPattern, anchorId }: ContextAppProps) => {

const addFilter = useCallback(
async (field: DataViewField | string, values: unknown, operation: string) => {
const newFilters = generateFilters(filterManager, field, values, operation, indexPattern.id!);
const newFilters = generateFilters(filterManager, field, values, operation, indexPattern);
filterManager.addFilters(newFilters);
if (indexPatterns) {
const fieldName = typeof field === 'string' ? field : field.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,7 @@ export function DiscoverLayout({
(field: DataViewField | string, values: string, operation: '+' | '-') => {
const fieldName = typeof field === 'string' ? field : field.name;
popularizeField(indexPattern, fieldName, indexPatterns, capabilities);
const newFilters = generateFilters(
filterManager,
field,
values,
operation,
String(indexPattern.id)
);
const newFilters = generateFilters(filterManager, field, values, operation, indexPattern);
if (trackUiMetric) {
trackUiMetric(METRIC_TYPE.CLICK, 'filter_added');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class SavedSearchEmbeddable
field,
value,
operator,
indexPattern.id!
indexPattern
);
filters = filters.map((filter) => ({
...filter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const FIELDS_WITHOUT_KEYWORD_MAPPING = new Set([
'@timestamp',
'resource.sub_type',
'resource.name',
'resource.id',
'rule.name',
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const getResourceFindingsQuery = ({
...query,
bool: {
...query?.bool,
filter: [...(query?.bool?.filter || []), { term: { 'resource_id.keyword': resourceId } }],
filter: [...(query?.bool?.filter || []), { term: { 'resource.id': resourceId } }],
},
},
pit: { id: pitId },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const getExpandColumn = <T extends unknown>({

export const getFindingsColumns = (): Array<EuiBasicTableColumn<CspFinding>> => [
{
field: 'resource_id',
field: 'resource.id',
name: (
<ColumnNameWithTooltip
columnName={TEXT.RESOURCE_ID}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ export const latestFindingsMapping: MappingTypeMapping = {
id: {
type: 'keyword',
ignore_above: 1024,
fields: {
text: {
type: 'text',
},
},
},
name: {
type: 'keyword',
Expand All @@ -85,16 +80,6 @@ export const latestFindingsMapping: MappingTypeMapping = {
},
},
},
resource_id: {
// deprecated - the new field is resource.id
type: 'text',
fields: {
keyword: {
ignore_above: 1024,
type: 'keyword',
},
},
},
rule: {
properties: {
name: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const IndexDataVisualizerView: FC<IndexDataVisualizerViewProps> = (dataVi
field,
values,
operation,
String(currentDataView.id)
currentDataView
);
if (newFilters) {
data.query.filterManager.addFilters(newFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ describe('EngineCreationLogic', () => {
jest.clearAllMocks();
});

it('GETs to /internal/enterprise_search/indices', () => {
it('GETs to /internal/enterprise_search/search_indices', () => {
EngineCreationLogic.actions.loadIndices();
expect(http.get).toHaveBeenCalledWith('/internal/enterprise_search/indices');
expect(http.get).toHaveBeenCalledWith('/internal/enterprise_search/search_indices');
});

it('calls onLoadIndicesSuccess with payload on load is successful', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const EngineCreationLogic = kea<MakeLogicType<EngineCreationValues, Engin
loadIndices: async () => {
const { http } = HttpLogic.values;
try {
const indices = await http.get('/internal/enterprise_search/indices');
const indices = await http.get('/internal/enterprise_search/search_indices');
actions.onLoadIndicesSuccess(indices as ElasticsearchIndex[]);
} catch (e) {
flashAPIErrors(e);
Expand Down
16 changes: 12 additions & 4 deletions x-pack/plugins/enterprise_search/server/lib/fetch_indices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ describe('fetchIndices lib function', () => {
mockClient.asCurrentUser.indices.get.mockImplementation(() => regularIndexResponse);
mockClient.asCurrentUser.indices.stats.mockImplementation(() => regularIndexStatsResponse);

await expect(fetchIndices(mockClient as unknown as IScopedClusterClient)).resolves.toEqual([
await expect(
fetchIndices(mockClient as unknown as IScopedClusterClient, 'search-*', /search-.*/)
).resolves.toEqual([
{
health: 'green',
status: 'open',
Expand Down Expand Up @@ -104,7 +106,9 @@ describe('fetchIndices lib function', () => {

mockClient.asCurrentUser.indices.get.mockImplementationOnce(() => aliasedIndexResponse);
mockClient.asCurrentUser.indices.stats.mockImplementationOnce(() => aliasedStatsResponse);
await expect(fetchIndices(mockClient as unknown as IScopedClusterClient)).resolves.toEqual([
await expect(
fetchIndices(mockClient as unknown as IScopedClusterClient, 'search-*', /search-.*/)
).resolves.toEqual([
{
health: 'green',
status: 'open',
Expand Down Expand Up @@ -149,7 +153,9 @@ describe('fetchIndices lib function', () => {
mockClient.asCurrentUser.indices.stats.mockImplementationOnce(() => missingStatsResponse);
// simulates when an index has been deleted after get indices call
// deleted index won't be present in the indices stats call response
await expect(fetchIndices(mockClient as unknown as IScopedClusterClient)).resolves.toEqual([
await expect(
fetchIndices(mockClient as unknown as IScopedClusterClient, 'search-*', /search-.*/)
).resolves.toEqual([
{
name: 'search-regular-index',
total: {
Expand All @@ -170,7 +176,9 @@ describe('fetchIndices lib function', () => {

it('should return empty array when no index found', async () => {
mockClient.asCurrentUser.indices.get.mockImplementationOnce(() => ({}));
await expect(fetchIndices(mockClient as unknown as IScopedClusterClient)).resolves.toEqual([]);
await expect(
fetchIndices(mockClient as unknown as IScopedClusterClient, 'search-*', /search-.*/)
).resolves.toEqual([]);
expect(mockClient.asCurrentUser.indices.stats).not.toHaveBeenCalled();
});
});
15 changes: 8 additions & 7 deletions x-pack/plugins/enterprise_search/server/lib/fetch_indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { IScopedClusterClient } from '@kbn/core/server';

import { ElasticsearchIndex } from '../../common/types';

export const fetchIndices = async (client: IScopedClusterClient): Promise<ElasticsearchIndex[]> => {
const indexNamesString = 'search-*';
const indexNamesRegEx = /^search-*/;

export const fetchIndices = async (
client: IScopedClusterClient,
indexPattern: string,
indexRegExp: RegExp
): Promise<ElasticsearchIndex[]> => {
// This call retrieves alias and settings information about indices
const indices = await client.asCurrentUser.indices.get({
index: indexNamesString,
index: indexPattern,
expand_wildcards: ['open'],
// only get specified index properties from ES to keep the response under 536MB
// node.js string length limit: https://github.com/nodejs/node/issues/33960
Expand All @@ -30,7 +31,7 @@ export const fetchIndices = async (client: IScopedClusterClient): Promise<Elasti
}

const { indices: indicesStats = {} } = await client.asCurrentUser.indices.stats({
index: indexNamesString,
index: indexPattern,
expand_wildcards: ['open'],
metric: ['docs', 'store'],
});
Expand Down Expand Up @@ -73,5 +74,5 @@ export const fetchIndices = async (client: IScopedClusterClient): Promise<Elasti
});
return engines;
})
.filter(({ name }) => name.match(indexNamesRegEx));
.filter(({ name }) => name.match(indexRegExp));
};
4 changes: 2 additions & 2 deletions x-pack/plugins/enterprise_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {

import { registerAppSearchRoutes } from './routes/app_search';
import { registerConfigDataRoute } from './routes/enterprise_search/config_data';
import { registerListRoute } from './routes/enterprise_search/indices';
import { registerIndexRoutes } from './routes/enterprise_search/indices';
import { registerTelemetryRoute } from './routes/enterprise_search/telemetry';
import { registerWorkplaceSearchRoutes } from './routes/workplace_search';

Expand Down Expand Up @@ -160,7 +160,7 @@ export class EnterpriseSearchPlugin implements Plugin {
registerConfigDataRoute(dependencies);
registerAppSearchRoutes(dependencies);
registerWorkplaceSearchRoutes(dependencies);
registerListRoute(dependencies);
registerIndexRoutes(dependencies);

/**
* Bootstrap the routes, saved objects, and collector for telemetry
Expand Down
Loading

0 comments on commit e0c7b7e

Please sign in to comment.