Skip to content

Commit

Permalink
Fix Prettier issues
Browse files Browse the repository at this point in the history
  • Loading branch information
guidomodarelli committed Sep 12, 2024
1 parent f3eb084 commit 3626401
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -37,14 +41,14 @@ describe('cell-filter-actions', () => {
// @ts-expect-error Argument of type '{ flattenHit: jest.Mock<any, any>; }' 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');
Expand Down Expand Up @@ -74,14 +78,14 @@ describe('cell-filter-actions', () => {
// @ts-expect-error Argument of type '{ flattenHit: jest.Mock<any, any>; }' 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');
Expand All @@ -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',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
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,
rows: any[],
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',
});
Expand All @@ -34,9 +44,9 @@ export const filterIsAction = (
return (
<Component
onClick={handleClick}
iconType="plusInCircle"
iconType='plusInCircle'
aria-label={filterForValueLabel}
data-test-subj="filterForValue"
data-test-subj='filterForValue'
>
{filterForValueText}
</Component>
Expand All @@ -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', {
Expand All @@ -75,9 +85,9 @@ export const filterIsNotAction =
return (
<Component
onClick={handleClick}
iconType="minusInCircle"
iconType='minusInCircle'
aria-label={filterOutValueLabel}
data-test-subj="filterOutValue"
data-test-subj='filterOutValue'
>
{filterOutValueText}
</Component>
Expand All @@ -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;

Expand Down
110 changes: 72 additions & 38 deletions plugins/main/public/components/common/data-grid/data-grid-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
source: T,
_id: string,
_index: string,
_type: string,
_score: number,
} | {}

export const parseData = <T = unknown>(resultsHits: SearchResponse<T>['hits']['hits']): ParseData<T>[] => {
const data = resultsHits.map((hit) => {
import {
FILTER_OPERATOR,
PatternDataSourceFilterManager,
} from '../data-source/pattern/pattern-data-source-filter-manager';

type ParseData<T> =
| {
source: T;
_id: string;
_index: string;
_type: string;
_score: number;
}
| {};

export const parseData = <T = unknown>(
resultsHits: SearchResponse<T>['hits']['hits'],
): ParseData<T>[] => {
const data = resultsHits.map(hit => {
if (!hit) {
return {};
}
Expand All @@ -34,15 +45,20 @@ export const parseData = <T = unknown>(resultsHits: SearchResponse<T>['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];
}
Expand Down Expand Up @@ -75,14 +91,25 @@ export const getFieldFormatted = (rowIndex: number, columnId: string, indexPatte
};

// receive search params
export const exportSearchToCSV = async (params: SearchParams): Promise<void> => {
export const exportSearchToCSV = async (
params: SearchParams,
): Promise<void> => {
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;
Expand Down Expand Up @@ -113,13 +140,13 @@ export const exportSearchToCSV = async (params: SearchParams): Promise<void> =>
}

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]);
}
Expand All @@ -134,8 +161,8 @@ export const exportSearchToCSV = async (params: SearchParams): Promise<void> =>
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 '';
Expand Down Expand Up @@ -163,18 +190,18 @@ export const exportSearchToCSV = async (params: SearchParams): Promise<void> =>
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]);
};
Expand All @@ -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,
Expand All @@ -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;
};
Expand All @@ -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,
),
);
};

Expand All @@ -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;
});
Expand Down

0 comments on commit 3626401

Please sign in to comment.