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

[Backport 2.x] Updated alerts and findings UI pages #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
165 changes: 118 additions & 47 deletions public/pages/Alerts/components/AlertFlyout/AlertFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,124 @@ import {
EuiFlyout,
EuiFlyoutBody,
EuiFlyoutHeader,
EuiFormRow,
EuiLink,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import { AlertItem } from '../../../../../server/models/interfaces';
import { AlertItem, RuleSource } from '../../../../../server/models/interfaces';
import React from 'react';
import { ContentPanel } from '../../../../components/ContentPanel';
import { DEFAULT_EMPTY_DATA } from '../../../../utils/constants';
import { renderTime } from '../../../../utils/helpers';
import { FindingsService } from '../../../../services';
import { ALERT_STATE, DEFAULT_EMPTY_DATA } from '../../../../utils/constants';
import { createTextDetailsGroup, renderTime } from '../../../../utils/helpers';
import { FindingsService, RuleService } from '../../../../services';
import FindingDetailsFlyout from '../../../Findings/components/FindingDetailsFlyout';
import { Detector, Rule } from '../../../../../models/interfaces';
import { parseAlertSeverityToOption } from '../../../CreateDetector/components/ConfigureAlerts/utils/helpers';
import { Finding } from '../../../Findings/models/interfaces';

export interface AlertFlyoutProps {
alertItem: AlertItem;
detector: Detector;
findingsService: FindingsService;
ruleService: RuleService;
onClose: () => void;
onAcknowledge: (selectedItems: AlertItem[]) => void;
}

export interface AlertFlyoutState {
acknowledged: boolean;
findingFlyoutData?: Finding;
findingItems: Finding[];
loading: boolean;
rules: { [key: string]: Rule };
}

export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutState> {
constructor(props: AlertFlyoutProps) {
super(props);

this.state = {
acknowledged: props.alertItem.state === ALERT_STATE.ACKNOWLEDGED,
findingItems: [],
loading: false,
rules: {},
};
}

async componentDidMount() {
this.getFindings();
}

setFindingFlyoutData(finding?: Finding) {
this.setState({ findingFlyoutData: finding });
}

getFindings = async () => {
this.setState({ loading: true });
const findingRes = await this.props.findingsService.getFindings({
detectorId: this.props.alertItem.detector_id,
});

if (findingRes.ok) {
this.setState({ findingItems: findingRes.response.findings });
}
}
await this.getRules();
this.setState({ loading: false });
};

createTextDetailsGroup(data: { label: string; content: string; url?: string }[]) {
return (
<>
<EuiFlexGroup style={{ padding: 20 }}>
{data.map(({ label, content, url }) => {
return (
<EuiFlexItem key={label} grow={false} style={{ minWidth: '30%' }}>
<EuiFormRow label={label}>
{url ? (
<EuiLink>{content || DEFAULT_EMPTY_DATA}</EuiLink>
) : (
<EuiText>{content || DEFAULT_EMPTY_DATA}</EuiText>
)}
</EuiFormRow>
</EuiFlexItem>
);
})}
</EuiFlexGroup>
<EuiSpacer size={'xl'} />
</>
);
}
getRules = async () => {
try {
const { ruleService } = this.props;
const { findingItems } = this.state;
const ruleIds: string[] = [];
findingItems.forEach((finding) => {
finding.queries.forEach((query) => ruleIds.push(query.id));
});
const body = {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
terms: {
_id: ruleIds,
},
},
},
},
};

setFindingFlyoutData(finding?: Finding) {
this.setState({ findingFlyoutData: finding });
}
if (ruleIds.length > 0) {
const prePackagedResponse = await ruleService.getRules(true, body);
const customResponse = await ruleService.getRules(false, body);

const allRules: { [id: string]: RuleSource } = {};
if (prePackagedResponse.ok) {
prePackagedResponse.response.hits.hits.forEach(
(hit) => (allRules[hit._id] = hit._source)
);
} else {
console.error('Failed to retrieve pre-packaged rules:', prePackagedResponse.error);
}
if (customResponse.ok) {
customResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
} else {
console.error('Failed to retrieve custom rules:', customResponse.error);
// TODO: Display toast with error details
}

this.setState({ rules: allRules });
}
} catch (e) {
console.error('Failed to retrieve rules:', e);
// TODO: Display toast with error details
}
};

createFindingTableColumns(): EuiBasicTableColumn<Finding>[] {
const { detector } = this.props;
const { rules } = this.state;
return [
{
field: 'timestamp',
Expand All @@ -98,48 +145,54 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
sortable: true,
dataType: 'string',
render: (id, finding) =>
<EuiLink onClick={() => this.setFindingFlyoutData(finding)}>{id}</EuiLink> ||
DEFAULT_EMPTY_DATA,
(
<EuiLink onClick={() => this.setFindingFlyoutData(finding)}>
{`${(id as string).slice(0, 7)}...`}
</EuiLink>
) || DEFAULT_EMPTY_DATA,
},
{
field: 'queries',
name: 'Rule name',
sortable: true,
dataType: 'string',
render: (queries: any) => queries[0].name || DEFAULT_EMPTY_DATA,
render: (queries: any[]) => rules[queries[0]?.id]?.title || DEFAULT_EMPTY_DATA,
},
{
field: 'detector_name',
field: 'detector_id',
name: 'Threat detector',
sortable: true,
dataType: 'string',
render: (name: string) => name || DEFAULT_EMPTY_DATA,
render: () => detector.name || DEFAULT_EMPTY_DATA,
},
{
field: 'queries',
name: 'Log type',
sortable: true,
dataType: 'string',
render: (queries: any) => queries[0].category || DEFAULT_EMPTY_DATA,
render: () => detector.detector_type || DEFAULT_EMPTY_DATA,
},
];
}

render() {
const { onClose, alertItem } = this.props;
const { onClose, alertItem, detector, onAcknowledge } = this.props;
const {
trigger_name,
state,
severity,
start_time,
last_notification_time,
finding_ids,
detector_id,
} = alertItem;
const { acknowledged, findingFlyoutData, loading, rules } = this.state;

return !!this.state.findingFlyoutData ? (
<FindingDetailsFlyout
finding={this.state.findingFlyoutData}
{...this.props}
finding={{
...findingFlyoutData,
detector: { _id: detector.id, _index: '', _source: detector },
}}
closeFlyout={onClose}
backButton={
<EuiButtonIcon
Expand All @@ -150,6 +203,7 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
size="s"
/>
}
allRules={rules}
/>
) : (
<EuiFlyout
Expand All @@ -170,7 +224,15 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
<EuiFlexItem grow={8}>
<EuiFlexGroup justifyContent="flexEnd" alignItems="center">
<EuiFlexItem grow={false}>
<EuiButton>Acknowledge</EuiButton>
<EuiButton
disabled={acknowledged || alertItem.state !== ALERT_STATE.ACTIVE}
onClick={() => {
this.setState({ acknowledged: true });
onAcknowledge([alertItem]);
}}
>
Acknowledge
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButtonIcon iconType="cross" iconSize="m" display="empty" onClick={onClose} />
Expand All @@ -180,23 +242,32 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
</EuiFlexGroup>
</EuiFlyoutHeader>
<EuiFlyoutBody>
{this.createTextDetailsGroup([
{createTextDetailsGroup([
{ label: 'Alert trigger name', content: trigger_name },
{ label: 'Alert status', content: state },
{ label: 'Alert severity', content: severity },
{
label: 'Alert severity',
content: parseAlertSeverityToOption(severity)?.label || DEFAULT_EMPTY_DATA,
},
])}
{this.createTextDetailsGroup([
{createTextDetailsGroup([
{ label: 'Start time', content: start_time },
{ label: 'Last updated time', content: last_notification_time },
{ label: '', content: '' },
])}
{createTextDetailsGroup([
{ label: 'Detector', content: detector.name },
{ label: '', content: '' },
{ label: '', content: '' },
])}
{this.createTextDetailsGroup([{ label: 'Detector', content: detector_id }])}

<EuiSpacer size={'xxl'} />

<ContentPanel title={`Findings (${finding_ids.length})`}>
<EuiBasicTable<Finding>
columns={this.createFindingTableColumns()}
items={this.state.findingItems}
loading={loading}
/>
</ContentPanel>
</EuiFlyoutBody>
Expand Down
Loading