mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-12 07:05:56 -07:00
Enhancement: use lighter endpoints for qbittorrent (#6388)
This commit is contained in:
@@ -10,13 +10,28 @@ export default function Component({ service }) {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { widget } = service;
|
const { widget } = service;
|
||||||
|
|
||||||
const { data: torrentData, error: torrentError } = useWidgetAPI(widget, "torrents");
|
const { data: transferData, error: transferError } = useWidgetAPI(widget, "transfer");
|
||||||
|
const { data: totalCountData, error: totalCountError } = useWidgetAPI(widget, "torrentCount");
|
||||||
|
const { data: completedCountData, error: completedCountError } = useWidgetAPI(widget, "torrentCount", {
|
||||||
|
filter: "completed",
|
||||||
|
});
|
||||||
|
const { data: leechTorrentData, error: leechTorrentError } = useWidgetAPI(
|
||||||
|
widget,
|
||||||
|
widget?.enableLeechProgress ? "torrents" : "",
|
||||||
|
widget?.enableLeechProgress ? { filter: "downloading" } : undefined,
|
||||||
|
);
|
||||||
|
|
||||||
if (torrentError) {
|
const apiError = transferError || totalCountError || completedCountError || leechTorrentError;
|
||||||
return <Container service={service} error={torrentError} />;
|
if (apiError) {
|
||||||
|
return <Container service={service} error={apiError} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!torrentData) {
|
if (
|
||||||
|
!transferData ||
|
||||||
|
totalCountData === undefined ||
|
||||||
|
completedCountData === undefined ||
|
||||||
|
(widget?.enableLeechProgress && !leechTorrentData)
|
||||||
|
) {
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="qbittorrent.leech" />
|
<Block label="qbittorrent.leech" />
|
||||||
@@ -27,24 +42,15 @@ export default function Component({ service }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let rateDl = 0;
|
const rateDl = Number(transferData?.dl_info_speed ?? 0);
|
||||||
let rateUl = 0;
|
const rateUl = Number(transferData?.up_info_speed ?? 0);
|
||||||
let completed = 0;
|
const totalCount = Number(totalCountData?.all ?? totalCountData?.count ?? totalCountData ?? 0);
|
||||||
const leechTorrents = [];
|
const completedCount = Number(
|
||||||
|
completedCountData?.completed ?? completedCountData?.count ?? completedCountData?.all ?? completedCountData ?? 0,
|
||||||
|
);
|
||||||
|
const leech = Math.max(0, totalCount - completedCount);
|
||||||
|
|
||||||
for (let i = 0; i < torrentData.length; i += 1) {
|
const leechTorrents = Array.isArray(leechTorrentData) ? [...leechTorrentData] : [];
|
||||||
const torrent = torrentData[i];
|
|
||||||
rateDl += torrent.dlspeed;
|
|
||||||
rateUl += torrent.upspeed;
|
|
||||||
if (torrent.progress === 1) {
|
|
||||||
completed += 1;
|
|
||||||
}
|
|
||||||
if (torrent.state.includes("DL") || torrent.state === "downloading") {
|
|
||||||
leechTorrents.push(torrent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const leech = torrentData.length - completed;
|
|
||||||
const statePriority = [
|
const statePriority = [
|
||||||
"downloading",
|
"downloading",
|
||||||
"forcedDL",
|
"forcedDL",
|
||||||
@@ -55,7 +61,6 @@ export default function Component({ service }) {
|
|||||||
"queuedDL",
|
"queuedDL",
|
||||||
"pausedDL",
|
"pausedDL",
|
||||||
];
|
];
|
||||||
|
|
||||||
leechTorrents.sort((firstTorrent, secondTorrent) => {
|
leechTorrents.sort((firstTorrent, secondTorrent) => {
|
||||||
const firstStateIndex = statePriority.indexOf(firstTorrent.state);
|
const firstStateIndex = statePriority.indexOf(firstTorrent.state);
|
||||||
const secondStateIndex = statePriority.indexOf(secondTorrent.state);
|
const secondStateIndex = statePriority.indexOf(secondTorrent.state);
|
||||||
@@ -70,7 +75,7 @@ export default function Component({ service }) {
|
|||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="qbittorrent.leech" value={t("common.number", { value: leech })} />
|
<Block label="qbittorrent.leech" value={t("common.number", { value: leech })} />
|
||||||
<Block label="qbittorrent.download" value={t("common.bibyterate", { value: rateDl, decimals: 1 })} />
|
<Block label="qbittorrent.download" value={t("common.bibyterate", { value: rateDl, decimals: 1 })} />
|
||||||
<Block label="qbittorrent.seed" value={t("common.number", { value: completed })} />
|
<Block label="qbittorrent.seed" value={t("common.number", { value: completedCount })} />
|
||||||
<Block label="qbittorrent.upload" value={t("common.bibyterate", { value: rateUl, decimals: 1 })} />
|
<Block label="qbittorrent.upload" value={t("common.bibyterate", { value: rateUl, decimals: 1 })} />
|
||||||
</Container>
|
</Container>
|
||||||
{widget?.enableLeechProgress &&
|
{widget?.enableLeechProgress &&
|
||||||
|
|||||||
@@ -34,19 +34,38 @@ describe("widgets/qbittorrent/component", () => {
|
|||||||
expect(screen.getByText("qbittorrent.upload")).toBeInTheDocument();
|
expect(screen.getByText("qbittorrent.upload")).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("computes leech/seed counts and upload/download rates, and can render leech progress entries", () => {
|
it("uses lightweight endpoints for counts/rates and filtered torrents for leech progress", () => {
|
||||||
useWidgetAPI.mockReturnValue({
|
useWidgetAPI.mockImplementation((_widget, endpoint, query) => {
|
||||||
|
if (endpoint === "transfer") {
|
||||||
|
return { data: { dl_info_speed: 15, up_info_speed: 3 }, error: undefined };
|
||||||
|
}
|
||||||
|
if (endpoint === "torrentCount" && !query) {
|
||||||
|
return { data: 2, error: undefined };
|
||||||
|
}
|
||||||
|
if (endpoint === "torrentCount" && query?.filter === "completed") {
|
||||||
|
return { data: 1, error: undefined };
|
||||||
|
}
|
||||||
|
if (endpoint === "torrents" && query?.filter === "downloading") {
|
||||||
|
return {
|
||||||
data: [
|
data: [
|
||||||
{ name: "A", dlspeed: 10, upspeed: 1, progress: 1, state: "uploading" },
|
{
|
||||||
{ name: "B", dlspeed: 5, upspeed: 2, progress: 0.5, state: "downloading", eta: 60, size: 100, amount_left: 50 },
|
name: "B",
|
||||||
|
progress: 0.5,
|
||||||
|
state: "downloading",
|
||||||
|
eta: 60,
|
||||||
|
size: 100,
|
||||||
|
amount_left: 50,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
error: undefined,
|
error: undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return { data: undefined, error: undefined };
|
||||||
});
|
});
|
||||||
|
|
||||||
const service = { widget: { type: "qbittorrent", enableLeechProgress: true } };
|
const service = { widget: { type: "qbittorrent", enableLeechProgress: true } };
|
||||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
|
|
||||||
// total=2, completed=1 => leech=1
|
|
||||||
expectBlockValue(container, "qbittorrent.leech", 1);
|
expectBlockValue(container, "qbittorrent.leech", 1);
|
||||||
expectBlockValue(container, "qbittorrent.seed", 1);
|
expectBlockValue(container, "qbittorrent.seed", 1);
|
||||||
expectBlockValue(container, "qbittorrent.download", 15);
|
expectBlockValue(container, "qbittorrent.download", 15);
|
||||||
|
|||||||
@@ -4,8 +4,16 @@ const widget = {
|
|||||||
proxyHandler: qbittorrentProxyHandler,
|
proxyHandler: qbittorrentProxyHandler,
|
||||||
|
|
||||||
mappings: {
|
mappings: {
|
||||||
|
transfer: {
|
||||||
|
endpoint: "transfer/info",
|
||||||
|
},
|
||||||
|
torrentCount: {
|
||||||
|
endpoint: "torrents/count",
|
||||||
|
optionalParams: ["filter"],
|
||||||
|
},
|
||||||
torrents: {
|
torrents: {
|
||||||
endpoint: "torrents/info",
|
endpoint: "torrents/info",
|
||||||
|
optionalParams: ["filter"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user