-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add screen context to investigation details page
- Loading branch information
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
.../observability_solution/investigate_app/public/pages/details/hooks/use_screen_context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters