Skip to content

Commit

Permalink
[Security Solution][Timeline] Remove BrowserField type
Browse files Browse the repository at this point in the history
  • Loading branch information
lgestc committed Jul 25, 2024
1 parent a8e19ee commit 915ede1
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 79 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
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 @@ -10,11 +10,11 @@ import memoizeOne from 'memoize-one';
import React from 'react';
import styled from 'styled-components';
import { getCategory } from '@kbn/triggers-actions-ui-plugin/public';
import type { FieldSpec } from '@kbn/data-plugin/common';
import { SecurityCellActions, CellActionsMode, SecurityCellActionsTrigger } from '../cell_actions';
import type { BrowserFields } from '../../containers/source';
import * as i18n from './translations';
import type { EventFieldsData } from './types';
import type { BrowserField } from '../../../../common/search_strategy';
import { FieldValueCell } from './table/field_value_cell';
import { FieldNameCell } from './table/field_name_cell';
import { getSourcererScopeId } from '../../../helpers';
Expand All @@ -35,10 +35,10 @@ const HoverActionsContainer = styled(EuiPanel)`
HoverActionsContainer.displayName = 'HoverActionsContainer';

export const getFieldFromBrowserField = memoizeOne(
(field: string, browserFields: BrowserFields): BrowserField | undefined => {
(field: string, browserFields: BrowserFields): Partial<FieldSpec> | undefined => {
const category = getCategory(field);

return browserFields[category]?.fields?.[field] as BrowserField;
return browserFields[category]?.fields?.[field];
},
(newArgs, lastArgs) => newArgs[0] === lastArgs[0]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import { render, screen } from '@testing-library/react';
import React from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';

import type { BrowserField } from '../../../containers/source';
import { FieldValueCell } from './field_value_cell';
import { TestProviders } from '../../../mock';
import type { EventFieldsData } from '../types';
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('FieldValueCell', () => {
};
const messageValues = ['Endpoint network event'];

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import type { CSSProperties } from 'react';
import React from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';
import { EuiFlexGroup, EuiFlexItem, EuiText } from '@elastic/eui';
import type { BrowserField } from '../../../containers/source';

import { OverflowField } from '../../tables/helpers';
import { FormattedFieldValue } from '../../../../timelines/components/timeline/body/renderers/formatted_field';
import { MESSAGE_FIELD_NAME } from '../../../../timelines/components/timeline/body/renderers/constants';
Expand All @@ -19,7 +20,7 @@ export interface FieldValueCellProps {
contextId: string;
data: EventFieldsData | FieldsData;
eventId: string;
fieldFromBrowserField?: Partial<BrowserField>;
fieldFromBrowserField?: Partial<FieldSpec>;
getLinkValue?: (field: string) => string | null;
isDraggable?: boolean;
linkValue?: string | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import { render, screen } from '@testing-library/react';
import React from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';

import type { BrowserField } from '../../../containers/source';
import { PrevalenceCellRenderer } from './prevalence_cell';
import { TestProviders } from '../../../mock';
import type { EventFieldsData } from '../types';
Expand All @@ -25,7 +25,7 @@ const mockUseAlertPrevalence = useAlertPrevalence as jest.Mock;

const eventId = 'TUWyf3wBFCFU0qRJTauW';
const hostIpValues = ['127.0.0.1', '::1', '10.1.2.3', '2001:0DB8:AC10:FE01::'];
const hostIpFieldFromBrowserField: BrowserField = {
const hostIpFieldFromBrowserField: FieldSpec = {
aggregatable: true,
name: 'host.ip',
readFromDocValues: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import { act, render, screen } from '@testing-library/react';
import React from 'react';
import type { FieldSpec } from '@kbn/data-plugin/common';

import type { BrowserField } from '../../../containers/source';
import { SummaryValueCell } from './summary_value_cell';
import { TestProviders } from '../../../mock';
import type { EventFieldsData } from '../types';
Expand All @@ -22,7 +22,7 @@ jest.mock('../../../hooks/use_get_field_spec');

const eventId = 'TUWyf3wBFCFU0qRJTauW';
const hostIpValues = ['127.0.0.1', '::1', '10.1.2.3', '2001:0DB8:AC10:FE01::'];
const hostIpFieldFromBrowserField: BrowserField = {
const hostIpFieldFromBrowserField: FieldSpec = {
aggregatable: true,
name: 'host.ip',
readFromDocValues: false,
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 @@ -263,23 +262,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
Loading

0 comments on commit 915ede1

Please sign in to comment.