Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Inventory][ECO] Entities table #193272

Merged
merged 22 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const ENTITY_LAST_SEEN = 'entity.lastSeenTimestamp';
export const ENTITY_ID = 'entity.id';
export const ENTITY_TYPE = 'entity.type';
export const ENTITY_DISPLAY_NAME = 'entity.displayName';
export const ENTITY_DEFINITION_ID = 'entity.definitionId';
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,23 @@ export const Example: Story<{}> = () => {
/>
);
};

export const EmptyGridExample: Story<{}> = () => {
const [pageIndex, setPageIndex] = useState(0);
const [sort, setSort] = useState<EuiDataGridSorting['columns'][0]>({
id: ENTITY_LAST_SEEN,
direction: 'desc',
});

return (
<EntitiesGrid
entities={[]}
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
loading={false}
sortDirection={sort.direction}
sortField={sort.id}
onChangePage={setPageIndex}
onChangeSort={setSort}
pageIndex={pageIndex}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
ENTITY_TYPE,
} from '../../../common/es_fields/entities';
import { APIReturnType } from '../../api';
import { MAX_NUMBER_OF_ENTITIES } from '../../../common/entities';

type InventoryEntitiesAPIReturnType = APIReturnType<'GET /internal/inventory/entities'>;

Expand All @@ -33,17 +32,23 @@ type EntityColumnIds = typeof ENTITY_DISPLAY_NAME | typeof ENTITY_LAST_SEEN | ty
const columns: EuiDataGridColumn[] = [
{
id: ENTITY_DISPLAY_NAME,
displayAsText: 'Entity name',
displayAsText: i18n.translate('xpack.inventory.entitiesGrid.euiDataGrid.entityNameLabel', {
defaultMessage: 'Entity name',
}),
isSortable: true,
},
{
id: ENTITY_TYPE,
displayAsText: 'Type',
displayAsText: i18n.translate('xpack.inventory.entitiesGrid.euiDataGrid.typeLabel', {
defaultMessage: 'Type',
}),
isSortable: true,
},
{
id: ENTITY_LAST_SEEN,
displayAsText: 'Last seen',
displayAsText: i18n.translate('xpack.inventory.entitiesGrid.euiDataGrid.lastSeenLabel', {
defaultMessage: 'Last seen',
}),
defaultSortDirection: 'desc',
isSortable: true,
schema: 'datetime',
Expand Down Expand Up @@ -83,54 +88,59 @@ export function EntitiesGrid({
[onChangeSort]
);

if (loading) {
return <EuiLoadingSpinner size="s" />;
}
const CellValue = useCallback(
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
({ rowIndex, columnId }: EuiDataGridCellValueElementProps) => {
const entity = entities[rowIndex];
if (entity === undefined) {
return null;
}

function CellValue({ rowIndex, columnId }: EuiDataGridCellValueElementProps) {
const entity = entities[rowIndex];
if (entity === undefined) {
return null;
}
const columnEntityTableId = columnId as EntityColumnIds;
switch (columnEntityTableId) {
case ENTITY_TYPE:
return <EuiBadge color="hollow">{entity[columnEntityTableId]}</EuiBadge>;
case ENTITY_LAST_SEEN:
return (
<FormattedMessage
id="xpack.inventory.entitiesGrid.euiDataGrid.lastSeen"
defaultMessage="{date} @ {time}"
values={{
date: (
<FormattedDate
value={entity[columnEntityTableId]}
month="short"
day="numeric"
year="numeric"
/>
),
time: (
<FormattedTime
value={entity[columnEntityTableId]}
hour12={false}
hour="2-digit"
minute="2-digit"
second="2-digit"
/>
),
}}
/>
);
case ENTITY_DISPLAY_NAME:
return (
// TODO: link to the appropriate page based on entity type https://github.com/elastic/kibana/issues/192676
<EuiLink data-test-subj="inventoryCellValueLink" className="eui-textTruncate">
{entity[columnEntityTableId]}
</EuiLink>
);
default:
return entity[columnId as EntityColumnIds] || '';
}
},
[entities]
);

const columnEntityTableId = columnId as EntityColumnIds;
switch (columnEntityTableId) {
case ENTITY_TYPE:
return <EuiBadge color="hollow">{entity[columnEntityTableId]}</EuiBadge>;
case ENTITY_LAST_SEEN:
return (
<FormattedMessage
id="xpack.inventory.entitiesGrid.euiDataGrid.lastSeen"
defaultMessage="{date} @ {time}"
values={{
date: (
<FormattedDate
value={entity[columnEntityTableId]}
month="short"
day="numeric"
year="numeric"
/>
),
time: (
<FormattedTime
value={entity[columnEntityTableId]}
hour12={false}
hour="2-digit"
minute="2-digit"
second="2-digit"
/>
),
}}
/>
);
case ENTITY_DISPLAY_NAME:
return (
// TODO: link to the appropriate page based on entity type https://github.com/elastic/kibana/issues/192676
<EuiLink data-test-subj="inventoryCellValueLink">{entity[columnEntityTableId]}</EuiLink>
);
default:
return entity[columnId as EntityColumnIds] || '';
}
if (loading) {
return <EuiLoadingSpinner size="s" />;
}

const currentPage = pageIndex + 1;
Expand Down Expand Up @@ -158,10 +168,11 @@ export function EntitiesGrid({
values={{
currentItems: (
<strong>
{pageIndex * PAGE_SIZE + 1}-{PAGE_SIZE * currentPage}
{Math.min(entities.length, pageIndex * PAGE_SIZE + 1)}-
{Math.min(entities.length, PAGE_SIZE * currentPage)}
</strong>
),
total: MAX_NUMBER_OF_ENTITIES,
total: entities.length,
boldEntites: (
<strong>
{i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export function InventoryPage() {
query: {
sortDirection,
sortField,
// TODO: update this to read data from the entity type search filter https://github.com/elastic/kibana/issues/192329
entityTypes: JSON.stringify(['service', 'host', 'container']),
},
},
signal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type ObservabilityElasticsearchClient } from '@kbn/observability-utils/
import { esqlResultToPlainObjects } from '@kbn/observability-utils/es/utils/esql_result_to_plain_objects';
import { MAX_NUMBER_OF_ENTITIES, type EntityType } from '../../../common/entities';
import {
ENTITY_DEFINITION_ID,
ENTITY_DISPLAY_NAME,
ENTITY_ID,
ENTITY_LAST_SEEN,
Expand All @@ -21,13 +22,19 @@ const ENTITIES_LATEST_ALIAS = entitiesAliasPattern({
dataset: ENTITY_LATEST,
});

interface LatestEntity {
const BUILTIN_SERVICES_FROM_ECS_DATA = 'builtin_services_from_ecs_data';
const BUILTIN_HOSTS_FROM_ECS_DATA = 'builtin_hosts_from_ecs_data';
const BUILTIN_CONTAINERS_FROM_ECS_DATA = 'builtin_containers_from_ecs_data';

export interface LatestEntity {
[ENTITY_LAST_SEEN]: string;
[ENTITY_TYPE]: string;
[ENTITY_DISPLAY_NAME]: string;
[ENTITY_ID]: string;
}

const DEFAULT_ENTITY_TYPES = ['service', 'host', 'container'];

export async function getLatestEntities({
inventoryEsClient,
sortDirection,
Expand All @@ -37,12 +44,19 @@ export async function getLatestEntities({
inventoryEsClient: ObservabilityElasticsearchClient;
sortDirection: 'asc' | 'desc';
sortField: string;
entityTypes: EntityType[];
entityTypes?: EntityType[];
}) {
const entityTypesFilter = entityTypes?.length ? entityTypes : DEFAULT_ENTITY_TYPES;
const latestEntitiesEsqlResponse = await inventoryEsClient.esql('get_latest_entities', {
query: `FROM ${ENTITIES_LATEST_ALIAS}
| WHERE entity.type IN (${entityTypes.map((entityType) => `"${entityType}"`).join()})
| WHERE entity.definitionId IN ("builtin_services_from_ecs_data", "builtin_hosts_from_ecs_data", "builtin_containers_from_ecs_data")
| WHERE ${ENTITY_TYPE} IN (${entityTypesFilter.map((entityType) => `"${entityType}"`).join()})
| WHERE ${ENTITY_DEFINITION_ID} IN (${[
BUILTIN_SERVICES_FROM_ECS_DATA,
BUILTIN_HOSTS_FROM_ECS_DATA,
BUILTIN_CONTAINERS_FROM_ECS_DATA,
]
.map((buildin) => `"${buildin}"`)
.join()})
| SORT ${sortField} ${sortDirection}
| LIMIT ${MAX_NUMBER_OF_ENTITIES}
| KEEP ${ENTITY_LAST_SEEN}, ${ENTITY_TYPE}, ${ENTITY_DISPLAY_NAME}, ${ENTITY_ID}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import { getLatestEntities } from './get_latest_entities';
export const listLatestEntitiesRoute = createInventoryServerRoute({
endpoint: 'GET /internal/inventory/entities',
params: t.type({
query: t.type({
sortField: t.string,
sortDirection: t.union([t.literal('asc'), t.literal('desc')]),
entityTypes: jsonRt.pipe(t.array(entityTypeRt)),
}),
query: t.intersection([
t.type({
sortField: t.string,
sortDirection: t.union([t.literal('asc'), t.literal('desc')]),
}),
t.partial({
entityTypes: jsonRt.pipe(t.array(entityTypeRt)),
}),
]),
}),
options: {
tags: ['access:inventory'],
Expand Down