Skip to content

Commit

Permalink
feat(rca): add screen context into investigation details (#194753)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme authored Oct 3, 2024
1 parent 01bc493 commit 2e6a257
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useInvestigation } from '../../contexts/investigation_context';
import { InvestigationHeader } from '../investigation_header/investigation_header';
import { InvestigationItems } from '../investigation_items/investigation_items';
import { InvestigationNotes } from '../investigation_notes/investigation_notes';
import { useScreenContext } from '../../hooks/use_screen_context';

interface Props {
user: AuthenticatedUser;
Expand All @@ -30,6 +31,7 @@ export function InvestigationDetails({ user }: Props) {
start: { observabilityShared },
},
} = useKibana();
useScreenContext();

const ObservabilityPageTemplate = observabilityShared.navigation.PageTemplate;
const { investigation } = useInvestigation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function useFetchAlert({ id }: AlertParams): UseFetchAlertResponse {
signal,
});
},
staleTime: 60 * 1000,
refetchOnWindowFocus: false,
onError: (error: Error) => {
toasts.addError(error, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 { 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 { useFetchAlert } from './use_fetch_alert';

export function useScreenContext() {
const {
dependencies: {
start: { observabilityAIAssistant },
},
} = 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 });

useEffect(() => {
if (!investigation || isAlertDetailsLoading) {
return;
}

observabilityAIAssistant.service.setScreenContext({
screenDescription: dedent(`
The user is looking at the details of an investigation in order to understand the root cause of an issue.
The investigation details include the title, status, tags, and its time range.
${alertDetails ? getAlertDetailScreenContext(alertDetails) : ''}
Title: ${investigation.title}
Tags: ${investigation.tags.join(', ')}
Status: ${investigation.status}
Start time: ${new Date(investigation.params.timeRange.from).toISOString()}
End time: ${new Date(investigation.params.timeRange.to).toISOString()}
`),
data: [
{
name: 'investigation',
description: 'The investigation details, including metadata, notes and items.',
value: investigation,
},
...(alertDetails
? [
{
name: 'alert',
description: 'The alert details that triggered the investigation.',
value: alertDetails,
},
]
: []),
],
});
}, [observabilityAIAssistant, investigation, alertDetails, isAlertDetailsLoading]);
}

function getAlertDetailScreenContext(alertDetail: EcsFieldsResponse) {
const alertState = alertDetail[ALERT_STATUS];
const alertStarted = alertDetail[ALERT_START];

return dedent(`The investigation originates from an ${alertState} alert which started at ${alertStarted}.
${
alertDetail[ALERT_REASON]
? `The reason given for the alert is ${alertDetail[ALERT_REASON]}.`
: ''
}
`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { InvestigationNotFound } from '../../components/investigation_not_found/
import { useFetchInvestigation } from '../../hooks/use_fetch_investigation';
import { useKibana } from '../../hooks/use_kibana';
import { InvestigationDetails } from './components/investigation_details/investigation_details';
import { InvestigationDetailsPathParams } from './types';
import { InvestigationProvider } from './contexts/investigation_context';
import { InvestigationDetailsPathParams } from './types';

export function InvestigationDetailsPage() {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import type {
import type { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/public';
import type { UiActionsSetup, UiActionsStart } from '@kbn/ui-actions-plugin/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type {
ObservabilityAIAssistantPublicSetup,
ObservabilityAIAssistantPublicStart,
} from '@kbn/observability-ai-assistant-plugin/public';

/* eslint-disable @typescript-eslint/no-empty-interface*/

Expand All @@ -46,6 +50,7 @@ export interface InvestigateAppSetupDependencies {
unifiedSearch: {};
uiActions: UiActionsSetup;
security: SecurityPluginSetup;
observabilityAIAssistant: ObservabilityAIAssistantPublicSetup;
}

export interface InvestigateAppStartDependencies {
Expand All @@ -60,6 +65,7 @@ export interface InvestigateAppStartDependencies {
unifiedSearch: UnifiedSearchPublicPluginStart;
uiActions: UiActionsStart;
security: SecurityPluginStart;
observabilityAIAssistant: ObservabilityAIAssistantPublicStart;
}

export interface InvestigateAppPublicSetup {}
Expand Down

0 comments on commit 2e6a257

Please sign in to comment.