Debian Codegraph @

pkg/storaged/jobs-panel.jsx

raw
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8
9 import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
10 import { DataList, DataListCell, DataListItem, DataListItemCells, DataListItemRow } from "@patternfly/react-core/dist/esm/components/DataList/index.js";
11 import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js";
12
13 import { StorageButton } from "./storage-controls.jsx";
14 import { block_name, mdraid_name, lvol_name, format_delay } from "./utils.js";
15
16 const _ = cockpit.gettext;
17
18 /* Human readable descriptions of the symbolic "Operation"
19 * property of job objects. These are from the storaged
20 * documentation at
21 *
22 * https://storaged.org/doc/udisks2-api/latest/gdbus-org.freedesktop.UDisks2.Job.html
23 */
24
25 const descriptions = {
26 'ata-smart-selftest': _("SMART self-test of $target"),
27 'drive-eject': _("Ejecting $target"),
28 'encrypted-unlock': _("Unlocking $target"),
29 'encrypted-lock': _("Locking $target"),
30 'encrypted-modify': _("Modifying $target"),
31 'encrypted-resize': _("Resizing $target"),
32 'swapspace-start': _("Starting swapspace $target"),
33 'swapspace-stop': _("Stopping swapspace $target"),
34 'filesystem-mount': _("Mounting $target"),
35 'filesystem-unmount': _("Unmounting $target"),
36 'filesystem-modify': _("Modifying $target"),
37 'filesystem-resize': _("Resizing $target"),
38 'filesystem-check': _("Checking $target"),
39 'filesystem-repair': _("Repairing $target"),
40 'format-erase': _("Erasing $target"),
41 'format-mkfs': _("Creating filesystem on $target"),
42 'loop-setup': _("Setting up loop device $target"),
43 'partition-modify': _("Modifying $target"),
44 'partition-delete': _("Deleting $target"),
45 'partition-create': _("Creating partition $target"),
46 cleanup: _("Cleaning up for $target"),
47 'ata-secure-erase': _("Securely erasing $target"),
48 'ata-enhanced-secure-erase': _("Very securely erasing $target"),
49 'md-raid-stop': _("Stopping MDRAID device $target"),
50 'md-raid-start': _("Starting MDRAID device $target"),
51 'md-raid-fault-device': _("Marking $target as faulty"),
52 'md-raid-remove-device': _("Removing $target from MDRAID device"),
53 'md-raid-create': _("Creating MDRAID device $target"),
54 'mdraid-check-job': _("Checking MDRAID device $target"),
55 'mdraid-repair-job': _("Checking and repairing MDRAID device $target"),
56 'mdraid-recover-job': _("Recovering MDRAID device $target"),
57 'mdraid-sync-job': _("Synchronizing MDRAID device $target"),
58 'lvm-lvol-delete': _("Deleting $target"),
59 'lvm-lvol-activate': _("Activating $target"),
60 'lvm-lvol-deactivate': _("Deactivating $target"),
61 'lvm-lvol-snapshot': _("Creating snapshot of $target"),
62 'lvm-vg-create': _("Creating LVM2 volume group $target"),
63 'lvm-vg-delete': _("Deleting LVM2 volume group $target"),
64 'lvm-vg-add-device': _("Adding physical volume to $target"),
65 'lvm-vg-rem-device': _("Removing physical volume from $target"),
66 'lvm-vg-empty-device': _("Emptying $target"),
67 'lvm-vg-create-volume': _("Creating logical volume $target"),
68 'lvm-vg-rename': _("Renaming $target"),
69 'lvm-vg-resize': _("Resizing $target")
70 };
71
72 function make_description(client, job) {
73 let fmt = descriptions[job.Operation];
74 if (!fmt)
75 fmt = _("Operation '$operation' on $target");
76
77 const target = job.Objects.map(function (path) {
78 if (client.blocks[path])
79 return block_name(client.blocks[client.blocks[path].CryptoBackingDevice] || client.blocks[path]);
80 else if (client.mdraids[path])
81 return mdraid_name(client.mdraids[path]);
82 else if (client.vgroups[path])
83 return client.vgroups[path].Name;
84 else if (client.lvols[path])
85 return lvol_name(client.lvols[path]);
86 else
87 return _("unknown target");
88 }).join(", ");
89
90 return cockpit.format(fmt, { operation: job.Operation, target });
91 }
92
93 class JobRow extends React.Component {
94 render() {
95 const job = this.props.job;
96
97 function cancel() {
98 return job.Cancel({});
99 }
100
101 let remaining = null;
102 if (job.ExpectedEndTime > 0) {
103 const d = job.ExpectedEndTime / 1000 - this.props.now;
104 if (d > 0)
105 remaining = format_delay(d);
106 }
107
108 return (
109 <DataListItem>
110 <DataListItemRow>
111 <DataListItemCells
112 dataListCells={[
113 <DataListCell key="spinner" isFilled={false}>
114 <Spinner size="lg" />
115 </DataListCell>,
116 <DataListCell key="desc" className="job-description" isFilled={false}>
117 {make_description(this.props.client, job)}
118 </DataListCell>,
119 <DataListCell key="progress" isFilled={false}>
120 {job.ProgressValid && (job.Progress * 100).toFixed() + "%"}
121 </DataListCell>,
122 <DataListCell key="remaining" isFilled={false}>
123 {remaining}
124 </DataListCell>,
125 <DataListCell key="job-action" isFilled={false} alignRight>
126 { job.Cancelable ? <StorageButton onClick={cancel}>{_("Cancel")}</StorageButton> : null }
127 </DataListCell>,
128 ]}
129 />
130 </DataListItemRow>
131 </DataListItem>
132 );
133 }
134 }
135
136 export class JobsPanel extends React.Component {
137 constructor() {
138 super();
139 this.reminder = null;
140 }
141
142 componentWillUnmount() {
143 if (this.reminder) {
144 window.clearTimeout(this.reminder);
145 this.reminder = null;
146 }
147 }
148
149 render() {
150 const client = this.props.client;
151 const path_jobs = client.path_jobs[this.props.path] || [];
152 const server_now = new Date().getTime() + client.time_offset;
153
154 function cmp_job(job_a, job_b) {
155 return job_a.StartTime - job_b.StartTime;
156 }
157
158 function job_is_stable(job) {
159 const age_ms = server_now - job.StartTime / 1000;
160 if (age_ms >= 2000)
161 return true;
162
163 if (job.ExpectedEndTime > 0 && (job.ExpectedEndTime / 1000 - server_now) >= 2000)
164 return true;
165
166 return false;
167 }
168
169 let jobs = [];
170 let have_reminder = false;
171 for (const j of path_jobs) {
172 if (job_is_stable(j)) {
173 jobs.push(j);
174 } else if (!have_reminder) {
175 // If there is a unstable job, we have to check again in a bit since being
176 // stable or not depends on the current time.
177 if (this.reminder)
178 window.clearTimeout(this.reminder);
179 this.reminder = window.setTimeout(() => { this.setState({}) }, 1000);
180 have_reminder = true;
181 }
182 }
183
184 if (jobs.length === 0)
185 return null;
186
187 jobs = jobs.sort(cmp_job);
188
189 return (
190 <CardBody className="contains-list">
191 <DataList isCompact aria-label={_("Jobs")}>
192 { jobs.map(j => <JobRow key={j.path} client={client} job={j} now={server_now} />) }
193 </DataList>
194 </CardBody>
195 );
196 }
197 }
198
199 export function job_progress_wrapper(client, path1, path2) {
200 return function (vals, progress_callback, action_function) {
201 function client_changed() {
202 const jobs = client.path_jobs[path1] || client.path_jobs[path2];
203 if (jobs && jobs.length > 0) {
204 const job = jobs[0];
205 let desc = make_description(client, job);
206 if (job.ProgressValid)
207 desc += cockpit.format(" ($0%)", (job.Progress * 100).toFixed());
208 progress_callback(desc, job.Cancelable ? () => job.Cancel({}) : null);
209 } else {
210 progress_callback(null, null);
211 }
212 }
213
214 client.addEventListener("changed", client_changed);
215 return action_function(vals, progress_callback)
216 .finally(() => { client.removeEventListener("changed", client_changed) });
217 };
218 }