From 3626401f2d099167a291bfef76930e5e90ef8b9d Mon Sep 17 00:00:00 2001 From: Guido Modarelli Date: Thu, 12 Sep 2024 10:16:33 -0300 Subject: [PATCH] Fix Prettier issues --- .../data-grid/cell-filter-actions.test.tsx | 20 +++- .../common/data-grid/cell-filter-actions.tsx | 38 +++--- .../common/data-grid/data-grid-service.ts | 110 ++++++++++++------ 3 files changed, 110 insertions(+), 58 deletions(-) diff --git a/plugins/main/public/components/common/data-grid/cell-filter-actions.test.tsx b/plugins/main/public/components/common/data-grid/cell-filter-actions.test.tsx index 3ee3cb7680..b3dcea6cb0 100644 --- a/plugins/main/public/components/common/data-grid/cell-filter-actions.test.tsx +++ b/plugins/main/public/components/common/data-grid/cell-filter-actions.test.tsx @@ -1,5 +1,9 @@ import { fireEvent, render, screen } from '@testing-library/react'; -import { cellFilterActions, filterIsAction, filterIsNotAction } from './cell-filter-actions'; +import { + cellFilterActions, + filterIsAction, + filterIsNotAction, +} from './cell-filter-actions'; import { EuiButtonEmpty } from '@elastic/eui'; const indexPattern = { @@ -37,14 +41,14 @@ describe('cell-filter-actions', () => { // @ts-expect-error Argument of type '{ flattenHit: jest.Mock; }' is not assignable to parameter of type 'IndexPattern' indexPattern, rows, - onFilter + onFilter, )({ rowIndex: 0, columnId: TEST_COLUMN_ID, Component: EuiButtonEmpty, isExpanded: false, closePopover: () => {}, - }) + }), ); let component = screen.getByText('Filter for value'); @@ -74,14 +78,14 @@ describe('cell-filter-actions', () => { // @ts-expect-error Argument of type '{ flattenHit: jest.Mock; }' is not assignable to parameter of type 'IndexPattern' indexPattern, rows, - onFilter + onFilter, )({ rowIndex: 0, columnId: TEST_COLUMN_ID, Component: EuiButtonEmpty, isExpanded: false, closePopover: () => {}, - }) + }), ); let component = screen.getByText('Filter out value'); @@ -92,7 +96,11 @@ describe('cell-filter-actions', () => { fireEvent.click(component); expect(onFilter).toHaveBeenCalledTimes(1); - expect(onFilter).toHaveBeenCalledWith(TEST_COLUMN_ID, TEST_VALUE, 'is not'); + expect(onFilter).toHaveBeenCalledWith( + TEST_COLUMN_ID, + TEST_VALUE, + 'is not', + ); }); }); }); diff --git a/plugins/main/public/components/common/data-grid/cell-filter-actions.tsx b/plugins/main/public/components/common/data-grid/cell-filter-actions.tsx index 9376555651..fd57493e6c 100644 --- a/plugins/main/public/components/common/data-grid/cell-filter-actions.tsx +++ b/plugins/main/public/components/common/data-grid/cell-filter-actions.tsx @@ -1,8 +1,14 @@ -import { EuiDataGridColumn, EuiDataGridColumnCellActionProps } from '@elastic/eui'; +import { + EuiDataGridColumn, + EuiDataGridColumnCellActionProps, +} from '@elastic/eui'; import { i18n } from '@osd/i18n'; import React from 'react'; -import { IFieldType, IndexPattern } from '../../../../../../src/plugins/data/common'; -import { FILTER_OPERATOR } from '../data-source'; +import { + IFieldType, + IndexPattern, +} from '../../../../../../src/plugins/data/common'; +import { FILTER_OPERATOR } from '../data-source/pattern/pattern-data-source-filter-manager'; export const filterIsAction = ( indexPattern: IndexPattern, @@ -10,10 +16,14 @@ export const filterIsAction = ( onFilter: ( columndId: string, value: any, - operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT - ) => void + operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT, + ) => void, ) => { - return ({ rowIndex, columnId, Component }: EuiDataGridColumnCellActionProps) => { + return ({ + rowIndex, + columnId, + Component, + }: EuiDataGridColumnCellActionProps) => { const filterForValueText = i18n.translate('discover.filterForValue', { defaultMessage: 'Filter for value', }); @@ -34,9 +44,9 @@ export const filterIsAction = ( return ( {filterForValueText} @@ -51,8 +61,8 @@ export const filterIsNotAction = onFilter: ( columndId: string, value: any, - operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT - ) => void + operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT, + ) => void, ) => ({ rowIndex, columnId, Component }: EuiDataGridColumnCellActionProps) => { const filterOutValueText = i18n.translate('discover.filterOutValue', { @@ -75,9 +85,9 @@ export const filterIsNotAction = return ( {filterOutValueText} @@ -92,8 +102,8 @@ export function cellFilterActions( onFilter: ( columndId: string, value: any, - operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT - ) => void + operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT, + ) => void, ) { if (!field.filterable) return; diff --git a/plugins/main/public/components/common/data-grid/data-grid-service.ts b/plugins/main/public/components/common/data-grid/data-grid-service.ts index 1cd5184383..7ee7dcb70d 100644 --- a/plugins/main/public/components/common/data-grid/data-grid-service.ts +++ b/plugins/main/public/components/common/data-grid/data-grid-service.ts @@ -2,22 +2,33 @@ import { SearchResponse } from '../../../../../../src/core/server'; import * as FileSaver from '../../../services/file-saver'; import { beautifyDate } from '../../agents/vuls/inventory/lib'; import { SearchParams, search } from '../search-bar/search-bar-service'; -import { Filter, IFieldType, IndexPattern } from '../../../../../../src/plugins/data/common'; +import { + Filter, + IFieldType, + IndexPattern, +} from '../../../../../../src/plugins/data/common'; export const MAX_ENTRIES_PER_QUERY = 10000; import { tDataGridColumn } from './use-data-grid'; import { cellFilterActions } from './cell-filter-actions'; -import { FILTER_OPERATOR, PatternDataSourceFilterManager } from '../data-source'; - -type ParseData = { - source: T, - _id: string, - _index: string, - _type: string, - _score: number, -} | {} - -export const parseData = (resultsHits: SearchResponse['hits']['hits']): ParseData[] => { - const data = resultsHits.map((hit) => { +import { + FILTER_OPERATOR, + PatternDataSourceFilterManager, +} from '../data-source/pattern/pattern-data-source-filter-manager'; + +type ParseData = + | { + source: T; + _id: string; + _index: string; + _type: string; + _score: number; + } + | {}; + +export const parseData = ( + resultsHits: SearchResponse['hits']['hits'], +): ParseData[] => { + const data = resultsHits.map(hit => { if (!hit) { return {}; } @@ -34,15 +45,20 @@ export const parseData = (resultsHits: SearchResponse['hits']['h return data; }; -export const getFieldFormatted = (rowIndex: number, columnId: string, indexPattern: IndexPattern, rowsParsed: ParseData[]) => { - const field = indexPattern.fields.find((field) => field.name === columnId); +export const getFieldFormatted = ( + rowIndex: number, + columnId: string, + indexPattern: IndexPattern, + rowsParsed: ParseData[], +) => { + const field = indexPattern.fields.find(field => field.name === columnId); let fieldValue = null; if (columnId.includes('.')) { // when the column is a nested field. The column could have 2 to n levels // get dinamically the value of the nested field const nestedFields = columnId.split('.'); fieldValue = rowsParsed[rowIndex]; - nestedFields.forEach((field) => { + nestedFields.forEach(field => { if (fieldValue) { fieldValue = fieldValue[field]; } @@ -75,14 +91,25 @@ export const getFieldFormatted = (rowIndex: number, columnId: string, indexPatte }; // receive search params -export const exportSearchToCSV = async (params: SearchParams): Promise => { +export const exportSearchToCSV = async ( + params: SearchParams, +): Promise => { const DEFAULT_MAX_SIZE_PER_CALL = 1000; - const { indexPattern, filters = [], query, sorting, fields, pagination } = params; + const { + indexPattern, + filters = [], + query, + sorting, + fields, + pagination, + } = params; // when the pageSize is greater than the default max size per call (10000) // then we need to paginate the search const mustPaginateSearch = pagination?.pageSize && pagination?.pageSize > DEFAULT_MAX_SIZE_PER_CALL; - const pageSize = mustPaginateSearch ? DEFAULT_MAX_SIZE_PER_CALL : pagination?.pageSize; + const pageSize = mustPaginateSearch + ? DEFAULT_MAX_SIZE_PER_CALL + : pagination?.pageSize; const totalHits = pagination?.pageSize || DEFAULT_MAX_SIZE_PER_CALL; let pageIndex = params.pagination?.pageIndex || 0; let hitsCount = 0; @@ -113,13 +140,13 @@ export const exportSearchToCSV = async (params: SearchParams): Promise => } const resultsFields = fields; - const data = allHits.map((hit) => { + const data = allHits.map(hit => { // check if the field type is a date const dateFields = indexPattern.fields.getByType('date'); - const dateFieldsNames = dateFields.map((field) => field.name); + const dateFieldsNames = dateFields.map(field => field.name); const flattenHit = indexPattern.flattenHit(hit); // replace the date fields with the formatted date - dateFieldsNames.forEach((field) => { + dateFieldsNames.forEach(field => { if (flattenHit[field]) { flattenHit[field] = beautifyDate(flattenHit[field]); } @@ -134,8 +161,8 @@ export const exportSearchToCSV = async (params: SearchParams): Promise => if (!data || data.length === 0) return; const parsedData = data - .map((row) => { - const parsedRow = resultsFields?.map((field) => { + .map(row => { + const parsedRow = resultsFields?.map(field => { const value = row[field]; if (value === undefined || value === null) { return ''; @@ -163,18 +190,18 @@ export const exportSearchToCSV = async (params: SearchParams): Promise => const onFilterCellActions = ( indexPatternId: string, filters: Filter[], - setFilters: (filters: Filter[]) => void + setFilters: (filters: Filter[]) => void, ) => { return ( columndId: string, value: any, - operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT + operation: FILTER_OPERATOR.IS | FILTER_OPERATOR.IS_NOT, ) => { const newFilter = PatternDataSourceFilterManager.createFilter( operation, columndId, value, - indexPatternId + indexPatternId, ); setFilters([...filters, newFilter]); }; @@ -186,9 +213,9 @@ const mapToDataGridColumn = ( rows: any[], filters: Filter[], setFilters: (filters: Filter[]) => void, - defaultColumns: tDataGridColumn[] + defaultColumns: tDataGridColumn[], ): tDataGridColumn => { - const defaultColumn = defaultColumns.find((column) => column.id === field.name); + const defaultColumn = defaultColumns.find(column => column.id === field.name); return { ...field, id: field.name, @@ -200,7 +227,7 @@ const mapToDataGridColumn = ( field, indexPattern, rows, - onFilterCellActions(indexPattern.id as string, filters, setFilters) + onFilterCellActions(indexPattern.id as string, filters, setFilters), ), } as tDataGridColumn; }; @@ -211,16 +238,23 @@ export const parseColumns = ( indexPattern: IndexPattern, rows: any[], filters: Filter[], - setFilters: (filters: Filter[]) => void + setFilters: (filters: Filter[]) => void, ): tDataGridColumn[] => { // remove _source field becuase is a object field and is not supported // merge the properties of the field with the default columns if (!fields?.length) return defaultColumns; return fields - .filter((field) => field.name !== '_source') - .map((field) => - mapToDataGridColumn(field, indexPattern, rows, filters, setFilters, defaultColumns) + .filter(field => field.name !== '_source') + .map(field => + mapToDataGridColumn( + field, + indexPattern, + rows, + filters, + setFilters, + defaultColumns, + ), ); }; @@ -233,12 +267,12 @@ export const parseColumns = ( */ export const getAllCustomRenders = ( customColumns: tDataGridColumn[], - discoverColumns: tDataGridColumn[] + discoverColumns: tDataGridColumn[], ): tDataGridColumn[] => { - const customColumnsWithRender = customColumns.filter((column) => column.render); - const allColumns = discoverColumns.map((column) => { + const customColumnsWithRender = customColumns.filter(column => column.render); + const allColumns = discoverColumns.map(column => { const customColumn = customColumnsWithRender.find( - (customColumn) => customColumn.id === column.id + customColumn => customColumn.id === column.id, ); return customColumn || column; });