Skip to content

Commit

Permalink
Feature: Search limit (#418)
Browse files Browse the repository at this point in the history
The idea here is to limit the number of queries to be made between each
running.

The current feature `excludeRecentSearch` can already filter off some
results, so combined with a limit feature we can have more flexibility.

Example of my usage:
- My library has 440 items
- `excludeRecentSearch` set to `5 days`
- `searchLimit` set to `100`
- `searchCadence` set to `1 day`

Every day the search runs, it picks only the 100 that have not be done
before

The user needs to pay attemption of their values or this can keep items
out of the search, for example, if the `excludeRecentSearch` is too
short for the limit, it will be start to pick items again...

fixes #432
  • Loading branch information
mmgoodnow committed Jun 30, 2023
2 parents 33c68f9 + 8d75927 commit 2544077
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ function createCommandWithSharedOptions(name, description) {
"--search-timeout <timeout>",
"Timeout for unresponsive searches",
fallback(fileConfig.searchTimeout, "30 seconds")
)
.option(
"--search-limit <number>",
"The number of searches before stops",
parseInt,
fallback(fileConfig.searchLimit, 0)
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/config.template.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,11 @@ module.exports = {
* null
*/
searchTimeout: undefined,

/**
* The number of searches to be done before it stop.
* Combine this with "excludeRecentSearch" and "searchCadence" to smooth long-term API usage patterns.
* Default is no limit.
*/
searchLimit: undefined,
};
7 changes: 7 additions & 0 deletions src/config.template.docker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,11 @@ module.exports = {
* null
*/
searchTimeout: undefined,

/**
* The number of searches to be done before stop.
* Combine this with "excludeRecentSearch" and "searchCadence" to smooth long-term API usage patterns.
* Default is no limit.
*/
searchLimit: undefined,
};
1 change: 1 addition & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface FileConfig {
rssCadence?: string;
snatchTimeout?: string;
searchTimeout?: string;
searchLimit?: number;
}

interface GenerateConfigParams {
Expand Down
13 changes: 11 additions & 2 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export async function checkNewCandidateMatch(
}

async function findSearchableTorrents() {
const { torrents, dataDirs, torrentDir } = getRuntimeConfig();
const { torrents, dataDirs, torrentDir, searchLimit } = getRuntimeConfig();
let allSearchees: Searchee[] = [];
if (Array.isArray(torrents)) {
const searcheeResults = await Promise.all(
Expand All @@ -255,7 +255,7 @@ async function findSearchableTorrents() {
}

const hashesToExclude = allSearchees.map((t) => t.infoHash).filter(Boolean);
const filteredTorrents = await filterAsync(
let filteredTorrents = await filterAsync(
filterDupes(allSearchees).filter(filterByContent),
filterTimestamps
);
Expand All @@ -265,6 +265,15 @@ async function findSearchableTorrents() {
message: `Found ${allSearchees.length} torrents, ${filteredTorrents.length} suitable to search for matches`,
});

if (searchLimit && filteredTorrents.length > searchLimit) {
logger.info({
label: Label.SEARCH,
message: `Limited to ${searchLimit} searches`,
});

filteredTorrents = filteredTorrents.slice(0, searchLimit);
}

return { samples: filteredTorrents, hashesToExclude };
}

Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action, LinkType, MatchMode } from "./constants.js";

export interface RuntimeConfig {
offset: number;
delay: number;
Expand Down Expand Up @@ -30,6 +31,7 @@ export interface RuntimeConfig {
rssCadence: number;
snatchTimeout: number;
searchTimeout: number;
searchLimit: number;
}

let runtimeConfig: RuntimeConfig;
Expand Down

0 comments on commit 2544077

Please sign in to comment.