78 lines
2.7 KiB
React
Raw Normal View History

2023-08-01 03:54:19 +03:00
import { useTranslation } from "next-i18next";
2023-08-01 13:05:17 +03:00
import Container from "../components/container";
import Block from "../components/block";
2023-08-01 03:54:19 +03:00
import useWidgetAPI from "utils/proxy/use-widget-api";
import ResolvedIcon from "components/resolvedicon";
const statusMap = {
R: <ResolvedIcon icon="mdi-circle" width={32} height={32} />, // running
S: <ResolvedIcon icon="mdi-circle-outline" width={32} height={32} />, // sleeping
D: <ResolvedIcon icon="mdi-circle-double" width={32} height={32} />, // disk sleep
Z: <ResolvedIcon icon="mdi-circle-opacity" width={32} height={32} />, // zombie
T: <ResolvedIcon icon="mdi-decagram-outline" width={32} height={32} />, // traced
t: <ResolvedIcon icon="mdi-hexagon-outline" width={32} height={32} />, // traced
X: <ResolvedIcon icon="mdi-rhombus-outline" width={32} height={32} />, // dead
2023-08-01 03:54:19 +03:00
};
const defaultInterval = 1000;
2023-08-01 03:54:19 +03:00
export default function Component({ service }) {
const { t } = useTranslation();
2023-09-06 13:53:39 +03:00
const { widget } = service;
const { chart, refreshInterval = defaultInterval, version = 3 } = widget;
const memoryInfoKey = version === 3 ? 0 : "data";
2023-08-01 03:54:19 +03:00
const { data, error } = useWidgetAPI(service.widget, `${version}/processlist`, {
refreshInterval: Math.max(defaultInterval, refreshInterval),
2023-08-01 03:54:19 +03:00
});
if (error) {
return <Container service={service} widget={widget} />;
2023-08-01 03:54:19 +03:00
}
if (!data) {
return (
<Container chart={chart}>
<Block position="bottom-3 left-3">-</Block>
</Container>
);
2023-08-01 03:54:19 +03:00
}
2023-09-06 13:53:39 +03:00
data.splice(chart ? 5 : 1);
2023-08-01 03:54:19 +03:00
return (
2023-09-06 13:53:39 +03:00
<Container chart={chart}>
2023-08-01 16:39:46 +03:00
<Block position="top-4 right-3 left-3">
2023-08-01 03:54:19 +03:00
<div className="flex items-center text-xs">
<div className="grow" />
<div className="w-14 text-right italic">{t("resources.cpu")}</div>
<div className="w-14 text-right">{t("resources.mem")}</div>
</div>
2023-08-01 13:05:17 +03:00
</Block>
2023-08-01 16:39:46 +03:00
<Block position="bottom-4 right-3 left-3">
2023-08-01 13:05:17 +03:00
<div className="pointer-events-none text-theme-900 dark:text-theme-200">
{data.map((item) => (
<div key={item.pid} className="text-[0.75rem] h-[0.8rem]">
<div className="flex items-center">
<div className="w-3 h-3 mr-1.5 opacity-50">{statusMap[item.status]}</div>
<div className="opacity-75 grow truncate">{item.name}</div>
<div className="opacity-25 w-14 text-right">{item.cpu_percent.toFixed(1)}%</div>
<div className="opacity-25 w-14 text-right">
{t("common.bytes", {
value: item.memory_info[memoryInfoKey] ?? item.memory_info.wset,
maximumFractionDigits: 0,
})}
</div>
2023-08-01 03:54:19 +03:00
</div>
</div>
))}
2023-08-01 13:05:17 +03:00
</div>
</Block>
</Container>
2023-08-01 03:54:19 +03:00
);
}