| 1 |
/* |
| 2 |
* Copyright (C) 2025 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React, { useState } from 'react'; |
| 7 |
|
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { ExclamationCircleIcon } from "@patternfly/react-icons"; |
| 10 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 11 |
import { Icon } from '@patternfly/react-core/dist/esm/components/Icon/index.js'; |
| 12 |
|
| 13 |
import cockpit from "cockpit"; |
| 14 |
import { useEvent, useInit } from 'hooks'; |
| 15 |
|
| 16 |
function countFailingDisks(proxies) { |
| 17 |
const sataFail = Object.keys(proxies.drives_ata).reduce((acc, drive) => { |
| 18 |
const smart = proxies.drives_ata[drive]; |
| 19 |
if (smart.SmartFailing) { |
| 20 |
return acc + 1; |
| 21 |
} else { |
| 22 |
return acc; |
| 23 |
} |
| 24 |
}, 0); |
| 25 |
|
| 26 |
const nvmeFail = Object.keys(proxies.nvme_controller).reduce((acc, drive) => { |
| 27 |
const smart = proxies.nvme_controller[drive]; |
| 28 |
if (smart.SmartCriticalWarning.length > 0) { |
| 29 |
return acc + 1; |
| 30 |
} else { |
| 31 |
return acc; |
| 32 |
} |
| 33 |
}, 0); |
| 34 |
|
| 35 |
return sataFail + nvmeFail; |
| 36 |
} |
| 37 |
|
| 38 |
const proxies = { |
| 39 |
drives_ata: null, |
| 40 |
nvme_controller: null, |
| 41 |
}; |
| 42 |
|
| 43 |
export const SmartOverviewStatus = () => { |
| 44 |
useEvent(proxies.drives_ata, "changed"); |
| 45 |
useEvent(proxies.nvme_controller, "changed"); |
| 46 |
const [initDone, setInitDone] = useState(false); |
| 47 |
|
| 48 |
useInit(async () => { |
| 49 |
const udisksdbus = cockpit.dbus("org.freedesktop.UDisks2"); |
| 50 |
|
| 51 |
const addProxy = (iface) => { |
| 52 |
return udisksdbus.proxies("org.freedesktop.UDisks2." + iface, "/org/freedesktop/UDisks2"); |
| 53 |
}; |
| 54 |
|
| 55 |
proxies.drives_ata = addProxy("Drive.Ata"); |
| 56 |
proxies.nvme_controller = addProxy("NVMe.Controller"); |
| 57 |
await Promise.all(Object.keys(proxies).map(proxy => proxies[proxy].wait())); |
| 58 |
|
| 59 |
setInitDone(true); |
| 60 |
}); |
| 61 |
|
| 62 |
if (initDone === false || proxies.drives_ata === null || proxies.nvme_controller === null) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
const failingDisks = countFailingDisks(proxies); |
| 67 |
if (failingDisks === 0) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
return ( |
| 72 |
<li id="smart-status"> |
| 73 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 74 |
<Icon status="danger"> |
| 75 |
<ExclamationCircleIcon /> |
| 76 |
</Icon> |
| 77 |
<Button variant="link" component="a" isInline |
| 78 |
onClick={() => cockpit.jump("/storage")} |
| 79 |
> |
| 80 |
{cockpit.format(cockpit.ngettext("$0 disk is failing", |
| 81 |
"$0 disks are failing", failingDisks), |
| 82 |
failingDisks)} |
| 83 |
</Button> |
| 84 |
</Flex> |
| 85 |
</li> |
| 86 |
); |
| 87 |
}; |