forked from cockpit-project/cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporting.jsx
442 lines (390 loc) · 17.3 KB
/
reporting.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* This file is part of Cockpit.
*
* Copyright (C) 2019 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import React from "react";
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
import { Card, CardBody, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js";
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js";
import { ExternalLinkAltIcon } from "@patternfly/react-icons";
import { show_modal_dialog } from "cockpit-components-dialog.jsx";
import { useInit } from "hooks";
import './reporting.scss';
const _ = cockpit.gettext;
const TaskState = Object.freeze({
READY: 0,
RUNNING: 1,
COMPLETED: 2,
ERROR: 3,
CANCELED: 4,
});
const PromptType = Object.freeze({
ASK: 0,
ASK_YES_NO: 1,
ASK_YES_NO_YESFOREVER: 2,
ASK_YES_NO_SAVE: 3,
ASK_PASSWORD: 4,
});
const ProblemState = Object.freeze({
REPORTABLE: 0,
REPORTING: 1,
REPORTED: 2,
UNREPORTABLE: 3,
});
const client = cockpit.dbus("org.freedesktop.problems", { superuser: "try" });
// For one-off fetches of properties to avoid setting up a cache for everything.
function get_problem_properties(problem) {
function executor(resolve, reject) {
client.wait().then(() => resolve(client));
}
return new Promise(executor)
.then(() => client.call(problem,
"org.freedesktop.DBus.Properties",
"GetAll", ["org.freedesktop.Problems2.Entry"]));
}
const FAFWorkflowRow = ({ problem }) => {
const [problemState, setProblemState] = React.useState(ProblemState.REPORTABLE);
const [process, setProcess] = React.useState(null);
const [reportLinks, setReportLinks] = React.useState([]);
const [message, setMessage] = React.useState("");
const updateStatusFromBus = () => {
get_problem_properties(problem.path)
.catch(exception => {
setProblemState(ProblemState.UNREPORTABLE);
console.error(cockpit.format("Getting properties for problem $0 failed: $1", problem.path, exception));
})
.then((properties) => {
if (!properties) {
return;
}
if (!properties[0].CanBeReported.v) {
setProblemState(ProblemState.UNREPORTABLE);
return;
}
const reportLinks = [];
let reported = false;
for (const report of properties[0].Reports.v) {
if (report[0] === "ABRT Server") {
if ("URL" in report[1]) {
reportLinks.push(report[1].URL.v.v);
}
reported = true;
}
}
if (reported) {
setProblemState(ProblemState.REPORTED);
setReportLinks(reportLinks);
}
});
};
useInit(() => {
updateStatusFromBus();
});
const onCancelButtonClick = _event => process.close("canceled");
const onReportButtonClick = _event => {
setProblemState(ProblemState.UNREPORTABLE);
const process = cockpit.spawn(["reporter-ureport", "-d", problem.ID],
{
err: "out",
superuser: "true",
})
.stream((data) => setMessage(data))
.then(() => setProblemState(ProblemState.REPORTED))
.catch(exception => {
setProblemState(ProblemState.REPORTABLE);
if (exception.exit_signal != null) {
console.error(cockpit.format("reporter-ureport was killed with signal $0", exception.exit_signal));
}
})
.finally(() => updateStatusFromBus());
setProblemState(ProblemState.REPORTING);
setProcess(process);
};
return <WorkflowRow label={_("Report to ABRT Analytics")}
message={message}
onCancelButtonClick={onCancelButtonClick}
onReportButtonClick={onReportButtonClick}
problemState={problemState}
reportLinks={reportLinks}
/>;
};
const BusWorkflowRow = ({ problem, client, workflow }) => {
const [message, setMessage] = React.useState("");
const [problemState, setProblemState] = React.useState(ProblemState.REPORTABLE);
const [reportLinks, setReportLinks] = React.useState([]);
const [task, setTask] = React.useState(null);
const label = workflow[1];
const inputRef = React.createRef();
const updateStatusFromBus = () => {
const on_get_properties = properties => {
if (!properties[0].CanBeReported.v) {
setProblemState(ProblemState.UNREPORTABLE);
return;
}
const reportLinks = [];
let reported = false;
for (const report of properties[0].Reports.v) {
if (!("WORKFLOW" in report[1])) {
continue;
}
if (workflow[0] !== report[1].WORKFLOW.v.v) {
continue;
}
if (report[0] === "ABRT Server" || report[0] === "uReport") {
continue;
}
if ("URL" in report[1]) {
reportLinks.push(report[1].URL.v.v);
}
reported = true;
}
if (reported) {
setProblemState(ProblemState.REPORTED);
setReportLinks(reportLinks);
}
};
const on_get_properties_rejected = exception => {
setProblemState(ProblemState.UNREPORTABLE);
console.error(cockpit.format("Getting properties for problem $0 failed: $1", problem.path, exception));
};
get_problem_properties(problem.path).then(on_get_properties, on_get_properties_rejected);
};
useInit(() => {
updateStatusFromBus();
});
const createTask = client => {
return client.call("/org/freedesktop/reportd/Service",
"org.freedesktop.reportd.Service", "CreateTask",
[workflow[0], problem.path])
.then(result => onCreateTask(result[0], client));
};
const onCancelButtonClick = _event => task.Cancel();
const onCreateTask = (object_path, client) => {
const task_proxy = client.proxy("org.freedesktop.reportd.Task", object_path);
task_proxy
.wait()
.then((_object_path) => {
task_proxy.addEventListener("changed", (_event, data) => {
switch (data.Status) {
case TaskState.RUNNING:
// To avoid a needless D-Bus round trip.
return;
case TaskState.CANCELED:
setMessage(_("Reporting was canceled"));
// falls through
case TaskState.ERROR:
setProblemState(ProblemState.REPORTABLE);
break;
case TaskState.COMPLETED:
setProblemState(ProblemState.REPORTED);
break;
default:
break;
}
updateStatusFromBus();
});
task_proxy.addEventListener("Prompt", (_event, object_path, message, type) => {
setMessage(_("Waiting for input…"));
const task_prompt = client.proxy("org.freedesktop.reportd.Task.Prompt", object_path);
const props = {
body: <p>{message}</p>,
};
const footerProps = {
actions: [],
cancel_clicked: () => {
task_proxy.Cancel();
},
};
switch (type) {
case PromptType.ASK:
case PromptType.ASK_PASSWORD:
props.body = (
<div>
<p>{message}</p>
<input className="full-width"
ref={inputRef}
type={type == PromptType.ASK_PASSWORD ? "password" : "text"} />
</div>
);
footerProps.actions.push(
{
caption: _("Send"),
clicked: () => {
return task_prompt.wait().then(() => {
task_prompt.Input = inputRef.current.value;
task_prompt.Commit();
});
},
style: "primary",
}
);
break;
case PromptType.ASK_YES_NO_YESFOREVER:
case PromptType.ASK_YES_NO:
case PromptType.ASK_YES_NO_SAVE:
footerProps.actions.push(
{
caption: _("Yes"),
clicked: () => {
return task_prompt.wait().then(() => {
task_prompt.Response = true;
task_prompt.Commit();
});
},
},
{
caption: _("No"),
clicked: (callback) => {
return task_prompt.wait().then(() => {
task_prompt.Response = false;
task_prompt.Commit();
});
},
},
);
}
show_modal_dialog(props, footerProps);
});
task_proxy.addEventListener("Progress", (_event, message) => {
if (/^\.+$/.exec(message) === null) {
// abrt-retrace-client starts printing dots if the last message it receives is repeated
setMessage(message);
}
});
setTask(task_proxy);
task_proxy.Start().catch(ex => {
/* GLib encodes errors for transport over the wire,
* but we don’t have a good way of decoding them without calling into GIO.
*
* https://developer-old.gnome.org/gio/stable/gio-GDBusError.html#g-dbus-error-encode-gerror
*
* 19 is G_IO_ERROR_CANCELLED. No need to handle user cancellations.
*/
if (/Code19/.exec(ex.name) != null) {
return;
}
console.error(cockpit.format("reportd task for workflow $0 did not finish: $1", workflow[0], (ex.problem || ex.message)));
setMessage(_("Reporting failed"));
});
})
.catch(ex => console.error(cockpit.format("Setting up a D-Bus proxy for $0 failed: $1", object_path, ex)));
};
const onReportButtonClick = (_event) => {
setMessage(_("Waiting to start…"));
setProblemState(ProblemState.REPORTING);
client.wait()
.catch(exception => console.error(cockpit.format("Channel for reportd D-Bus client closed: $0", exception.problem || exception.message)))
.then(() => createTask(client))
.catch(exception => {
const newMessage = cockpit.format("reportd task could not be created: $0", (exception.problem || exception.message));
setMessage(newMessage);
setProblemState(ProblemState.REPORTABLE);
console.error(newMessage);
});
};
return <WorkflowRow label={label}
message={message}
onCancelButtonClick={onCancelButtonClick}
onReportButtonClick={onReportButtonClick}
problemState={problemState}
reportLinks={reportLinks}
/>;
};
const WorkflowRow = ({ message, problemState, reportLinks, label, onReportButtonClick, onCancelButtonClick }) => {
let status = message;
if (problemState === ProblemState.REPORTED) {
if (reportLinks.length === 1) {
status = (
<a href={reportLinks[0]} rel="noopener noreferrer" target="_blank">
<ExternalLinkAltIcon />{_("View report")}
</a>
);
} else if (reportLinks.length > 1) {
const reportLinksComps = reportLinks.map((reportLink, index) => [
index > 0 && ", ",
<a key={index.toString()} href={reportLink} rel="noopener noreferrer" target="_blank">
<ExternalLinkAltIcon /> {index + 1}
</a>
]);
status = <p>{_("Reports:")} {reportLinksComps}</p>;
} else {
status = _("Reported; no links available");
}
}
let button = null;
if (problemState === ProblemState.REPORTING) {
button = (
<Button key={"cancel_" + label}
variant="secondary"
onClick={onCancelButtonClick}>
{_("Cancel")}
</Button>
);
} else {
button = (
<Button key={"report_" + label}
variant="primary"
isDisabled={problemState !== ProblemState.REPORTABLE}
onClick={problemState === ProblemState.REPORTABLE ? onReportButtonClick : undefined}>
{_("Report")}
</Button>
);
}
return (
<Split hasGutter>
<SplitItem>{label}</SplitItem>
<SplitItem isFilled>
{problemState === ProblemState.REPORTING && <Spinner size="md" /> }
{status}
</SplitItem>
<SplitItem>{button}</SplitItem>
</Split>
);
};
const reportd_client = cockpit.dbus("org.freedesktop.reportd", { superuser: "try" });
export const ReportingTable = ({ problem }) => {
const [workflows, setWorkflows] = React.useState([]);
useInit(() => {
reportd_client
.wait()
.then(() => getWorkflows(reportd_client),
exception => console.error(cockpit.format("Channel for reportd D-Bus client closed: $0", exception.problem || exception.message)));
});
const getWorkflows = client => {
client.call("/org/freedesktop/reportd/Service", "org.freedesktop.reportd.Service", "GetWorkflows", [problem.path])
.then((args, _options) => setWorkflows(args[0]),
exception => console.error(cockpit.format("Failed to get workflows for problem $0: $1", problem.path, (exception.problem || exception.message))));
};
return (
<Card>
<CardTitle component="h2">{_("Crash reporting")}</CardTitle>
<CardBody>
<FAFWorkflowRow problem={problem} />
{
workflows.map((workflow, index) => [
<BusWorkflowRow key={index.toString()}
problem={problem}
client={reportd_client}
workflow={workflow} />
])
}
</CardBody>
</Card>
);
};