-
Notifications
You must be signed in to change notification settings - Fork 352
/
AlertTableRow.jsx
327 lines (302 loc) · 9.78 KB
/
AlertTableRow.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
/* eslint-disable react/no-did-update-set-state */
import React from 'react';
import PropTypes from 'prop-types';
import { Button, FormGroup, Input, Label } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faStar as faStarSolid,
faUser,
} from '@fortawesome/free-solid-svg-icons';
import { faStar as faStarRegular } from '@fortawesome/free-regular-svg-icons';
import { createQueryParams } from '../../helpers/url';
import { getStatus, getGraphsURL, modifyAlert, formatNumber } from '../helpers';
import SimpleTooltip from '../../shared/SimpleTooltip';
import ProgressBar from '../ProgressBar';
import {
alertStatusMap,
phDefaultTimeRangeValue,
phTimeRanges,
} from '../constants';
export default class AlertTableRow extends React.Component {
constructor(props) {
super(props);
this.state = {
starred: this.props.alert.starred,
checkboxSelected: false,
};
}
componentDidUpdate(prevProps) {
const { selectedAlerts, alert } = this.props;
// reset alert checkbox when an action is taken in the AlertActionPanel
// (it resets selectedAlerts) or an individual alert has been deselected
// and removed from selectedAlerts
if (prevProps.selectedAlerts !== selectedAlerts) {
if (!selectedAlerts.length) {
this.setState({ checkboxSelected: false });
} else {
const index = selectedAlerts.findIndex(item => item.id === alert.id);
this.setState({ checkboxSelected: index !== -1 });
}
}
}
getTimeRange = () => {
const { alertSummary } = this.props;
const defaultTimeRange =
alertSummary.repository === 'mozilla-beta'
? 7776000
: phDefaultTimeRangeValue;
const timeRange = Math.max(
defaultTimeRange,
phTimeRanges
.map(time => time.value)
.find(
value => Date.now() / 1000.0 - alertSummary.push_timestamp <= value,
),
);
// default value of one year, for one a push_timestamp exceeds the one year value slightly
return timeRange || 31536000;
};
toggleStar = async () => {
const { starred } = this.state;
const { alert } = this.props;
const updatedStar = {
starred: !starred,
};
// passed as prop only for testing purposes
const { data, failureStatus } = await this.props.modifyAlert(
alert,
updatedStar,
);
if (failureStatus) {
return this.props.updateViewState({
errorMessages: [`Failed to update alert ${alert.id}: ${data}`],
});
}
this.setState(updatedStar);
};
getReassignment = alert => {
let text = 'to';
let alertId = alert.related_summary_id;
if (alert.related_summary_id === this.props.alertSummary.id) {
text = 'from';
alertId = alert.summary_id;
}
return (
<span>
{` ${text} `}
<a
href={`#/alerts?id=${alertId}`}
rel="noopener noreferrer"
className="text-info"
>{`alert #${alertId}`}</a>
</span>
);
};
updateCheckbox = () => {
const { updateSelectedAlerts, selectedAlerts, alert } = this.props;
const { checkboxSelected } = this.state;
const index = selectedAlerts.findIndex(item => item.id === alert.id);
if (checkboxSelected && index === -1) {
return updateSelectedAlerts({
selectedAlerts: [...selectedAlerts, alert],
});
}
if (!checkboxSelected && index !== -1) {
selectedAlerts.splice(index, 1);
return updateSelectedAlerts({ selectedAlerts, allSelected: false });
}
};
getTitleText = (alert, alertStatus) => {
const { repository, framework, id } = this.props.alertSummary;
let statusColor = '';
let textEffect = '';
if (alertStatus === 'invalid') {
statusColor = 'text-danger';
}
if (alertStatus === 'untriaged') {
statusColor = 'text-success';
}
if (
alertStatus === 'invalid' ||
(alert.related_summary_id && alert.related_summary_id !== id)
) {
textEffect = 'strike-through';
}
const timeRange = this.getTimeRange();
return (
<span>
<span className={textEffect} id={`alert ${alert.id} title`}>
{alert.title}
</span>{' '}
(<span className={statusColor}>{alertStatus}</span>
{alert.related_summary_id && this.getReassignment(alert)}){' '}
<span className="result-links">
<a
href={getGraphsURL(alert, timeRange, repository, framework)}
target="_blank"
rel="noopener noreferrer"
>
{' '}
graph
</a>
{alert.series_signature.has_subtests && (
<a
href={this.getSubtestsURL()}
target="_blank"
rel="noopener noreferrer"
>
{' '}
· subtests
</a>
)}
</span>
</span>
);
};
// arbitrary scale from 0-20% multiplied by 5, capped
// at 100 (so 20% regression === 100% bad)
getCappedMagnitude = percent => Math.min(Math.abs(percent) * 5, 100);
getSubtestsURL = () => {
const { alert, alertSummary } = this.props;
const urlParameters = {
framework: alertSummary.framework,
originalProject: alertSummary.repository,
originalSignature: alert.series_signature.id,
newProject: alertSummary.repository,
newSignature: alert.series_signature.id,
originalRevision: alertSummary.prev_push_revision,
newRevision: alertSummary.revision,
};
return `#/comparesubtest${createQueryParams(urlParameters)}`;
};
render() {
const { user, alert, alertSummary } = this.props;
const { starred, checkboxSelected } = this.state;
const alertStatus = getStatus(alert.status, alertStatusMap);
const tooltipText = alert.classifier_email
? `Classified by ${alert.classifier_email}`
: 'Classified automatically';
return (
<tr
className={
alertSummary.notes ? 'border-top border-left border-right' : 'border'
}
data-testid={alert.id}
>
<td className="table-width-xs px-1">
<FormGroup check className="ml-2 pl-4">
<Label hidden>alert {alert.id} title</Label>
<Input
aria-label={`alert ${alert.id} title`}
data-testid={`alert ${alert.id} checkbox`}
type="checkbox"
disabled={!user.isStaff}
checked={checkboxSelected}
onChange={() =>
this.setState(
{ checkboxSelected: !checkboxSelected },
this.updateCheckbox,
)
}
/>
</FormGroup>
</td>
<td className="px-0">
<Button
color="black"
aria-label={
starred
? 'Remove bookmark from this Alert'
: 'Bookmark this Alert'
}
className={
starred
? 'visible border border-0 bg-transparent'
: 'border border-0 bg-transparent'
}
data-testid={`alert ${alert.id.toString()} star`}
onClick={this.toggleStar}
>
<FontAwesomeIcon
title={starred ? 'starred' : 'not starred'}
icon={starred ? faStarSolid : faStarRegular}
/>
</Button>
</td>
<td className="text-left">
{alertStatus !== 'untriaged' ? (
<SimpleTooltip
text={this.getTitleText(alert, alertStatus)}
tooltipText={tooltipText}
/>
) : (
this.getTitleText(alert, alertStatus)
)}
</td>
<td className="table-width-md">{formatNumber(alert.prev_value)}</td>
<td className="table-width-sm">
<span
className={alert.is_regression ? 'text-danger' : 'text-success'}
>
{alert.prev_value < alert.new_value && <span><</span>}
{alert.prev_value > alert.new_value && <span>></span>}
</span>
</td>
<td className="table-width-md">{formatNumber(alert.new_value)}</td>
<td className="table-width-md">
<SimpleTooltip
textClass="detail-hint"
text={`${alert.amount_pct}%`}
tooltipText={`Absolute difference: ${alert.amount_abs}`}
/>
</td>
<td className="table-width-lg">
<ProgressBar
magnitude={this.getCappedMagnitude(alert.amount_pct)}
regression={alert.is_regression}
color={!alert.is_regression ? 'success' : 'danger'}
/>
</td>
<td className="table-width-sm">
<SimpleTooltip
textClass="detail-hint"
text={
alert.manually_created ? (
<FontAwesomeIcon
title="Alert created by a Sheriff"
icon={faUser}
/>
) : (
formatNumber(alert.t_value)
)
}
tooltipText={
alert.manually_created
? 'Alert created by a Sheriff'
: 'Confidence value as calculated by Perfherder alerts. Note that this is NOT the same as the calculation used in the compare view'
}
/>
</td>
</tr>
);
}
}
AlertTableRow.propTypes = {
alertSummary: PropTypes.shape({
repository: PropTypes.string,
framework: PropTypes.number,
id: PropTypes.number,
}).isRequired,
user: PropTypes.shape({}),
alert: PropTypes.shape({
starred: PropTypes.bool,
}).isRequired,
updateSelectedAlerts: PropTypes.func.isRequired,
selectedAlerts: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
updateViewState: PropTypes.func.isRequired,
modifyAlert: PropTypes.func,
};
AlertTableRow.defaultProps = {
user: null,
modifyAlert,
};