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

Streamline rules request #281

Merged
Merged
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
45 changes: 11 additions & 34 deletions public/pages/Alerts/components/AlertFlyout/AlertFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Detector } from '../../../../../models/interfaces';
import { parseAlertSeverityToOption } from '../../../CreateDetector/components/ConfigureAlerts/utils/helpers';
import { Finding } from '../../../Findings/models/interfaces';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { RulesViewModelActor } from '../../../Rules/models/RulesViewModelActor';

export interface AlertFlyoutProps {
alertItem: AlertItem;
Expand All @@ -54,9 +55,13 @@ export interface AlertFlyoutState {
}

export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutState> {
private rulesViewModelActor: RulesViewModelActor;

constructor(props: AlertFlyoutProps) {
super(props);

this.rulesViewModelActor = new RulesViewModelActor(props.ruleService);

this.state = {
acknowledged: props.alertItem.state === ALERT_STATE.ACKNOWLEDGED,
findingItems: [],
Expand Down Expand Up @@ -98,50 +103,22 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
};

getRules = async () => {
const { notifications, ruleService } = this.props;
const { notifications } = this.props;
try {
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,
},
},
},
},
};

if (ruleIds.length > 0) {
const prePackagedResponse = await ruleService.getRules(true, body);
const customResponse = await ruleService.getRules(false, body);
const rulesResponse = await this.rulesViewModelActor.fetchRules({
_id: ruleIds,
});

const allRules: { [id: string]: RuleSource } = {};
if (prePackagedResponse.ok) {
prePackagedResponse.response.hits.hits.forEach(
(hit) => (allRules[hit._id] = hit._source)
);
} else {
errorNotificationToast(
notifications,
'retrieve',
'pre-packaged rules',
prePackagedResponse.error
);
}
if (customResponse.ok) {
customResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
} else {
errorNotificationToast(notifications, 'retrieve', 'custom rules', customResponse.error);
}
rulesResponse.forEach((hit) => (allRules[hit._id] = hit._source));

this.setState({ rules: allRules });
}
} catch (e: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export const UpdateDetectorRules: React.FC<UpdateDetectorRulesProps> = (props) =
const ruleItems = prePackagedRuleItems.concat(customRuleItems);

const onRuleDetails = (ruleItem: RuleItem) => {
console.log('onRuleDetails', ruleItem);
setFlyoutData(() => ({
title: ruleItem.name,
level: ruleItem.severity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default class FindingDetailsFlyout extends Component<
source: fullRule.source,
ruleInfo: {
_source: fullRule,
prePackaged: fullRule.prePackaged,
} as RuleItemInfoBase,
},
});
Expand Down
46 changes: 11 additions & 35 deletions public/pages/Findings/containers/Findings/Findings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { DetectorHit, RuleSource } from '../../../../../server/models/interfaces
import { NotificationsStart } from 'opensearch-dashboards/public';
import { DateTimeFilter } from '../../../Overview/models/interfaces';
import { ChartContainer } from '../../../../components/Charts/ChartContainer';
import { RulesViewModelActor } from '../../../Rules/models/RulesViewModelActor';

interface FindingsProps extends RouteComponentProps {
detectorService: DetectorsService;
Expand Down Expand Up @@ -98,10 +99,12 @@ export const groupByOptions = [

class Findings extends Component<FindingsProps, FindingsState> {
static contextType = CoreServicesContext;
private rulesViewModelActor: RulesViewModelActor;

constructor(props: FindingsProps) {
super(props);

this.rulesViewModelActor = new RulesViewModelActor(props.ruleService);
const {
dateTimeFilter = {
startTime: DEFAULT_DATE_RANGE.start,
Expand Down Expand Up @@ -192,42 +195,15 @@ class Findings extends Component<FindingsProps, FindingsState> {
};

getRules = async (ruleIds: string[]) => {
const { notifications, ruleService } = this.props;
const { notifications } = this.props;
try {
const body = {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
terms: {
_id: ruleIds,
},
},
},
},
};

const prePackagedResponse = await ruleService.getRules(true, body);
const customResponse = await ruleService.getRules(false, body);

const allRules: { [id: string]: any } = {};
if (prePackagedResponse.ok) {
prePackagedResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
} else {
errorNotificationToast(
notifications,
'retrieve',
'pre-packaged rules',
prePackagedResponse.error
);
}
if (customResponse.ok) {
customResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
} else {
errorNotificationToast(notifications, 'retrieve', 'custom rules', customResponse.error);
}
const rulesResponse = await this.rulesViewModelActor.fetchRules({
_id: ruleIds,
});

const allRules: { [id: string]: RuleSource } = {};
rulesResponse.forEach((hit) => (allRules[hit._id] = hit._source));

this.setState({ rules: allRules });
} catch (e) {
errorNotificationToast(notifications, 'retrieve', 'rules', e);
Expand Down
Loading