Skip to content

Commit 5508caa

Browse files
Streamline rules request (opensearch-project#281)
* [TASK] Add unit tests for util methods opensearch-project#268 Signed-off-by: Jovan Cvetkovic <[email protected]> * [FEATURE] Streamline rules request via RuleViewModelActor opensearch-project#278 Signed-off-by: Jovan Cvetkovic <[email protected]> * [FEATURE] Streamline rules request via RuleViewModelActor opensearch-project#278 Signed-off-by: Jovan Cvetkovic <[email protected]> Signed-off-by: Jovan Cvetkovic <[email protected]>
1 parent 6da7c82 commit 5508caa

File tree

8 files changed

+654
-90
lines changed

8 files changed

+654
-90
lines changed

public/pages/Alerts/components/AlertFlyout/AlertFlyout.tsx

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { Detector } from '../../../../../models/interfaces';
3333
import { parseAlertSeverityToOption } from '../../../CreateDetector/components/ConfigureAlerts/utils/helpers';
3434
import { Finding } from '../../../Findings/models/interfaces';
3535
import { NotificationsStart } from 'opensearch-dashboards/public';
36+
import { RulesViewModelActor } from '../../../Rules/models/RulesViewModelActor';
3637

3738
export interface AlertFlyoutProps {
3839
alertItem: AlertItem;
@@ -54,9 +55,13 @@ export interface AlertFlyoutState {
5455
}
5556

5657
export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutState> {
58+
private rulesViewModelActor: RulesViewModelActor;
59+
5760
constructor(props: AlertFlyoutProps) {
5861
super(props);
5962

63+
this.rulesViewModelActor = new RulesViewModelActor(props.ruleService);
64+
6065
this.state = {
6166
acknowledged: props.alertItem.state === ALERT_STATE.ACKNOWLEDGED,
6267
findingItems: [],
@@ -98,50 +103,22 @@ export class AlertFlyout extends React.Component<AlertFlyoutProps, AlertFlyoutSt
98103
};
99104

100105
getRules = async () => {
101-
const { notifications, ruleService } = this.props;
106+
const { notifications } = this.props;
102107
try {
103108
const { findingItems } = this.state;
104109
const ruleIds: string[] = [];
105110
findingItems.forEach((finding) => {
106111
finding.queries.forEach((query) => ruleIds.push(query.id));
107112
});
108-
const body = {
109-
from: 0,
110-
size: 5000,
111-
query: {
112-
nested: {
113-
path: 'rule',
114-
query: {
115-
terms: {
116-
_id: ruleIds,
117-
},
118-
},
119-
},
120-
},
121-
};
122113

123114
if (ruleIds.length > 0) {
124-
const prePackagedResponse = await ruleService.getRules(true, body);
125-
const customResponse = await ruleService.getRules(false, body);
115+
const rulesResponse = await this.rulesViewModelActor.fetchRules({
116+
_id: ruleIds,
117+
});
126118

127119
const allRules: { [id: string]: RuleSource } = {};
128-
if (prePackagedResponse.ok) {
129-
prePackagedResponse.response.hits.hits.forEach(
130-
(hit) => (allRules[hit._id] = hit._source)
131-
);
132-
} else {
133-
errorNotificationToast(
134-
notifications,
135-
'retrieve',
136-
'pre-packaged rules',
137-
prePackagedResponse.error
138-
);
139-
}
140-
if (customResponse.ok) {
141-
customResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
142-
} else {
143-
errorNotificationToast(notifications, 'retrieve', 'custom rules', customResponse.error);
144-
}
120+
rulesResponse.forEach((hit) => (allRules[hit._id] = hit._source));
121+
145122
this.setState({ rules: allRules });
146123
}
147124
} catch (e: any) {

public/pages/Findings/components/FindingDetailsFlyout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default class FindingDetailsFlyout extends Component<
9292
source: fullRule.source,
9393
ruleInfo: {
9494
_source: fullRule,
95+
prePackaged: fullRule.prePackaged,
9596
} as RuleItemInfoBase,
9697
},
9798
});

public/pages/Findings/containers/Findings/Findings.tsx

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { DetectorHit, RuleSource } from '../../../../../server/models/interfaces
5353
import { NotificationsStart } from 'opensearch-dashboards/public';
5454
import { DateTimeFilter } from '../../../Overview/models/interfaces';
5555
import { ChartContainer } from '../../../../components/Charts/ChartContainer';
56+
import { RulesViewModelActor } from '../../../Rules/models/RulesViewModelActor';
5657

5758
interface FindingsProps extends RouteComponentProps {
5859
detectorService: DetectorsService;
@@ -98,10 +99,12 @@ export const groupByOptions = [
9899

99100
class Findings extends Component<FindingsProps, FindingsState> {
100101
static contextType = CoreServicesContext;
102+
private rulesViewModelActor: RulesViewModelActor;
101103

102104
constructor(props: FindingsProps) {
103105
super(props);
104106

107+
this.rulesViewModelActor = new RulesViewModelActor(props.ruleService);
105108
const {
106109
dateTimeFilter = {
107110
startTime: DEFAULT_DATE_RANGE.start,
@@ -192,42 +195,15 @@ class Findings extends Component<FindingsProps, FindingsState> {
192195
};
193196

194197
getRules = async (ruleIds: string[]) => {
195-
const { notifications, ruleService } = this.props;
198+
const { notifications } = this.props;
196199
try {
197-
const body = {
198-
from: 0,
199-
size: 5000,
200-
query: {
201-
nested: {
202-
path: 'rule',
203-
query: {
204-
terms: {
205-
_id: ruleIds,
206-
},
207-
},
208-
},
209-
},
210-
};
211-
212-
const prePackagedResponse = await ruleService.getRules(true, body);
213-
const customResponse = await ruleService.getRules(false, body);
214-
215-
const allRules: { [id: string]: any } = {};
216-
if (prePackagedResponse.ok) {
217-
prePackagedResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
218-
} else {
219-
errorNotificationToast(
220-
notifications,
221-
'retrieve',
222-
'pre-packaged rules',
223-
prePackagedResponse.error
224-
);
225-
}
226-
if (customResponse.ok) {
227-
customResponse.response.hits.hits.forEach((hit) => (allRules[hit._id] = hit._source));
228-
} else {
229-
errorNotificationToast(notifications, 'retrieve', 'custom rules', customResponse.error);
230-
}
200+
const rulesResponse = await this.rulesViewModelActor.fetchRules({
201+
_id: ruleIds,
202+
});
203+
204+
const allRules: { [id: string]: RuleSource } = {};
205+
rulesResponse.forEach((hit) => (allRules[hit._id] = hit._source));
206+
231207
this.setState({ rules: allRules });
232208
} catch (e) {
233209
errorNotificationToast(notifications, 'retrieve', 'rules', e);

0 commit comments

Comments
 (0)