Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

fix(downloads): select Transmission and Deluge torrents before limiting (#7292)

Thomas73f27ccd2d10
diff --git a/packages/api/src/router/widgets/downloads.ts b/packages/api/src/router/widgets/downloads.ts
index 0e2de7745..622c84d6d 100644
--- a/packages/api/src/router/widgets/downloads.ts
+++ b/packages/api/src/router/widgets/downloads.ts
@@ -52,7 +52,7 @@ export const downloadsRouter = createTRPCRouter({
mcp: {
enabled: true,
description:
- "Get active download jobs and queue status from connected download clients (qBittorrent, SABnzbd, Transmission, Deluge, NZBGet). REQUIRED: integrationIds (array of download client integration IDs from integration_all). OPTIONAL: limitPerIntegration (number, default 50). For qBittorrent, optional selection applies completed/category filters and provider-compatible sorting before the display limit, scanning at most 100 pages of at most 100 torrents. Unsupported sort fields and other providers still sort the returned window locally. For SABnzbd, includeArchivedHistory includes archived jobs when true and defaults to false; historyWindowDays is a positive whole number of days and defaults to 7. Archived history is scanned in pages of 100, up to 100 pages.",
+ "Get active download jobs and queue status from connected download clients (qBittorrent, SABnzbd, Transmission, Deluge, NZBGet). REQUIRED: integrationIds (array of download client integration IDs from integration_all). OPTIONAL: limitPerIntegration (number, default 50). For qBittorrent, Transmission and Deluge, optional selection applies completed/category filters and supported sorting before the display limit. qBittorrent scans at most 100 pages of at most 100 torrents; Transmission and Deluge select from their full RPC responses. Unsupported sort fields and other providers still sort the returned window locally. For SABnzbd, includeArchivedHistory includes archived jobs when true and defaults to false; historyWindowDays is a positive whole number of days and defaults to 7. Archived history is scanned in pages of 100, up to 100 pages.",
},
})
.concat(createManyWidgetIntegrationMiddleware("query", "downloads"))
diff --git a/packages/integrations/src/download-client/deluge/deluge-integration.ts b/packages/integrations/src/download-client/deluge/deluge-integration.ts
index a4a0c12d4..88bcf81a1 100644
--- a/packages/integrations/src/download-client/deluge/deluge-integration.ts
+++ b/packages/integrations/src/download-client/deluge/deluge-integration.ts
@@ -11,10 +11,14 @@ import type { IntegrationTestingInput } from "../../base/integration";
import { TestConnectionError } from "../../base/test-connection/test-connection-error";
import type { TestingResult } from "../../base/test-connection/test-connection-service";
import type { DownloadClientJobsAndStatus } from "../../interfaces/downloads/download-client-data";
-import type { IDownloadClientIntegration } from "../../interfaces/downloads/download-client-integration";
+import type {
+ DownloadClientSelection,
+ IDownloadClientIntegration,
+} from "../../interfaces/downloads/download-client-integration";
import type { DownloadClientItem } from "../../interfaces/downloads/download-client-items";
import type { DownloadClientStatus } from "../../interfaces/downloads/download-client-status";
import type { IntegrationHttpAuthentication } from "../../http-auth";
+import { selectDownloadTorrents } from "../select-torrents";
@HandleIntegrationErrors([integrationOFetchHttpErrorHandler])
export class DelugeIntegration extends Integration implements IDownloadClientIntegration {
@@ -55,7 +59,10 @@ export class DelugeIntegration extends Integration implements IDownloadClientInt
};
}
- public async getClientJobsAndStatusAsync(input: { limit: number }): Promise<DownloadClientJobsAndStatus> {
+ public async getClientJobsAndStatusAsync(input: {
+ limit: number;
+ selection?: DownloadClientSelection;
+ }): Promise<DownloadClientJobsAndStatus> {
const type = "torrent";
const client = await this.getClientAsync();
// Currently there is no way to limit the number of returned torrents
@@ -76,7 +83,32 @@ export class DelugeIntegration extends Integration implements IDownloadClientInt
},
types: [type],
};
- const items = torrents.slice(0, input.limit).map((torrent): DownloadClientItem => {
+ const currentTime = dayjs().unix();
+ const selectedTorrents = selectDownloadTorrents(torrents, input, {
+ category: (torrent) => torrent.label,
+ progress: (torrent) => torrent.progress / 100,
+ upSpeed: (torrent) => torrent.upload_payload_rate,
+ downSpeed: (torrent) => {
+ if (torrent.progress === 100) return undefined;
+ return torrent.download_payload_rate;
+ },
+ name: (torrent) => torrent.name,
+ size: (torrent) => torrent.total_wanted,
+ added: (torrent) => torrent.time_added * 1000,
+ index: (torrent) => torrent.queue,
+ sent: (torrent) => torrent.total_uploaded,
+ received: (torrent) => Math.floor(torrent.total_wanted * (torrent.progress / 100)),
+ ratio: (torrent) => {
+ if (torrent.total_wanted <= 0) return undefined;
+ return torrent.total_uploaded / torrent.total_wanted;
+ },
+ state: (torrent) => DelugeIntegration.getTorrentState(torrent.state),
+ time: (torrent) => {
+ if (torrent.progress === 100) return Math.min((torrent.completed_time - currentTime) * 1000, -1);
+ return Math.max(torrent.eta * 1000, 0);
+ },
+ });
+ const items = selectedTorrents.map((torrent): DownloadClientItem => {
const state = DelugeIntegration.getTorrentState(torrent.state);
return {
type,
@@ -89,7 +121,7 @@ export class DelugeIntegration extends Integration implements IDownloadClientInt
upSpeed: torrent.upload_payload_rate,
time:
torrent.progress === 100
- ? Math.min((torrent.completed_time - dayjs().unix()) * 1000, -1)
+ ? Math.min((torrent.completed_time - currentTime) * 1000, -1)
: Math.max(torrent.eta * 1000, 0),
added: torrent.time_added * 1000,
state,
@@ -97,7 +129,15 @@ export class DelugeIntegration extends Integration implements IDownloadClientInt
category: torrent.label,
};
});
- return { status, items };
+ if (!input.selection) return { status, items };
+
+ const ratioItems = torrents.slice(0, input.limit).map((torrent) => ({
+ sent: torrent.total_uploaded,
+ size: torrent.total_wanted,
+ progress: torrent.progress / 100,
+ category: torrent.label,
+ }));
+ return { status, items, ratioItems };
}
public async pauseQueueAsync() {
diff --git a/packages/integrations/src/download-client/select-torrents.ts b/packages/integrations/src/download-client/select-torrents.ts
new file mode 100644
index 000000000..0d91d40f1
--- /dev/null
+++ b/packages/integrations/src/download-client/select-torrents.ts
@@ -0,0 +1,50 @@
+import type { DownloadClientSelection } from "../interfaces/downloads/download-client-integration";
+import type { DownloadClientItem } from "../interfaces/downloads/download-client-items";
+
+type TorrentSelectionFields<TTorrent> = {
+ [TField in "category" | "progress" | "upSpeed"]: (torrent: TTorrent) => DownloadClientItem[TField];
+} & Partial<Record<keyof DownloadClientItem | "ratio", (torrent: TTorrent) => string | number | string[] | undefined>>;
+
+export function selectDownloadTorrents<TTorrent>(
+ torrents: TTorrent[],
+ { limit, selection }: { limit: number; selection?: DownloadClientSelection },
+ fields: TorrentSelectionFields<TTorrent>,
+): TTorrent[] {
+ if (!selection) return torrents.slice(0, limit);
+
+ const eligibleTorrents = torrents.filter((torrent) => {
+ if (selection.categoryFilter.length > 0) {
+ const category = fields.category(torrent) ?? "";
+ let categories: string[];
+ if (Array.isArray(category)) categories = category;
+ else categories = [category];
+ const matches = selection.categoryFilter.some((filter) => categories.includes(filter));
+ if (matches !== selection.filterIsWhitelist) return false;
+ }
+ if (fields.progress(torrent) < 1) return true;
+ return selection.showCompletedTorrent && (fields.upSpeed(torrent) ?? 0) >= selection.activeTorrentThreshold * 1024;
+ });
+
+ const sortValue = fields[selection.sort as keyof typeof fields];
+ if (sortValue) {
+ let multiplier = 1;
+ if (selection.descending) multiplier = -1;
+ eligibleTorrents.sort((left, right) => {
+ let leftValue = sortValue(left) ?? 0;
+ let rightValue = sortValue(right) ?? 0;
+ if (selection.sort === "time") {
+ if (leftValue === 0) leftValue = Infinity;
+ if (rightValue === 0) rightValue = Infinity;
+ }
+ if (typeof leftValue === "string" && typeof rightValue === "string") {
+ return leftValue.localeCompare(rightValue) * multiplier;
+ }
+ if (typeof leftValue === "number" && typeof rightValue === "number") {
+ if (leftValue === rightValue) return 0;
+ return (leftValue - rightValue) * multiplier;
+ }
+ return 0;
+ });
+ }
+ return eligibleTorrents.slice(0, limit);
+}
diff --git a/packages/integrations/src/download-client/transmission/transmission-integration.ts b/packages/integrations/src/download-client/transmission/transmission-integration.ts
index a1cd56ad0..b17b05889 100644
--- a/packages/integrations/src/download-client/transmission/transmission-integration.ts
+++ b/packages/integrations/src/download-client/transmission/transmission-integration.ts
@@ -10,9 +10,13 @@ import { Integration } from "../../base/integration";
import type { IntegrationTestingInput } from "../../base/integration";
import type { TestingResult } from "../../base/test-connection/test-connection-service";
import type { DownloadClientJobsAndStatus } from "../../interfaces/downloads/download-client-data";
-import type { IDownloadClientIntegration } from "../../interfaces/downloads/download-client-integration";
+import type {
+ DownloadClientSelection,
+ IDownloadClientIntegration,
+} from "../../interfaces/downloads/download-client-integration";
import type { DownloadClientItem } from "../../interfaces/downloads/download-client-items";
import type { DownloadClientStatus } from "../../interfaces/downloads/download-client-status";
+import { selectDownloadTorrents } from "../select-torrents";
@HandleIntegrationErrors([integrationOFetchHttpErrorHandler])
export class TransmissionIntegration extends Integration implements IDownloadClientIntegration {
@@ -24,7 +28,10 @@ export class TransmissionIntegration extends Integration implements IDownloadCli
};
}
- public async getClientJobsAndStatusAsync(input: { limit: number }): Promise<DownloadClientJobsAndStatus> {
+ public async getClientJobsAndStatusAsync(input: {
+ limit: number;
+ selection?: DownloadClientSelection;
+ }): Promise<DownloadClientJobsAndStatus> {
const type = "torrent";
const client = await this.getClientAsync();
// Currently there is no way to limit the number of returned torrents
@@ -36,7 +43,32 @@ export class TransmissionIntegration extends Integration implements IDownloadCli
const paused =
torrents.find(({ status }) => TransmissionIntegration.getTorrentState(status) !== "paused") === undefined;
const status: DownloadClientStatus = { paused, rates, types: [type] };
- const items = torrents.slice(0, input.limit).map((torrent): DownloadClientItem => {
+ const currentTime = dayjs().valueOf();
+ const selectedTorrents = selectDownloadTorrents(torrents, input, {
+ category: (torrent) => torrent.labels,
+ progress: (torrent) => torrent.percentDone,
+ upSpeed: (torrent) => torrent.rateUpload,
+ downSpeed: (torrent) => {
+ if (torrent.percentDone === 1) return undefined;
+ return torrent.rateDownload;
+ },
+ name: (torrent) => torrent.name,
+ size: (torrent) => torrent.totalSize,
+ added: (torrent) => torrent.addedDate * 1000,
+ index: (torrent) => torrent.queuePosition,
+ sent: (torrent) => torrent.uploadedEver,
+ received: (torrent) => torrent.downloadedEver,
+ ratio: (torrent) => {
+ if (torrent.totalSize <= 0) return undefined;
+ return torrent.uploadedEver / torrent.totalSize;
+ },
+ state: (torrent) => TransmissionIntegration.getTorrentState(torrent.status),
+ time: (torrent) => {
+ if (torrent.percentDone === 1) return Math.min(torrent.doneDate * 1000 - currentTime, -1);
+ return Math.max(torrent.eta * 1000, 0);
+ },
+ });
+ const items = selectedTorrents.map((torrent): DownloadClientItem => {
const state = TransmissionIntegration.getTorrentState(torrent.status);
return {
type,
@@ -50,7 +82,7 @@ export class TransmissionIntegration extends Integration implements IDownloadCli
upSpeed: torrent.rateUpload,
time:
torrent.percentDone === 1
- ? Math.min(torrent.doneDate * 1000 - dayjs().valueOf(), -1)
+ ? Math.min(torrent.doneDate * 1000 - currentTime, -1)
: Math.max(torrent.eta * 1000, 0),
added: torrent.addedDate * 1000,
state,
@@ -58,7 +90,15 @@ export class TransmissionIntegration extends Integration implements IDownloadCli
category: torrent.labels,
};
});
- return { status, items };
+ if (!input.selection) return { status, items };
+
+ const ratioItems = torrents.slice(0, input.limit).map((torrent) => ({
+ sent: torrent.uploadedEver,
+ size: torrent.totalSize,
+ progress: torrent.percentDone,
+ category: torrent.labels,
+ }));
+ return { status, items, ratioItems };
}
public async pauseQueueAsync() {