Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoblaz committed Feb 25, 2025
1 parent 080eb40 commit 9035ca7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
* 2.0.
*/

export const MAX_ASSETS_TO_LOAD = 500; // equivalent to MAX_FINDINGS_TO_LOAD in @kbn/cloud-security-posture-common
export const MAX_ASSETS_TO_LOAD = 500;
export const DEFAULT_VISIBLE_ROWS_PER_PAGE = 25;
export const ASSET_INVENTORY_INDEX_PATTERN = 'logs-cloud_asset_inventory.asset_inventory-*';

export const GRID_DATA_QUERY_KEY = 'asset_inventory_grid_data';
export const CHART_DATA_QUERY_KEY = 'asset_inventory_chart_data';

export const ASSET_INVENTORY_TABLE_ID = 'asset-inventory-table';

const LOCAL_STORAGE_PREFIX = 'assetInventory';
export const LOCAL_STORAGE_COLUMNS_KEY = `${LOCAL_STORAGE_PREFIX}:columns`;
export const LOCAL_STORAGE_COLUMNS_SETTINGS_KEY = `${LOCAL_STORAGE_COLUMNS_KEY}:settings`;
export const LOCAL_STORAGE_DATA_TABLE_PAGE_SIZE_KEY = `${LOCAL_STORAGE_PREFIX}:dataTable:pageSize`;

export const ASSET_INVENTORY_TEST_SUBJ_GRID = 'asset-inventory-test-subj-grid-wrapper';
export const ASSET_INVENTORY_TEST_SUBJ_PAGE_TITLE = 'all-assets-title'; // 'asset-inventory-test-subj-page-title'
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const LOCAL_STORAGE_DATA_TABLE_COLUMNS_KEY = 'assetInventory:dataTable:columns';
export interface AssetsBaseURLQuery {
query: Query;
filters: Filter[];
/**
* Filters that are part of the query but not persisted in the URL or in the Filter Manager
*/
nonPersistedFilters?: Filter[];
/**
* Grouping component selection
*/
Expand Down Expand Up @@ -60,12 +56,10 @@ export const useAssetInventoryDataTable = ({
defaultQuery = getDefaultQuery,
paginationLocalStorageKey,
columnsLocalStorageKey,
nonPersistedFilters,
}: {
defaultQuery?: (params: AssetsBaseURLQuery) => URLQuery;
paginationLocalStorageKey: string;
columnsLocalStorageKey?: string;
nonPersistedFilters?: Filter[];
}): AssetInventoryDataTableResult => {
const getPersistedDefaultQuery = usePersistedQuery<URLQuery>(defaultQuery);
const { urlQuery, setUrlQuery } = useUrlQuery<URLQuery>(getPersistedDefaultQuery);
Expand Down Expand Up @@ -128,7 +122,6 @@ export const useAssetInventoryDataTable = ({
const baseEsQuery = useBaseEsQuery({
filters: urlQuery.filters,
query: urlQuery.query,
...(nonPersistedFilters ? { nonPersistedFilters } : {}),
});

const handleUpdateQuery = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
type UnifiedDataTableSettingsColumn,
} from '@kbn/unified-data-table';
import { CellActionsProvider } from '@kbn/cell-actions';
import { type HttpSetup } from '@kbn/core-http-browser';
import { SHOW_MULTIFIELDS, SORT_DEFAULT_ORDER_SETTING } from '@kbn/discover-utils';
import { type DataTableRecord } from '@kbn/discover-utils/types';
import {
Expand Down Expand Up @@ -62,7 +61,16 @@ import {
} from '../hooks/use_asset_inventory_data_table';
import { useFetchGridData } from '../hooks/use_fetch_grid_data';
import { useFetchChartData } from '../hooks/use_fetch_chart_data';
import { DEFAULT_VISIBLE_ROWS_PER_PAGE, MAX_ASSETS_TO_LOAD } from '../constants';
import {
DEFAULT_VISIBLE_ROWS_PER_PAGE,
MAX_ASSETS_TO_LOAD,
ASSET_INVENTORY_TABLE_ID,
ASSET_INVENTORY_TEST_SUBJ_GRID,
ASSET_INVENTORY_TEST_SUBJ_PAGE_TITLE,
LOCAL_STORAGE_COLUMNS_KEY,
LOCAL_STORAGE_COLUMNS_SETTINGS_KEY,
LOCAL_STORAGE_DATA_TABLE_PAGE_SIZE_KEY,
} from '../constants';

const gridStyle: EuiDataGridStyle = {
border: 'horizontal',
Expand All @@ -75,9 +83,6 @@ const title = i18n.translate('xpack.securitySolution.assetInventory.allAssets.ta
defaultMessage: 'assets',
});

const columnsLocalStorageKey = 'assetInventoryColumns';
const LOCAL_STORAGE_DATA_TABLE_PAGE_SIZE_KEY = 'assetInventory:dataTable:pageSize';

const columnHeaders: Record<string, string> = {
'asset.risk': i18n.translate('xpack.securitySolution.assetInventory.allAssets.risk', {
defaultMessage: 'Risk',
Expand Down Expand Up @@ -131,17 +136,6 @@ const getDefaultQuery = ({ query, filters }: AssetsBaseURLQuery): URLQuery => ({
sort: [['@timestamp', 'desc']],
});

export interface AllAssetsProps {
height?: number | string;
nonPersistedFilters?: Filter[];
hasDistributionBar?: boolean;
/**
* This function will be used in the control column to create a rule for a specific finding.
*/
createFn?: (rowIndex: number) => ((http: HttpSetup) => Promise<unknown>) | undefined;
'data-test-subj'?: string;
}

// TODO: Asset Inventory - adjust and remove type casting once we have real universal entity data
const getEntity = (row: DataTableRecord): EntityEcs => {
return {
Expand All @@ -152,19 +146,10 @@ const getEntity = (row: DataTableRecord): EntityEcs => {
};
};

const ASSET_INVENTORY_TABLE_ID = 'asset-inventory-table';

const AllAssets = ({
nonPersistedFilters,
height,
hasDistributionBar = true,
createFn,
...rest
}: AllAssetsProps) => {
const AllAssets = () => {
const { euiTheme } = useEuiTheme();

const {
filters,
pageSize,
sort,
query,
Expand All @@ -177,9 +162,8 @@ const AllAssets = ({
setUrlQuery,
} = useAssetInventoryDataTable({
paginationLocalStorageKey: LOCAL_STORAGE_DATA_TABLE_PAGE_SIZE_KEY,
columnsLocalStorageKey,
columnsLocalStorageKey: LOCAL_STORAGE_COLUMNS_KEY,
defaultQuery: getDefaultQuery,
nonPersistedFilters,
});

// Table Flyout Controls -------------------------------------------------------------------
Expand Down Expand Up @@ -235,12 +219,12 @@ const AllAssets = ({
const totalHits = rowsData?.pages[0].total || 0;

const [columns, setColumns] = useLocalStorage(
columnsLocalStorageKey,
LOCAL_STORAGE_COLUMNS_KEY,
defaultColumns.map((c) => c.id)
);

const [persistedSettings, setPersistedSettings] = useLocalStorage<UnifiedDataTableSettings>(
`${columnsLocalStorageKey}:settings`,
LOCAL_STORAGE_COLUMNS_SETTINGS_KEY,
{
columns: defaultColumns.reduce((columnSettings, column) => {
const columnDefaultSettings = column.width ? { width: column.width } : {};
Expand Down Expand Up @@ -316,22 +300,18 @@ const AllAssets = ({
const isVirtualizationEnabled = pageSize >= 100;

const getWrapperHeight = () => {
if (height) return height;

// If virtualization is not needed the table will render unconstrained.
if (!isVirtualizationEnabled) return 'auto';

const baseHeight = 362; // height of Kibana Header + Findings page header and search bar
const filterBarHeight = filters?.length > 0 ? 40 : 0;
const distributionBarHeight = hasDistributionBar ? 52 : 0;
return `calc(100vh - ${baseHeight}px - ${filterBarHeight}px - ${distributionBarHeight}px)`;
return `calc(100vh - ${baseHeight}px)`;
};

return {
wrapperHeight: getWrapperHeight(),
mode: isVirtualizationEnabled ? 'virtualized' : 'standard',
};
}, [pageSize, height, filters?.length, hasDistributionBar]);
}, [pageSize]);

const onAddFilter: AddFieldFilterHandler | undefined = useMemo(
() =>
Expand Down Expand Up @@ -382,7 +362,6 @@ const AllAssets = ({
columns={currentColumns}
onAddColumn={onAddColumn}
onRemoveColumn={onRemoveColumn}
// groupSelectorComponent={groupSelectorComponent}
onResetColumns={onResetColumns}
/>
);
Expand All @@ -392,7 +371,7 @@ const AllAssets = ({
id: 'take-action',
width: 20,
headerCellRender: () => null,
rowCellRender: ({ rowIndex }) => (
rowCellRender: () => (
<EuiButtonIcon
aria-label={i18n.translate(
'xpack.securitySolution.assetInventory.flyout.moreActionsButton',
Expand All @@ -403,7 +382,6 @@ const AllAssets = ({
iconType="boxesHorizontal"
color="primary"
isLoading={isLoadingGridData}
// onClick={() => createFn(rowIndex)}
/>
),
},
Expand All @@ -420,7 +398,7 @@ const AllAssets = ({
loading={loadingState === DataLoadingState.loading}
/>
<EuiPageTemplate.Section>
<EuiTitle size="l" data-test-subj="all-assets-title">
<EuiTitle size="l" data-test-subj={ASSET_INVENTORY_TEST_SUBJ_PAGE_TITLE}>
<h1>
<FormattedMessage
id="xpack.securitySolution.assetInventory.allAssets"
Expand Down Expand Up @@ -459,7 +437,7 @@ const AllAssets = ({
) : null}
<CellActionsProvider getTriggerCompatibleActions={uiActions.getTriggerCompatibleActions}>
<div
data-test-subj={rest['data-test-subj']}
data-test-subj={ASSET_INVENTORY_TEST_SUBJ_GRID}
className={styles.gridContainer}
style={{
height: computeDataTableRendering.wrapperHeight,
Expand Down Expand Up @@ -501,8 +479,6 @@ const AllAssets = ({
gridStyleOverride={gridStyle}
rowLineHeightOverride="24px"
dataGridDensityState={DataGridDensity.EXPANDED}
showFullScreenButton
// showKeyboardShortcuts
/>
)}
</div>
Expand Down

0 comments on commit 9035ca7

Please sign in to comment.