-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAlertRuleList.jsx
executable file
·383 lines (352 loc) · 17.8 KB
/
AlertRuleList.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (C) 2018 Airbus CyberSecurity (SAS)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import PropTypes from 'prop-types';
import React from 'react';
import Reflux from 'reflux';
import createReactClass from 'create-react-class';
import AlertRuleStore from 'wizard/stores/AlertRuleStore';
import AlertRuleActions from 'wizard/actions/AlertRuleActions';
// TODO remove imports from react-bootstrap everywhere, and use only components/bootstrap?
import { Tooltip } from 'react-bootstrap';
import { DataTable, IfPermitted, OverlayElement, Spinner, Timestamp } from 'components/common';
import { Button } from 'components/bootstrap';
import PermissionsMixin from 'util/PermissionsMixin';
import Routes from 'routing/Routes';
import { LinkContainer } from 'react-router-bootstrap';
import AlertForm from './AlertForm';
import DateTime from 'logic/datetimes/DateTime';
import { FormattedMessage } from 'react-intl';
import AlertRuleText from './AlertRuleText'
import ButtonToEventDefinition from './buttons/ButtonToEventDefinition';
import ButtonToNotification from './buttons/ButtonToNotification';
import StreamsStore from 'stores/streams/StreamsStore';
const AlertRuleList = createReactClass({
displayName: 'AlertRuleList',
mixins: [Reflux.connect(AlertRuleStore), PermissionsMixin],
propTypes: {
field_order: PropTypes.array.isRequired,
},
contextTypes: {
intl: PropTypes.object.isRequired,
},
componentWillMount() {
const fieldsTitle = {
title: this.context.intl.formatMessage({id: "wizard.title", defaultMessage: "Title"}),
severity: this.context.intl.formatMessage({id: "wizard.severity", defaultMessage: "Severity"}),
description: this.context.intl.formatMessage({id: "wizard.fieldDescription", defaultMessage: "Description"}),
created: this.context.intl.formatMessage({id: "wizard.created", defaultMessage: "Created"}),
lastModified: this.context.intl.formatMessage({id: "wizard.lastModified", defaultMessage: "Last Modified"}),
user: this.context.intl.formatMessage({id: "wizard.user", defaultMessage: "User"}),
alerts: this.context.intl.formatMessage({id: "wizard.alerts", defaultMessage: "Alerts"}),
status: this.context.intl.formatMessage({id: "wizard.status", defaultMessage: "Status"}),
actions: this.context.intl.formatMessage({id: "wizard.actions", defaultMessage: "Actions"}),
rule: this.context.intl.formatMessage({id: "wizard.rule", defaultMessage: "Rule"})
};
const messages = {
infoDelete: this.context.intl.formatMessage({id: "wizard.buttonInfoDelete", defaultMessage: "Delete this alert rule"}),
infoUpdate: this.context.intl.formatMessage({id: "wizard.buttonInfoUpdate", defaultMessage: "Edit this alert rule"}),
infoEnable: this.context.intl.formatMessage({id: "wizard.buttonInfoEnable", defaultMessage: "Enable this alert rule"}),
infoDisable: this.context.intl.formatMessage({id: "wizard.buttonInfoDisable", defaultMessage: "Disable this alert rule"}),
infoClone: this.context.intl.formatMessage({id: "wizard.buttonInfoClone", defaultMessage: "Clone this alert rule"}),
createAlert: this.context.intl.formatMessage({id: "wizard.createAlert", defaultMessage: "Create alert rule"}),
importAlert: this.context.intl.formatMessage({id: "wizard.importAlert", defaultMessage: "Import alert rules"}),
exportAlert: this.context.intl.formatMessage({id: "wizard.exportAlert", defaultMessage :"Export alert rules"}),
confirmDeletion: this.context.intl.formatMessage({id: "wizard.confirmDeletion", defaultMessage :"Do you really want to delete the alert rule"}),
confirmDisable: this.context.intl.formatMessage({id: "wizard.confirmDisable", defaultMessage :"Do you really want to disable the alert rule"}),
};
this.setState({fieldsTitle:fieldsTitle});
this.setState({messages:messages});
this.list();
},
list() {
AlertRuleActions.listWithData().then(newAlerts => {
this.setState({alerts: newAlerts});
});
},
deleteAlert(name) {
AlertRuleActions.deleteByName(name);
},
_deleteAlertFunction(name) {
return () => {
if (window.confirm(`${this.state.messages.confirmDeletion} "${name}" ?`)) {
this.deleteAlert(name);
}
};
},
_onResume(stream, stream2) {
return () => {
StreamsStore.resume(stream, response => response).finally(() => this.list());
if (stream2 !== null && stream2 !== '') {
StreamsStore.resume(stream2, response => response).finally(() => this.list());
}
}
},
_onPause(name, stream, stream2) {
return () => {
if (window.confirm(`${this.state.messages.confirmDisable} "${name}" ?`)) {
StreamsStore.pause(stream, response => response).finally(() => this.list());
if (stream2 !== null && stream2 !== '') {
StreamsStore.pause(stream2, response => response);
}
}
}
},
_headerCellFormatter(header) {
let formattedHeaderCell;
switch (header.toLocaleLowerCase()) {
case '':
formattedHeaderCell = <th className="user-type">{header}</th>;
break;
case 'actions':
formattedHeaderCell = <th className="actions">{header}</th>;
break;
default:
formattedHeaderCell = <th>{header}</th>;
}
return formattedHeaderCell;
},
_onClone(name) {
return () => {
this.refs.cloneForm.open(name);
}
},
_onCloneSubmit(name, title, description) {
AlertRuleActions.get(name).then(rule => {
const newRule = {
title: title,
description: description,
severity: rule.severity,
condition_type: rule.condition_type,
condition_parameters: rule.condition_parameters,
stream: rule.stream,
second_stream: rule.second_stream
}
AlertRuleActions.create(newRule).finally(() => this.list());
});
},
_availableSeverityTypes() {
return [
{value: 'info', label: <FormattedMessage id= "wizard.info" defaultMessage= "Info" />},
{value: 'low', label: <FormattedMessage id= "wizard.low" defaultMessage= "Low" />},
{value: 'medium', label: <FormattedMessage id= "wizard.medium" defaultMessage= "Medium" />},
{value: 'high', label: <FormattedMessage id= "wizard.high" defaultMessage= "High" />},
];
},
_getSeverityType(type) {
return this._availableSeverityTypes().filter((t) => t.value === type)[0].label;
},
_availableFieldName() {
return [
{value: 'Severity', label: this.state.fieldsTitle.severity},
{value: 'Description', label: this.state.fieldsTitle.description},
{value: 'Created', label: this.state.fieldsTitle.created},
{value: 'Last Modified', label: this.state.fieldsTitle.lastModified},
{value: 'User', label: this.state.fieldsTitle.user},
{value: 'Alerts', label: this.state.fieldsTitle.alerts},
{value: 'Status', label: this.state.fieldsTitle.status},
{value: 'Rule', label: this.state.fieldsTitle.rule},
];
},
_getFieldName(field) {
return this._availableFieldName().filter((t) => t.value === field)[0].label;
},
_alertInfoFormatter(alert) {
let alertValid;
let colorValid;
let streamID;
if (alert.condition_parameters === null || alert.stream === null) {
alertValid = false;
colorValid = "#F7230C";
streamID = '';
} else {
alertValid = true;
colorValid = "#000000";
streamID = alert.stream.id;
if (alert.disabled) {
colorValid = "#ABABAB";
}
}
let streamId2 = null;
if (alert.second_stream) {
streamId2 = alert.second_stream.id;
}
const deleteAction = (
<IfPermitted permissions="wizard_alerts_rules:delete">
<button id="delete-alert" type="button" className="btn btn-md btn-primary"
title={this.state.messages.infoDelete} onClick={this._deleteAlertFunction(alert.title)}>
<FormattedMessage id ="wizard.delete" defaultMessage="Delete" />
</button>
</IfPermitted>
);
const updateAlert = (
<IfPermitted permissions="wizard_alerts_rules:read">
<LinkContainer to={Routes.pluginRoute('WIZARD_UPDATEALERT_ALERTRULETITLE')(alert.title.replace(/\//g, '%2F'))} disabled={!alertValid}>
<Button bsStyle="info" type="submit" title={this.state.messages.infoUpdate} >
<FormattedMessage id ="wizard.edit" defaultMessage="Edit" />
</Button>
</LinkContainer>
</IfPermitted>
);
let toggleStreamLink;
if (alert.disabled) {
toggleStreamLink = (
<Button bsStyle="success" onClick={this._onResume(streamID, streamId2)} disabled={!alertValid}
title={this.state.messages.infoEnable} style={{whiteSpace: 'pre'}} >
<FormattedMessage id ="wizard.enable" defaultMessage="Enable " />
</Button>
);
} else {
toggleStreamLink = (
<Button bsStyle="primary" onClick={this._onPause(alert.title, streamID, streamId2)} disabled={!alertValid}
title={this.state.messages.infoDisable} >
<FormattedMessage id ="wizard.disable" defaultMessage="Disable" />
</Button>
);
}
const cloneAlert = (
<Button id="clone-alert" type="button" bsStyle="info" onClick={this._onClone(alert.title)} disabled={!alertValid}
title={this.state.messages.infoClone} >
<FormattedMessage id ="wizard.clone" defaultMessage="Clone" />
</Button>
);
const actions = (
<div className="alert-actions pull-left">
{updateAlert}{' '}
<ButtonToEventDefinition target={alert.condition} disabled={!alertValid} />{' '}
<ButtonToNotification target={alert.notification} disabled={!alertValid} />{' '}
{cloneAlert}{' '}
{deleteAction}{' '}
{toggleStreamLink}{' '}
</div>
);
const tooltipAlertCount = (
<Tooltip id="default-alert-count-tooltip">
<FormattedMessage id ="wizard.tooltipAlerts" defaultMessage="The daily throughput and the total number of triggered alerts since the last modification of the alert rule" />
</Tooltip>);
const tooltipUser = (
<Tooltip id="default-user-tooltip">
<FormattedMessage id ="wizard.tooltipUser" defaultMessage="The last user who modified the alert rule" />
</Tooltip>);
let nbDays = (DateTime.now() - DateTime.parseFromString(alert.last_modified).toMoment()) / 1000 / 60 / 60 / 24;
let nbAlertDay = Math.round(alert.alert_count / Math.ceil(nbDays));
let tabFields = [<td className="limited">{alert.title}</td>];
this.props.field_order.map((field) => {
if (field.enabled) {
switch (field.name) {
case 'Severity':
tabFields.push(<td className="limited">{alert.severity ? this._getSeverityType(alert.severity) : ''}</td>);
break;
case 'Description':
tabFields.push(<td className="limited"><span style={{whiteSpace: 'pre-line'}}>{alert.description}</span></td>);
break;
case 'Created':
tabFields.push(<td className="limited"><Timestamp dateTime={DateTime.parseFromString(alert.created_at).toString(DateTime.Formats.DATETIME)} relative/></td>);
break;
case 'Last Modified':
tabFields.push(<td className="limited"><Timestamp dateTime={DateTime.parseFromString(alert.last_modified).toString(DateTime.Formats.DATETIME)} relative/>
</td>);
break;
case 'User':
tabFields.push(<td className="limited">
<OverlayElement overlay={tooltipUser} placement="top" useOverlay={true}
trigger={['hover', 'focus']}>
{alert.creator_user_id}
</OverlayElement>
</td>);
break;
case 'Alerts':
tabFields.push(<td className="limited">
<OverlayElement overlay={tooltipAlertCount} placement="top" useOverlay={true}
trigger={['hover', 'focus']}>
<div><FormattedMessage id ="wizard.manualCount" defaultMessage="{alertDay} alerts/day ({alertCount} total)"
values={{alertDay: nbAlertDay, alertCount: alert.alert_count}}/></div>
</OverlayElement>
</td>);
break;
case 'Status':
if (alertValid) {
tabFields.push(<td className="limited">{alert.disabled ?
<span style={{backgroundColor: 'orange', color: 'white'}}><FormattedMessage id ="wizard.disabled" defaultMessage="Disabled" /></span> :
<FormattedMessage id ="wizard.enabled" defaultMessage="Enabled" />}</td>);
} else {
tabFields.push(<td className="limited"><FormattedMessage id ="wizard.corrupted" defaultMessage="Corrupted" /></td>);
}
break;
case 'Rule':
tabFields.push(<td className="limited"><AlertRuleText alert={alert} /></td>);
break;
default:
break;
}
}
});
return (
<tr key={alert.title} style={{color:colorValid}}>
{tabFields}
<td style={{whiteSpace: 'nowrap'}}>{actions}</td>
</tr>
);
},
render() {
const filterKeys = ['title', 'severity', 'created_at', 'last_modified', 'creator_user_id'];
let headers = [this.state.fieldsTitle.title];
this.props.field_order.map((field) => {
if (field.enabled) {
headers.push(this._getFieldName(field.name));
}
});
headers.push(this.state.fieldsTitle.actions);
if (this.state.alerts) {
return (
<div>
<div className="alert-actions pull-right">
<LinkContainer to={Routes.pluginRoute('WIZARD_NEWALERT')}>
<Button bsStyle="success" type="submit" title={this.state.messages.createAlert}>
<FormattedMessage id ="wizard.create" defaultMessage="Create" />
</Button>
</LinkContainer>
{' '}
<LinkContainer to={Routes.pluginRoute('WIZARD_IMPORTALERT')}>
<Button bsStyle="success" type="submit" title={this.state.messages.importAlert}>
<FormattedMessage id ="wizard.import" defaultMessage="Import" />
</Button>
</LinkContainer>
{' '}
<LinkContainer to={Routes.pluginRoute('WIZARD_EXPORTALERT')}>
<Button bsStyle="success" type="submit" title={this.state.messages.exportAlert}>
<FormattedMessage id ="wizard.export" defaultMessage="Export" />
</Button>
</LinkContainer>
</div>
<DataTable id="alert-list"
className="table-hover"
headers={headers}
headerCellFormatter={this._headerCellFormatter}
sortByKey={"title"}
rows={this.state.alerts}
filterBy="title"
dataRowFormatter={this._alertInfoFormatter}
filterLabel={<FormattedMessage id ="wizard.filter" defaultMessage="Filter alert rules" />}
filterKeys={filterKeys} />
<AlertForm ref="cloneForm" onSubmit={this._onCloneSubmit} />
</div>
);
}
return <Spinner/>
},
});
export default AlertRuleList;