Skip to content

Commit

Permalink
Streamline rules request (#281)
Browse files Browse the repository at this point in the history
* [TASK] Add unit tests for util methods #268

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [FEATURE] Streamline rules request via RuleViewModelActor #278

Signed-off-by: Jovan Cvetkovic <[email protected]>

* [FEATURE] Streamline rules request via RuleViewModelActor #278

Signed-off-by: Jovan Cvetkovic <[email protected]>

Signed-off-by: Jovan Cvetkovic <[email protected]>
(cherry picked from commit 5508caa)
  • Loading branch information
jovancvetkovic3006 authored and github-actions[bot] committed Jan 9, 2023
1 parent 286ced2 commit 6433e26
Show file tree
Hide file tree
Showing 8 changed files with 654 additions and 90 deletions.
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
1 change: 1 addition & 0 deletions public/pages/Findings/components/FindingDetailsFlyout.tsx
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

0 comments on commit 6433e26

Please sign in to comment.