Skip to content

Commit

Permalink
Add screen context to investigation details page
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Oct 2, 2024
1 parent 8eceb0d commit 16ac871
Show file tree
Hide file tree
Showing 5 changed files with 101 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,91 @@
/*
* 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 { omit } from 'lodash';
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 } = useFetchAlert({ id: alertId });

useEffect(() => {
if (!investigation) {
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,
},
],
});
}, [observabilityAIAssistant, investigation, alertDetails]);
}

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]}.`
: ''
}
Use the following alert fields to understand the context of the alert:
${Object.entries(getRelevantAlertFields(alertDetail))
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
.join('\n')}
`);
}

function getRelevantAlertFields(alertDetail: EcsFieldsResponse) {
return omit(alertDetail.fields, [
'kibana.alert.rule.revision',
'kibana.alert.rule.execution.uuid',
'kibana.alert.flapping_history',
'kibana.alert.uuid',
'kibana.alert.rule.uuid',
'event.action',
'event.kind',
'kibana.alert.rule.tags',
'kibana.alert.maintenance_window_ids',
'kibana.alert.consecutive_matches',
]);
}
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 16ac871

Please sign in to comment.