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

[Investigation app] add entities route and investigation Contextual Insight #194432

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
27c0182
add entities route
dominiqueclarke Sep 30, 2024
2d305e7
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Sep 30, 2024
84a3817
Merge branch 'main' into feature/investigation-entities
dominiqueclarke Sep 30, 2024
dc8c47a
add initial contextual insight
dominiqueclarke Oct 1, 2024
be4dfb7
merge main
dominiqueclarke Oct 1, 2024
16f926d
adjust entities es client
dominiqueclarke Oct 1, 2024
72914b4
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Oct 1, 2024
a67953d
add entity sources to the assistant prompt
dominiqueclarke Oct 1, 2024
f041ec1
Merge branch 'feature/investigation-entities' of github.com:dominique…
dominiqueclarke Oct 1, 2024
503b7f8
adjust prompt
dominiqueclarke Oct 2, 2024
552d79c
Merge branch 'main' of https://github.com/elastic/kibana into feature…
dominiqueclarke Oct 2, 2024
1d65aec
Update x-pack/plugins/observability_solution/investigate_app/public/h…
dominiqueclarke Oct 2, 2024
2907990
remove unnecessary async keyword
dominiqueclarke Oct 2, 2024
3c542d4
Merge branch 'feature/investigation-entities' of github.com:dominique…
dominiqueclarke Oct 2, 2024
53655e5
pass esClient to getEntities
dominiqueclarke Oct 2, 2024
3fb9019
remove entity history references
dominiqueclarke Oct 2, 2024
862e98f
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 2, 2024
ed126e4
adjust alert hook
dominiqueclarke Oct 3, 2024
2d119de
adjust plugin definition
dominiqueclarke Oct 3, 2024
56b2fb1
Merge branch 'feature/investigation-entities' of github.com:dominique…
dominiqueclarke Oct 3, 2024
1932efe
remove imports from ai assistant
dominiqueclarke Oct 3, 2024
b70caeb
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Oct 3, 2024
c24a620
adjust types
dominiqueclarke Oct 3, 2024
d9d211b
Merge branch 'feature/investigation-entities' of github.com:dominique…
dominiqueclarke Oct 3, 2024
2a27f75
merge main
dominiqueclarke Oct 4, 2024
4ab1a83
account for missing sources
dominiqueclarke Oct 4, 2024
f95017d
adjust types
dominiqueclarke Oct 4, 2024
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
48 changes: 48 additions & 0 deletions packages/kbn-investigation-shared/src/rest_specs/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { z } from '@kbn/zod';

const metricsSchema = z.object({
failedTransactionRate: z.number().optional(),
latency: z.number().optional(),
throughput: z.number().optional(),
logErrorRate: z.number().optional(),
logRate: z.number().optional(),
});

const entitySchema = z.object({
id: z.string(),
definitionId: z.string(),
definitionVersion: z.string(),
displayName: z.string(),
firstSeenTimestamp: z.string(),
lastSeenTimestamp: z.string(),
identityFields: z.array(z.string()),
schemaVersion: z.string(),
type: z.string(),
metrics: metricsSchema,
});

const entitySourceSchema = z.object({
dataStream: z.string().optional(),
});

const entityWithSourceSchema = z.intersection(
entitySchema,
z.object({
sources: z.array(entitySourceSchema),
})
);

type EntityWithSource = z.output<typeof entityWithSourceSchema>;
type EntitySource = z.output<typeof entitySourceSchema>;

export { entitySchema, entityWithSourceSchema };
export type { EntityWithSource, EntitySource };
34 changes: 34 additions & 0 deletions packages/kbn-investigation-shared/src/rest_specs/get_entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { z } from '@kbn/zod';
import { entityWithSourceSchema } from './entity';

const getEntitiesParamsSchema = z
.object({
query: z
.object({
'service.name': z.string(),
'service.environment': z.string(),
'host.name': z.string(),
'container.id': z.string(),
})
.partial(),
})
.partial();

const getEntitiesResponseSchema = z.object({
entities: z.array(entityWithSourceSchema),
});

type GetEntitiesParams = z.infer<typeof getEntitiesParamsSchema.shape.query>;
type GetEntitiesResponse = z.output<typeof getEntitiesResponseSchema>;

export { getEntitiesParamsSchema, getEntitiesResponseSchema };
export type { GetEntitiesParams, GetEntitiesResponse };
4 changes: 4 additions & 0 deletions packages/kbn-investigation-shared/src/rest_specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type * from './update_item';
export type * from './update_note';
export type * from './event';
export type * from './get_events';
export type * from './entity';
export type * from './get_entities';

export * from './create';
export * from './create_item';
Expand All @@ -48,3 +50,5 @@ export * from './update_item';
export * from './update_note';
export * from './event';
export * from './get_events';
export * from './entity';
export * from './get_entities';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"configPath": ["xpack", "investigateApp"],
"requiredPlugins": [
"investigate",
"observabilityAIAssistant",
"observabilityShared",
"lens",
"dataViews",
Expand All @@ -28,7 +27,7 @@
"kibanaReact",
"kibanaUtils",
],
"optionalPlugins": [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already in the requiredPlugins

"optionalPlugins": ["observabilityAIAssistant"],
"extraPublicDirs": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export const investigationKeys = {
[...investigationKeys.detail(investigationId), 'notes'] as const,
detailItems: (investigationId: string) =>
[...investigationKeys.detail(investigationId), 'items'] as const,
entities: ({
investigationId,
...params
}: {
investigationId: string;
serviceName?: string;
serviceEnvironment?: string;
hostName?: string;
containerId?: string;
}) => [...investigationKeys.detail(investigationId), 'entities', params] as const,
};

export type InvestigationKeys = typeof investigationKeys;
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import { useQuery } from '@tanstack/react-query';
import { BASE_RAC_ALERTS_API_PATH, EcsFieldsResponse } from '@kbn/rule-registry-plugin/common';
import { useKibana } from '../../../hooks/use_kibana';
import { type GetInvestigationResponse, alertOriginSchema } from '@kbn/investigation-shared';
import { useKibana } from './use_kibana';

export interface AlertParams {
id?: string;
export interface UseFetchAlertParams {
investigation?: GetInvestigationResponse;
}

export interface UseFetchAlertResponse {
Expand All @@ -22,20 +23,22 @@ export interface UseFetchAlertResponse {
data: EcsFieldsResponse | undefined | null;
}

export function useFetchAlert({ id }: AlertParams): UseFetchAlertResponse {
export function useFetchAlert({ investigation }: UseFetchAlertParams): UseFetchAlertResponse {
const {
core: {
http,
notifications: { toasts },
},
} = useKibana();
const alertOriginInvestigation = alertOriginSchema.safeParse(investigation?.origin);
const alertId = alertOriginInvestigation.success ? alertOriginInvestigation.data.id : undefined;

const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data } = useQuery({
queryKey: ['fetchAlert', id],
queryKey: ['fetchAlert', investigation?.id],
queryFn: async ({ signal }) => {
return await http.get<EcsFieldsResponse>(BASE_RAC_ALERTS_API_PATH, {
query: {
id,
id: alertId,
},
signal,
});
Expand All @@ -47,7 +50,7 @@ export function useFetchAlert({ id }: AlertParams): UseFetchAlertResponse {
title: 'Something went wrong while fetching alert',
});
},
enabled: Boolean(id),
enabled: Boolean(alertId),
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useQuery } from '@tanstack/react-query';
import { GetEntitiesResponse } from '@kbn/investigation-shared';
import { useKibana } from './use_kibana';
import { investigationKeys } from './query_key_factory';

export interface EntityParams {
investigationId: string;
serviceName?: string;
serviceEnvironment?: string;
hostName?: string;
containerId?: string;
}

export function useFetchEntities({
investigationId,
serviceName,
serviceEnvironment,
hostName,
containerId,
}: EntityParams) {
const {
core: { http },
} = useKibana();

const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data } = useQuery({
queryKey: investigationKeys.entities({
investigationId,
serviceName,
serviceEnvironment,
hostName,
containerId,
}),
queryFn: async ({ signal }) => {
return await http.get<GetEntitiesResponse>('/api/observability/investigation/entities', {
query: {
'service.name': serviceName,
'service.environment': serviceEnvironment,
'host.name': hostName,
'container.id': containerId,
},
version: '2023-10-31',
signal,
});
},
refetchOnWindowFocus: false,
onError: (error: Error) => {
// ignore error
},
enabled: Boolean(investigationId && (serviceName || hostName || containerId)),
});

return {
data,
isInitialLoading,
isLoading,
isRefetching,
isSuccess,
isError,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* 2.0.
*/

import { alertOriginSchema } from '@kbn/investigation-shared';
import { ALERT_REASON, ALERT_START, ALERT_STATUS } from '@kbn/rule-data-utils';
import type { EcsFieldsResponse } from '@kbn/rule-registry-plugin/common';
import dedent from 'dedent';
import { useEffect } from 'react';
import { useKibana } from '../../../hooks/use_kibana';
import { useInvestigation } from '../contexts/investigation_context';
import { useKibana } from './use_kibana';
import { useInvestigation } from '../pages/details/contexts/investigation_context';
import { useFetchAlert } from './use_fetch_alert';

export function useScreenContext() {
Expand All @@ -22,9 +21,7 @@ export function useScreenContext() {
} = useKibana();

const { investigation } = useInvestigation();
const alertOriginInvestigation = alertOriginSchema.safeParse(investigation?.origin);
const alertId = alertOriginInvestigation.success ? alertOriginInvestigation.data.id : undefined;
const { data: alertDetails, isLoading: isAlertDetailsLoading } = useFetchAlert({ id: alertId });
const { data: alertDetails, isLoading: isAlertDetailsLoading } = useFetchAlert({ investigation });

useEffect(() => {
if (!investigation || isAlertDetailsLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EuiLoadingSpinner, EuiFlexItem } from '@elastic/eui';
import { css } from '@emotion/css';
import { ReactEmbeddableRenderer } from '@kbn/embeddable-plugin/public';
import type { GlobalWidgetParameters } from '@kbn/investigate-plugin/public';
import { useAbortableAsync } from '@kbn/observability-ai-assistant-plugin/public';
import { useAbortableAsync } from '@kbn/observability-utils/hooks/use_abortable_async';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { v4 } from 'uuid';
import { ErrorMessage } from '../../components/error_message';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ESQLSearchResponse } from '@kbn/es-types';
import { i18n } from '@kbn/i18n';
import { type GlobalWidgetParameters } from '@kbn/investigate-plugin/public';
import type { Suggestion } from '@kbn/lens-plugin/public';
import { useAbortableAsync } from '@kbn/observability-ai-assistant-plugin/public';
import { useAbortableAsync } from '@kbn/observability-utils/hooks/use_abortable_async';
import React, { useMemo } from 'react';
import { ErrorMessage } from '../../components/error_message';
import { useKibana } from '../../hooks/use_kibana';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ESQLColumn, ESQLRow } from '@kbn/es-types';
import { GlobalWidgetParameters } from '@kbn/investigate-plugin/public';
import { Item } from '@kbn/investigation-shared';
import type { Suggestion } from '@kbn/lens-plugin/public';
import { useAbortableAsync } from '@kbn/observability-ai-assistant-plugin/public';
import { useAbortableAsync } from '@kbn/observability-utils/hooks/use_abortable_async';
import React, { useEffect, useMemo, useState } from 'react';
import { ErrorMessage } from '../../../../components/error_message';
import { SuggestVisualizationList } from '../../../../components/suggest_visualization_list';
Expand Down
Loading
Loading