Skip to content

Commit

Permalink
[Security Solution][Timeline] Remove BrowserField type (elastic#188923)
Browse files Browse the repository at this point in the history
## Summary

This PR removes `BrowserField` type, as it is now replaced with data
plugins' `FieldSpec`.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
lgestc and kibanamachine authored Aug 2, 2024
1 parent 920a40c commit efb71fa
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { i18n } from '@kbn/i18n';
import type { EuiDataGridColumnActions } from '@elastic/eui';
import { keyBy } from 'lodash/fp';
import React from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';
import { BrowserFields } from '@kbn/timelines-plugin/common';

import { BrowserField, BrowserFields } from '@kbn/timelines-plugin/common';
import { DEFAULT_TABLE_COLUMN_MIN_WIDTH, DEFAULT_TABLE_DATE_COLUMN_MIN_WIDTH } from '../constants';
import { defaultColumnHeaderType } from '../../../store/data_table/defaults';
import { ColumnHeaderOptions } from '../../../common/types';
Expand All @@ -25,7 +26,7 @@ export const allowSorting = ({
browserField,
fieldName,
}: {
browserField: Partial<BrowserField> | undefined;
browserField: Partial<FieldSpec> | undefined;
fieldName: string;
}): boolean => {
const isAggregatable = browserField?.aggregatable ?? false;
Expand Down Expand Up @@ -94,8 +95,8 @@ export const allowSorting = ({
return isAllowlistedNonBrowserField || isAggregatable;
};

const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<BrowserField>> =>
Object.values(browserFields).reduce<Array<Partial<BrowserField>>>(
const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<FieldSpec>> =>
Object.values(browserFields).reduce<Array<Partial<FieldSpec>>>(
(acc, namespace) => [
...acc,
...Object.values(namespace.fields != null ? namespace.fields : {}),
Expand All @@ -105,8 +106,7 @@ const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<Browse

const getAllFieldsByName = (
browserFields: BrowserFields
): { [fieldName: string]: Partial<BrowserField> } =>
keyBy('name', getAllBrowserFields(browserFields));
): { [fieldName: string]: Partial<FieldSpec> } => keyBy('name', getAllBrowserFields(browserFields));

/**
* Valid built-in schema types for the `schema` property of `EuiDataGridColumn`
Expand Down Expand Up @@ -204,7 +204,7 @@ export const getColumnHeaders = (
const headersToMap = isEventRenderedView ? eventRenderedViewColumns : headers;
return headersToMap
? headersToMap.map((header) => {
const browserField: Partial<BrowserField> | undefined = browserFieldByName[header.id];
const browserField: Partial<FieldSpec> | undefined = browserFieldByName[header.id];

// augment the header with metadata from browserFields:
const augmentedHeader = {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/packages/security-solution/data_table/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@kbn/i18n-react",
"@kbn/ui-actions-plugin",
"@kbn/data-views-plugin",
"@kbn/field-formats-plugin"
"@kbn/field-formats-plugin",
"@kbn/data-plugin"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export type {
BeatFields,
IndexFieldsStrategyRequest,
IndexFieldsStrategyResponse,
BrowserField,
BrowserFields,
} from '@kbn/timelines-plugin/common';
export { EMPTY_BROWSER_FIELDS, EMPTY_INDEX_FIELDS } from '@kbn/timelines-plugin/common';
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { keyBy } from 'lodash/fp';
import type { DropResult } from '@hello-pangea/dnd';
import type { Dispatch } from 'redux';
import type { ActionCreator } from 'typescript-fsa';
import type { FieldSpec } from '@kbn/data-plugin/common';

import { getFieldIdFromDraggable, getProviderIdFromDraggable } from '@kbn/securitysolution-t-grid';
import { TableId } from '@kbn/securitysolution-data-table';
import { DEFAULT_COLUMN_MIN_WIDTH } from '../../../timelines/components/timeline/body/constants';
import { getScopedActions } from '../../../helpers';
import type { ColumnHeaderOptions } from '../../../../common/types';
import type { BrowserField, BrowserFields } from '../../../../common/search_strategy';
import type { BrowserFields } from '../../../../common/search_strategy';
import { dragAndDropActions } from '../../store/actions';
import type { IdToDataProvider } from '../../store/drag_and_drop/model';
import { addContentToTimeline } from '../../../timelines/components/timeline/data_providers/helpers';
Expand Down Expand Up @@ -184,8 +185,8 @@ export const allowTopN = ({
return isAllowlistedNonBrowserField || (isAggregatable && isAllowedType);
};

const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<BrowserField>> =>
Object.values(browserFields).reduce<Array<Partial<BrowserField>>>(
const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<FieldSpec>> =>
Object.values(browserFields).reduce<Array<Partial<FieldSpec>>>(
(acc, namespace) => [
...acc,
...Object.values(namespace.fields != null ? namespace.fields : {}),
Expand All @@ -195,8 +196,7 @@ const getAllBrowserFields = (browserFields: BrowserFields): Array<Partial<Browse

const getAllFieldsByName = (
browserFields: BrowserFields
): { [fieldName: string]: Partial<BrowserField> } =>
keyBy('name', getAllBrowserFields(browserFields));
): { [fieldName: string]: Partial<FieldSpec> } => keyBy('name', getAllBrowserFields(browserFields));

const linkFields: Record<string, string> = {
'kibana.alert.rule.name': 'kibana.alert.rule.uuid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { Filter } from '@kbn/es-query';
import { escapeDataProviderId } from '@kbn/securitysolution-t-grid';
import { isArray, isEmpty, isString } from 'lodash/fp';
import { useMemo } from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';

import {
AGENT_STATUS_FIELD_NAME,
EVENT_MODULE_FIELD_NAME,
Expand All @@ -29,7 +31,6 @@ import { EVENT_DURATION_FIELD_NAME } from '../../../../timelines/components/dura
import { getDisplayValue } from '../../../../timelines/components/timeline/data_providers/helpers';
import { PORT_NAMES } from '../../../../explore/network/components/port/helpers';
import { INDICATOR_REFERENCE } from '../../../../../common/cti/constants';
import type { BrowserField } from '../../../containers/source';
import type { DataProvider, DataProvidersAnd, QueryOperator } from '../../../../../common/types';
import { IS_OPERATOR } from '../../../../../common/types';

Expand All @@ -38,7 +39,7 @@ export interface UseActionCellDataProvider {
eventId?: string;
field: string;
fieldFormat?: string;
fieldFromBrowserField?: Partial<BrowserField>;
fieldFromBrowserField?: Partial<FieldSpec>;
fieldType?: string;
isObjectArray?: boolean;
linkValue?: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import type { BrowserField } from '../../containers/source';
import type { FieldSpec } from '@kbn/data-plugin/common';
import type { TimelineEventsDetailsItem } from '../../../../common/search_strategy';

export type EventFieldsData = BrowserField & TimelineEventsDetailsItem;
export type EventFieldsData = FieldSpec & TimelineEventsDetailsItem;

export interface FieldsData {
field: string;
Expand All @@ -20,7 +20,7 @@ export interface FieldsData {
export interface EnrichedFieldInfo {
data: FieldsData | EventFieldsData;
eventId: string;
fieldFromBrowserField?: Partial<BrowserField>;
fieldFromBrowserField?: Partial<FieldSpec>;
scopeId: string;
values: string[] | null | undefined;
linkValue?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import { useMemo } from 'react';
import type { DataViewFieldBase } from '@kbn/es-query';
import type { FieldSpec } from '@kbn/data-plugin/common';

import { getTermsAggregationFields } from '../../../../detection_engine/rule_creation_ui/components/step_define_rule/utils';
import { useRuleFields } from '../../../../detection_engine/rule_management/logic/use_rule_fields';
import type { BrowserField } from '../../../containers/source';
import { useMlCapabilities } from './use_ml_capabilities';
import { useMlRuleValidations } from './use_ml_rule_validations';
import { hasMlAdminPermissions } from '../../../../../common/machine_learning/has_ml_admin_permissions';
Expand All @@ -21,7 +21,7 @@ export interface UseMlRuleConfigReturn {
hasMlLicense: boolean;
loading: boolean;
mlFields: DataViewFieldBase[];
mlSuppressionFields: BrowserField[];
mlSuppressionFields: FieldSpec[];
allJobsStarted: boolean;
}

Expand All @@ -46,7 +46,7 @@ export const useMLRuleConfig = ({
machineLearningJobId,
});
const mlSuppressionFields = useMemo(
() => getTermsAggregationFields(mlFields as BrowserField[]),
() => getTermsAggregationFields(mlFields as FieldSpec[]),
[mlFields]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { isEmpty, isEqual, keyBy, pick } from 'lodash/fp';
import memoizeOne from 'memoize-one';
import { useCallback, useEffect, useRef, useState } from 'react';
import type { DataViewBase } from '@kbn/es-query';
import type { BrowserField, BrowserFields } from '@kbn/timelines-plugin/common';
import type { IIndexPatternFieldList } from '@kbn/data-views-plugin/common';
import type { BrowserFields } from '@kbn/timelines-plugin/common';
import type { FieldSpec, IIndexPatternFieldList } from '@kbn/data-views-plugin/common';
import type { DataViewSpec } from '@kbn/data-views-plugin/public';

import { useKibana } from '../../lib/kibana';
Expand All @@ -19,10 +19,10 @@ import { getDataViewStateFromIndexFields } from './use_data_view';
import { useAppToasts } from '../../hooks/use_app_toasts';
import type { ENDPOINT_FIELDS_SEARCH_STRATEGY } from '../../../../common/endpoint/constants';

export type { BrowserField, BrowserFields };
export type { BrowserFields };

export function getAllBrowserFields(browserFields: BrowserFields): Array<Partial<BrowserField>> {
const result: Array<Partial<BrowserField>> = [];
export function getAllBrowserFields(browserFields: BrowserFields): Array<Partial<FieldSpec>> {
const result: Array<Partial<FieldSpec>> = [];
for (const namespace of Object.values(browserFields)) {
if (namespace.fields) {
result.push(...Object.values(namespace.fields));
Expand All @@ -38,8 +38,7 @@ export function getAllBrowserFields(browserFields: BrowserFields): Array<Partial
*/
export const getAllFieldsByName = (
browserFields: BrowserFields
): { [fieldName: string]: Partial<BrowserField> } =>
keyBy('name', getAllBrowserFields(browserFields));
): { [fieldName: string]: Partial<FieldSpec> } => keyBy('name', getAllBrowserFields(browserFields));

export const getIndexFields = memoizeOne(
(title: string, fields: IIndexPatternFieldList): DataViewBase =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { useCallback, useRef } from 'react';
import type { Subscription } from 'rxjs';
import { useDispatch } from 'react-redux';
import memoizeOne from 'memoize-one';
import type { BrowserField, BrowserFields } from '@kbn/timelines-plugin/common';
import type { BrowserFields } from '@kbn/timelines-plugin/common';
import { getCategory } from '@kbn/triggers-actions-ui-plugin/public';
import type { DataViewSpec } from '@kbn/data-views-plugin/public';
import type { FieldCategory } from '@kbn/timelines-plugin/common/search_strategy';

import { useKibana } from '../../lib/kibana';
import { sourcererActions } from '../../../sourcerer/store';
Expand All @@ -28,10 +29,7 @@ export type IndexFieldSearch = (param: {
skipScopeUpdate?: boolean;
}) => Promise<void>;

type DangerCastForBrowserFieldsMutation = Record<
string,
Omit<BrowserField, 'fields'> & { fields: Record<string, BrowserField> }
>;
type DangerCastForBrowserFieldsMutation = Record<string, FieldCategory>;
interface DataViewInfo {
/**
* @deprecated use fields list on dataview / "indexPattern"
Expand Down Expand Up @@ -62,7 +60,7 @@ export const getDataViewStateFromIndexFields = memoizeOne(
}
const categoryFields = browserFields[category].fields;
if (categoryFields) {
categoryFields[name] = field as BrowserField;
categoryFields[name] = field;
}
}
return { browserFields: browserFields as DangerCastForBrowserFieldsMutation };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import React, { memo, useCallback, useState, useEffect, useMemo, useRef } from '
import styled from 'styled-components';
import { i18n as i18nCore } from '@kbn/i18n';
import { isEqual, isEmpty } from 'lodash';
import type { FieldSpec } from '@kbn/data-views-plugin/common';
import type { FieldSpec } from '@kbn/data-plugin/common';
import usePrevious from 'react-use/lib/usePrevious';
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types';
import { useQueryClient } from '@tanstack/react-query';
Expand Down Expand Up @@ -83,7 +83,6 @@ import {
import { EqlQueryBar } from '../eql_query_bar';
import { DataViewSelector } from '../data_view_selector';
import { ThreatMatchInput } from '../threatmatch_input';
import type { BrowserField } from '../../../../common/containers/source';
import { useFetchIndex } from '../../../../common/containers/source';
import { NewTermsFields } from '../new_terms_fields';
import { ScheduleItem } from '../../../rule_creation/components/schedule_item_form';
Expand Down Expand Up @@ -266,23 +265,23 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
[form]
);

const [aggFields, setAggregatableFields] = useState<BrowserField[]>([]);
const [aggFields, setAggregatableFields] = useState<FieldSpec[]>([]);

useEffect(() => {
const { fields } = indexPattern;
/**
* Typecasting to BrowserField because fields is
* Typecasting to FieldSpec because fields is
* typed as DataViewFieldBase[] which does not have
* the 'aggregatable' property, however the type is incorrect
*
* fields does contain elements with the aggregatable property.
* We will need to determine where these types are defined and
* figure out where the discrepency is.
*/
setAggregatableFields(aggregatableFields(fields as BrowserField[]));
setAggregatableFields(aggregatableFields(fields as FieldSpec[]));
}, [indexPattern]);

const termsAggregationFields: BrowserField[] = useMemo(
const termsAggregationFields: FieldSpec[] = useMemo(
() => getTermsAggregationFields(aggFields),
[aggFields]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import type { Type } from '@kbn/securitysolution-io-ts-alerting-types';

import type { BrowserField } from '../../../../common/containers/source';
import type { FieldSpec } from '@kbn/data-plugin/common';

import { CUSTOM_QUERY_REQUIRED, EQL_QUERY_REQUIRED, ESQL_QUERY_REQUIRED } from './translations';

Expand All @@ -19,7 +18,7 @@ import { isEqlRule, isEsqlRule } from '../../../../../common/detection_engine/ut
* Keyword, Numeric, ip, boolean, or binary.
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
*/
export const getTermsAggregationFields = (fields: BrowserField[]): BrowserField[] => {
export const getTermsAggregationFields = (fields: FieldSpec[]): FieldSpec[] => {
// binary types is excluded, as binary field has property aggregatable === false
const allowedTypesSet = new Set(['string', 'number', 'ip', 'boolean']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import type { BrowserField } from '@kbn/timelines-plugin/common';
import type { FieldSpec } from '@kbn/data-plugin/common';

import type { GetAggregatableFields, UseInspectButtonParams } from './hooks';
import { getAggregatableFields, useInspectButton, useStackByFields } from './hooks';
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('getAggregatableFields', () => {

expect(
getAggregatableFields(
{ [field]: mockBrowserFields?.destination?.fields?.[field] as Partial<BrowserField> },
{ [field]: mockBrowserFields?.destination?.fields?.[field] as Partial<FieldSpec> },
useLensCompatibleFields
)
).toHaveLength(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useCallback, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
import type { IFieldSubTypeNested } from '@kbn/es-query';
import type { FieldSpec } from '@kbn/data-plugin/common';

import type { BrowserField } from '@kbn/timelines-plugin/common';
import { i18n } from '@kbn/i18n';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import type { GlobalTimeArgs } from '../../../../common/containers/use_global_time';
Expand Down Expand Up @@ -64,13 +64,13 @@ export const useInspectButton = ({
}, [setQuery, loading, response, request, refetch, uniqueQueryId, deleteQuery, searchSessionId]);
};

export function isDataViewFieldSubtypeNested(field: Partial<BrowserField>) {
export function isDataViewFieldSubtypeNested(field: Partial<FieldSpec>) {
const subTypeNested = field?.subType as IFieldSubTypeNested;
return !!subTypeNested?.nested?.path;
}

export interface GetAggregatableFields {
[fieldName: string]: Partial<BrowserField>;
[fieldName: string]: Partial<FieldSpec>;
}

export function getAggregatableFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import { render, screen } from '@testing-library/react';
import React from 'react';

import type { BrowserField } from '@kbn/timelines-plugin/common';
import type { FieldSpec } from '@kbn/data-plugin/common';
import type { EventFieldsData } from '../../../../common/components/event_details/types';
import { TableFieldValueCell } from './table_field_value_cell';
import { TestProviders } from '../../../../common/mock';
Expand Down Expand Up @@ -88,7 +87,7 @@ describe('TableFieldValueCell', () => {
};
const messageValues = ['Endpoint network event'];

const messageFieldFromBrowserField: BrowserField = {
const messageFieldFromBrowserField: FieldSpec = {
aggregatable: false,
name: 'message',
readFromDocValues: false,
Expand Down Expand Up @@ -121,8 +120,8 @@ describe('TableFieldValueCell', () => {
});
});

describe('when `BrowserField` metadata IS available', () => {
const hostIpFieldFromBrowserField: BrowserField = {
describe('when `FieldSpec` metadata IS available', () => {
const hostIpFieldFromBrowserField: FieldSpec = {
aggregatable: true,
name: 'host.ip',
readFromDocValues: false,
Expand Down
Loading

0 comments on commit efb71fa

Please sign in to comment.