This commit is contained in:
Mateusz Gruszczyński
2026-05-06 23:10:53 +02:00
parent 98f155b53a
commit 2691442fc1
9 changed files with 351 additions and 366 deletions

View File

@@ -69,6 +69,5 @@ SOCKETIO_CORS_ALLOWED_ORIGINS = None if not _SOCKETIO_CORS else [item.strip() fo
TRAFFIC_HISTORY_RETENTION_DAYS = _env_int("PYTORRENT_TRAFFIC_HISTORY_RETENTION_DAYS", 90, 1) TRAFFIC_HISTORY_RETENTION_DAYS = _env_int("PYTORRENT_TRAFFIC_HISTORY_RETENTION_DAYS", 90, 1)
JOBS_RETENTION_DAYS = _env_int("PYTORRENT_JOBS_RETENTION_DAYS", 30, 1) JOBS_RETENTION_DAYS = _env_int("PYTORRENT_JOBS_RETENTION_DAYS", 30, 1)
SMART_QUEUE_HISTORY_RETENTION_DAYS = _env_int("PYTORRENT_SMART_QUEUE_HISTORY_RETENTION_DAYS", 30, 1) SMART_QUEUE_HISTORY_RETENTION_DAYS = _env_int("PYTORRENT_SMART_QUEUE_HISTORY_RETENTION_DAYS", 30, 1)
AUTOMATION_HISTORY_RETENTION_DAYS = _env_int("PYTORRENT_AUTOMATION_HISTORY_RETENTION_DAYS", 30, 1)
LOG_RETENTION_DAYS = _env_int("PYTORRENT_LOG_RETENTION_DAYS", 30, 1) LOG_RETENTION_DAYS = _env_int("PYTORRENT_LOG_RETENTION_DAYS", 30, 1)
SMART_QUEUE_LABEL = os.getenv("PYTORRENT_SMART_QUEUE_LABEL", "Smart Queue Paused") SMART_QUEUE_LABEL = os.getenv("PYTORRENT_SMART_QUEUE_LABEL", "Smart Queue Paused")

View File

@@ -326,11 +326,9 @@ def cleanup_summary() -> dict:
"jobs_total": _table_count("jobs"), "jobs_total": _table_count("jobs"),
"jobs_clearable": _table_count("jobs", "WHERE status NOT IN ('pending', 'running')"), "jobs_clearable": _table_count("jobs", "WHERE status NOT IN ('pending', 'running')"),
"smart_queue_history_total": _table_count("smart_queue_history"), "smart_queue_history_total": _table_count("smart_queue_history"),
"automation_history_total": _table_count("automation_history"),
"retention_days": { "retention_days": {
"jobs": JOBS_RETENTION_DAYS, "jobs": JOBS_RETENTION_DAYS,
"smart_queue_history": SMART_QUEUE_HISTORY_RETENTION_DAYS, "smart_queue_history": SMART_QUEUE_HISTORY_RETENTION_DAYS,
"automation_history": AUTOMATION_HISTORY_RETENTION_DAYS,
}, },
"database": _db_size(), "database": _db_size(),
} }
@@ -733,16 +731,6 @@ def cleanup_smart_queue():
return ok({"deleted": deleted, "cleanup": cleanup_summary()}) return ok({"deleted": deleted, "cleanup": cleanup_summary()})
@bp.post("/cleanup/automations")
def cleanup_automations():
from ..services import automation_rules
profile = preferences.active_profile()
if not profile:
return jsonify({"ok": False, "error": "No profile"}), 400
deleted = automation_rules.clear_history(profile["id"])
return ok({"deleted": deleted, "cleanup": cleanup_summary()})
@bp.post("/cleanup/all") @bp.post("/cleanup/all")
def cleanup_all(): def cleanup_all():
deleted_jobs = clear_jobs() deleted_jobs = clear_jobs()
@@ -753,10 +741,7 @@ def cleanup_all():
else: else:
cur = conn.execute("DELETE FROM smart_queue_history") cur = conn.execute("DELETE FROM smart_queue_history")
deleted_smart = int(cur.rowcount or 0) deleted_smart = int(cur.rowcount or 0)
from ..services import automation_rules return ok({"deleted": {"jobs": deleted_jobs, "smart_queue_history": deleted_smart}, "cleanup": cleanup_summary()})
profile = preferences.active_profile()
deleted_automation = automation_rules.clear_history(profile["id"]) if profile else 0
return ok({"deleted": {"jobs": deleted_jobs, "smart_queue_history": deleted_smart, "automation_history": deleted_automation}, "cleanup": cleanup_summary()})
@bp.post("/jobs/<job_id>/cancel") @bp.post("/jobs/<job_id>/cancel")
@@ -1067,32 +1052,6 @@ def automations_delete(rule_id: int):
return jsonify({'ok': False, 'error': str(exc)}), 400 return jsonify({'ok': False, 'error': str(exc)}), 400
@bp.delete('/automations/history/<int:history_id>')
def automations_history_delete(history_id: int):
from ..services import automation_rules
profile = preferences.active_profile()
if not profile:
return jsonify({'ok': False, 'error': 'No profile'}), 400
try:
deleted = automation_rules.delete_history_item(history_id, profile['id'])
return ok({'deleted': deleted, 'history': automation_rules.list_history(profile['id'])})
except Exception as exc:
return jsonify({'ok': False, 'error': str(exc)}), 400
@bp.delete('/automations/history')
def automations_history_clear():
from ..services import automation_rules
profile = preferences.active_profile()
if not profile:
return jsonify({'ok': False, 'error': 'No profile'}), 400
try:
deleted = automation_rules.clear_history(profile['id'])
return ok({'deleted': deleted, 'history': []})
except Exception as exc:
return jsonify({'ok': False, 'error': str(exc)}), 400
@bp.post('/automations/check') @bp.post('/automations/check')
def automations_check(): def automations_check():
from ..services import automation_rules from ..services import automation_rules

View File

@@ -88,26 +88,10 @@ def delete_rule(rule_id: int, profile_id: int, user_id: int | None = None) -> No
conn.execute('DELETE FROM automation_rule_state WHERE rule_id=? AND profile_id=?', (rule_id, profile_id)) conn.execute('DELETE FROM automation_rule_state WHERE rule_id=? AND profile_id=?', (rule_id, profile_id))
def list_history(profile_id: int, user_id: int | None = None, limit: int = 50) -> list[dict[str, Any]]: def list_history(profile_id: int, user_id: int | None = None, limit: int = 30) -> list[dict[str, Any]]:
user_id = user_id or default_user_id() user_id = user_id or default_user_id()
with connect() as conn: with connect() as conn:
return conn.execute('SELECT * FROM automation_history WHERE user_id=? AND profile_id=? ORDER BY created_at DESC LIMIT ?', (user_id, profile_id, max(1, min(int(limit or 50), 200)))).fetchall() return conn.execute('SELECT * FROM automation_history WHERE user_id=? AND profile_id=? ORDER BY created_at DESC LIMIT ?', (user_id, profile_id, max(1, min(int(limit or 30), 100)))).fetchall()
def delete_history_item(history_id: int, profile_id: int, user_id: int | None = None) -> int:
# Note: Allow removing a single automation history card from the UI without touching rules.
user_id = user_id or default_user_id()
with connect() as conn:
cur = conn.execute('DELETE FROM automation_history WHERE id=? AND user_id=? AND profile_id=?', (int(history_id), user_id, profile_id))
return int(cur.rowcount or 0)
def clear_history(profile_id: int, user_id: int | None = None) -> int:
# Note: History cleanup is separate from deleting automation rules.
user_id = user_id or default_user_id()
with connect() as conn:
cur = conn.execute('DELETE FROM automation_history WHERE user_id=? AND profile_id=?', (user_id, profile_id))
return int(cur.rowcount or 0)
def _condition_true(t: dict[str, Any], cond: dict[str, Any]) -> bool: def _condition_true(t: dict[str, Any], cond: dict[str, Any]) -> bool:
@@ -129,8 +113,8 @@ def _conditions_match(conn, rule: dict[str, Any], profile_id: int, t: dict[str,
for cond in rule.get('conditions') or []: for cond in rule.get('conditions') or []:
raw_ok = _condition_true(t, cond) raw_ok = _condition_true(t, cond)
negated = bool(cond.get('negate')) negated = bool(cond.get('negate'))
# Note: Negation is applied in the backend, so UI and API only store the condition flag.
ok = (not raw_ok) if negated else raw_ok ok = (not raw_ok) if negated else raw_ok
# Note: Conditions can now be negated in automation rules. Timed no-seeds keeps its old delayed behavior only for the positive condition, so old rules do not change.
if cond.get('type') == 'no_seeds' and int(cond.get('minutes') or 0) > 0 and not negated: if cond.get('type') == 'no_seeds' and int(cond.get('minutes') or 0) > 0 and not negated:
row = conn.execute('SELECT condition_since_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, h)).fetchone() row = conn.execute('SELECT condition_since_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, h)).fetchone()
if ok: if ok:
@@ -144,8 +128,7 @@ def _conditions_match(conn, rule: dict[str, Any], profile_id: int, t: dict[str,
return immediate_ok and delayed_ok return immediate_ok and delayed_ok
def _cooldown_ok(conn, rule: dict[str, Any], profile_id: int, torrent_hash: str = '*') -> bool: def _cooldown_ok(conn, rule: dict[str, Any], profile_id: int, torrent_hash: str = '__rule__') -> bool:
# Note: Automation cooldown is rule-wide for batch execution; '*' stores the last run for the whole rule.
cooldown = int(rule.get('cooldown_minutes') or 0) cooldown = int(rule.get('cooldown_minutes') or 0)
if cooldown <= 0: return True if cooldown <= 0: return True
row = conn.execute('SELECT last_applied_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, torrent_hash)).fetchone() row = conn.execute('SELECT last_applied_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, torrent_hash)).fetchone()
@@ -153,12 +136,12 @@ def _cooldown_ok(conn, rule: dict[str, Any], profile_id: int, torrent_hash: str
return _now_ts() - _ts(row['last_applied_at']) >= cooldown * 60 return _now_ts() - _ts(row['last_applied_at']) >= cooldown * 60
def _touch_rule_cooldown(conn, rule: dict[str, Any], profile_id: int, now: str) -> None: def _mark_rule_cooldown(conn, rule: dict[str, Any], profile_id: int, now: str) -> None:
conn.execute('INSERT INTO automation_rule_state(rule_id,profile_id,torrent_hash,last_matched_at,last_applied_at,updated_at) VALUES(?,?,?,?,?,?) ON CONFLICT(rule_id,profile_id,torrent_hash) DO UPDATE SET last_matched_at=excluded.last_matched_at, last_applied_at=excluded.last_applied_at, updated_at=excluded.updated_at', (rule['id'], profile_id, '*', now, now, now)) # Note: Cooldown is rule-level, so one batch execution blocks the whole automation until the cooldown expires.
conn.execute('INSERT INTO automation_rule_state(rule_id,profile_id,torrent_hash,last_applied_at,updated_at) VALUES(?,?,?,?,?) ON CONFLICT(rule_id,profile_id,torrent_hash) DO UPDATE SET last_applied_at=excluded.last_applied_at, updated_at=excluded.updated_at', (rule['id'], profile_id, '__rule__', now, now))
def _apply_effects_batch(c: Any, profile: dict[str, Any], torrents: list[dict[str, Any]], effects: list[dict[str, Any]]) -> list[dict[str, Any]]: def _apply_effects_bulk(c: Any, profile: dict[str, Any], torrents: list[dict[str, Any]], effects: list[dict[str, Any]]) -> list[dict[str, Any]]:
# Note: Rules now execute actions once for all matching torrents instead of calling move/check/start one item at a time.
hashes = [str(t.get('hash') or '') for t in torrents if str(t.get('hash') or '')] hashes = [str(t.get('hash') or '') for t in torrents if str(t.get('hash') or '')]
labels_by_hash = {str(t.get('hash') or ''): _label_names(t.get('label')) for t in torrents} labels_by_hash = {str(t.get('hash') or ''): _label_names(t.get('label')) for t in torrents}
applied: list[dict[str, Any]] = [] applied: list[dict[str, Any]] = []
@@ -167,36 +150,37 @@ def _apply_effects_batch(c: Any, profile: dict[str, Any], torrents: list[dict[st
typ = str(eff.get('type') or '') typ = str(eff.get('type') or '')
if typ == 'move': if typ == 'move':
path = str(eff.get('path') or '').strip() or rtorrent.default_download_path(profile) path = str(eff.get('path') or '').strip() or rtorrent.default_download_path(profile)
move_payload = {'path': path, 'move_data': bool(eff.get('move_data')), 'recheck': bool(eff.get('recheck', eff.get('move_data'))), 'keep_seeding': bool(eff.get('keep_seeding'))} payload = {
result = rtorrent.move_torrents(profile, hashes, move_payload) if path else None 'path': path,
if path: applied.append({'type': 'move', 'path': path, 'count': len(hashes), 'move_data': bool(eff.get('move_data')), 'recheck': bool(move_payload['recheck']), 'keep_seeding': bool(eff.get('keep_seeding')), 'result': result}) 'move_data': bool(eff.get('move_data')),
'recheck': bool(eff.get('recheck', eff.get('move_data'))),
'keep_seeding': bool(eff.get('keep_seeding')),
}
result = rtorrent.action(profile, hashes, 'move', payload)
applied.append({'type': 'move', 'path': path, 'count': len(hashes), 'move_data': payload['move_data'], 'recheck': payload['recheck'], 'keep_seeding': payload['keep_seeding'], 'result': result})
elif typ == 'add_label': elif typ == 'add_label':
label = str(eff.get('label') or '').strip() label = str(eff.get('label') or '').strip()
changed = 0
if label: if label:
for h in hashes: for h in hashes:
labels = labels_by_hash.get(h, []) labels = labels_by_hash.setdefault(h, [])
if label not in labels: if label not in labels:
labels.append(label); labels_by_hash[h] = labels; c.call('d.custom1.set', h, _label_value(labels)); changed += 1 labels.append(label); c.call('d.custom1.set', h, _label_value(labels))
applied.append({'type': 'add_label', 'label': label, 'count': changed}) applied.append({'type': 'add_label', 'label': label, 'count': len(hashes)})
elif typ == 'remove_label': elif typ == 'remove_label':
label = str(eff.get('label') or '').strip(); changed = 0 label = str(eff.get('label') or '').strip()
if label: if label:
for h in hashes: for h in hashes:
labels = labels_by_hash.get(h, []) labels = [x for x in labels_by_hash.get(h, []) if x != label]
new_labels = [x for x in labels if x != label] labels_by_hash[h] = labels; c.call('d.custom1.set', h, _label_value(labels))
if new_labels != labels: applied.append({'type': 'remove_label', 'label': label, 'count': len(hashes)})
labels_by_hash[h] = new_labels; c.call('d.custom1.set', h, _label_value(new_labels)); changed += 1
applied.append({'type': 'remove_label', 'label': label, 'count': changed})
elif typ == 'set_labels': elif typ == 'set_labels':
value = _label_value(_label_names(eff.get('labels'))); new_labels = _label_names(value) value = _label_value(_label_names(eff.get('labels')))
for h in hashes: for h in hashes:
labels_by_hash[h] = list(new_labels); c.call('d.custom1.set', h, value) labels_by_hash[h] = _label_names(value); c.call('d.custom1.set', h, value)
applied.append({'type': 'set_labels', 'labels': value, 'count': len(hashes)}) applied.append({'type': 'set_labels', 'labels': value, 'count': len(hashes)})
elif typ in {'pause', 'stop', 'start', 'resume', 'recheck'}: elif typ in {'pause', 'stop', 'start', 'resume', 'recheck'}:
method = {'pause':'d.pause','stop':'d.stop','start':'d.start','resume':'d.resume','recheck':'d.check_hash'}[typ] result = rtorrent.action(profile, hashes, typ, {})
for h in hashes: c.call(method, h) applied.append({'type': typ, 'count': len(hashes), 'result': result})
applied.append({'type': typ, 'count': len(hashes)})
return applied return applied
@@ -205,23 +189,30 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
if not profile: return {'ok': False, 'error': 'No active rTorrent profile'} if not profile: return {'ok': False, 'error': 'No active rTorrent profile'}
user_id = user_id or default_user_id(); profile_id = int(profile['id']) user_id = user_id or default_user_id(); profile_id = int(profile['id'])
rules = [r for r in list_rules(profile_id, user_id) if force or int(r.get('enabled') or 0)] rules = [r for r in list_rules(profile_id, user_id) if force or int(r.get('enabled') or 0)]
if not rules: return {'ok': True, 'checked': 0, 'applied': [], 'rules': 0} if not rules: return {'ok': True, 'checked': 0, 'applied': [], 'batches': [], 'rules': 0}
torrents = rtorrent.list_torrents(profile); c = rtorrent.client_for(profile); applied = []; now = utcnow() torrents = rtorrent.list_torrents(profile); c = rtorrent.client_for(profile); applied = []; batches = []; now = utcnow()
with connect() as conn: with connect() as conn:
for rule in rules: for rule in rules:
# Note: Automations now execute as one batch per rule, not as one independent action per torrent.
if not force and not _cooldown_ok(conn, rule, profile_id):
continue
matched = [t for t in torrents if _conditions_match(conn, rule, profile_id, t)] matched = [t for t in torrents if _conditions_match(conn, rule, profile_id, t)]
if not matched: continue if not matched:
if not force and not _cooldown_ok(conn, rule, profile_id, '*'): continue continue
hashes = [str(t.get('hash') or '') for t in matched if str(t.get('hash') or '')] hashes = [str(t.get('hash') or '') for t in matched if str(t.get('hash') or '')]
names = [str(t.get('name') or '') for t in matched] if not hashes:
try: actions = _apply_effects_batch(c, profile, matched, rule.get('effects') or []) continue
except Exception as exc: actions = [{'error': str(exc), 'count': len(hashes)}] try:
_touch_rule_cooldown(conn, rule, profile_id, now) actions = _apply_effects_bulk(c, profile, matched, rule.get('effects') or [])
for h in hashes: except Exception as exc:
actions = [{'error': str(exc), 'count': len(hashes)}]
for t in matched:
h = str(t.get('hash') or '')
conn.execute('INSERT INTO automation_rule_state(rule_id,profile_id,torrent_hash,last_matched_at,last_applied_at,updated_at) VALUES(?,?,?,?,?,?) ON CONFLICT(rule_id,profile_id,torrent_hash) DO UPDATE SET last_matched_at=excluded.last_matched_at, last_applied_at=excluded.last_applied_at, updated_at=excluded.updated_at', (rule['id'], profile_id, h, now, now, now)) conn.execute('INSERT INTO automation_rule_state(rule_id,profile_id,torrent_hash,last_matched_at,last_applied_at,updated_at) VALUES(?,?,?,?,?,?) ON CONFLICT(rule_id,profile_id,torrent_hash) DO UPDATE SET last_matched_at=excluded.last_matched_at, last_applied_at=excluded.last_applied_at, updated_at=excluded.updated_at', (rule['id'], profile_id, h, now, now, now))
history_payload = {'mode': 'batch', 'count': len(hashes), 'hashes': hashes, 'names': names[:50], 'actions': actions} applied.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'hash': h, 'name': t.get('name'), 'actions': [{'type': a.get('type', 'error'), 'count': a.get('count', len(hashes))} for a in actions]})
torrent_name = names[0] if len(names) == 1 else f'{len(hashes)} torrents' _mark_rule_cooldown(conn, rule, profile_id, now)
torrent_hash = hashes[0] if len(hashes) == 1 else ','.join(hashes[:20]) torrent_name = str(matched[0].get('name') or '') if len(matched) == 1 else f'{len(matched)} torrents'
conn.execute('INSERT INTO automation_history(user_id,profile_id,rule_id,torrent_hash,torrent_name,rule_name,actions_json,created_at) VALUES(?,?,?,?,?,?,?,?)', (user_id, profile_id, rule['id'], torrent_hash, torrent_name, str(rule.get('name') or ''), json.dumps(history_payload), now)) torrent_hash = hashes[0] if len(hashes) == 1 else f'batch:{rule["id"]}:{now}'
applied.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'count': len(hashes), 'hashes': hashes, 'names': names[:20], 'actions': actions}) conn.execute('INSERT INTO automation_history(user_id,profile_id,rule_id,torrent_hash,torrent_name,rule_name,actions_json,created_at) VALUES(?,?,?,?,?,?,?,?)', (user_id, profile_id, rule['id'], torrent_hash, torrent_name, str(rule.get('name') or ''), json.dumps(actions), now))
return {'ok': True, 'checked': len(torrents), 'rules': len(rules), 'applied': applied} batches.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'count': len(hashes), 'actions': actions})
return {'ok': True, 'checked': len(torrents), 'rules': len(rules), 'applied': applied, 'batches': batches}

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from ..config import AUTOMATION_HISTORY_RETENTION_DAYS, JOBS_RETENTION_DAYS, LOG_RETENTION_DAYS, SMART_QUEUE_HISTORY_RETENTION_DAYS, TRAFFIC_HISTORY_RETENTION_DAYS from ..config import JOBS_RETENTION_DAYS, LOG_RETENTION_DAYS, SMART_QUEUE_HISTORY_RETENTION_DAYS, TRAFFIC_HISTORY_RETENTION_DAYS
from ..db import connect from ..db import connect
_LAST_CLEANUP = 0.0 _LAST_CLEANUP = 0.0
@@ -30,7 +30,6 @@ def cleanup(force: bool = False) -> dict[str, int]:
targets = { targets = {
"traffic_history": ("created_at", TRAFFIC_HISTORY_RETENTION_DAYS), "traffic_history": ("created_at", TRAFFIC_HISTORY_RETENTION_DAYS),
"smart_queue_history": ("created_at", SMART_QUEUE_HISTORY_RETENTION_DAYS), "smart_queue_history": ("created_at", SMART_QUEUE_HISTORY_RETENTION_DAYS),
"automation_history": ("created_at", AUTOMATION_HISTORY_RETENTION_DAYS),
"jobs": ("updated_at", JOBS_RETENTION_DAYS), "jobs": ("updated_at", JOBS_RETENTION_DAYS),
"logs": ("created_at", LOG_RETENTION_DAYS), "logs": ("created_at", LOG_RETENTION_DAYS),
} }

View File

@@ -1274,16 +1274,31 @@ def start_or_resume_hash(c: ScgiRtorrentClient, torrent_hash: str) -> dict:
result['ok'] = result.get('ok', True) result['ok'] = result.get('ok', True)
return result return result
def action(profile: dict, torrent_hashes: list[str], name: str, payload: dict | None = None) -> dict:
def move_torrents(profile: dict, torrent_hashes: list[str], payload: dict | None = None) -> dict:
# Note: Shared move implementation keeps API move and automation move-to-path identical.
payload = payload or {} payload = payload or {}
c = client_for(profile) c = client_for(profile)
methods = {
"stop": "d.stop",
"recheck": "d.check_hash",
"reannounce": "d.tracker_announce",
"remove": "d.erase",
}
if name == "set_label":
label = str(payload.get("label") or "").strip()
for h in torrent_hashes:
c.call("d.custom1.set", h, label)
return {"ok": True, "count": len(torrent_hashes), "label": label}
if name == "set_ratio_group":
group = str(payload.get("ratio_group") or "").strip()
for h in torrent_hashes:
c.call("d.custom.set", h, "py_ratio_group", group)
return {"ok": True, "count": len(torrent_hashes), "ratio_group": group}
if name == "move":
path = _remote_clean_path(payload.get("path") or "") path = _remote_clean_path(payload.get("path") or "")
move_data = bool(payload.get("move_data")) move_data = bool(payload.get("move_data"))
recheck = bool(payload.get("recheck", move_data)) recheck = bool(payload.get("recheck", move_data))
keep_seeding = bool(payload.get("keep_seeding")) keep_seeding = bool(payload.get("keep_seeding"))
# Note: keep_seeding lets automation move completed data to another path and force the torrent back into seeding. # Note: Automations can force seeding after a physical move even if the torrent was not active before.
if not path: if not path:
raise ValueError("Missing path") raise ValueError("Missing path")
results = [] results = []
@@ -1329,40 +1344,11 @@ def move_torrents(profile: dict, torrent_hashes: list[str], payload: dict | None
c.call("d.start", h) c.call("d.start", h)
item["started_after_move"] = True item["started_after_move"] = True
except Exception as exc: except Exception as exc:
item["start_error"] = str(exc) item["start_after_move_error"] = str(exc)
else: else:
c.call("d.directory.set", h, path) c.call("d.directory.set", h, path)
if keep_seeding:
try:
c.call("d.start", h)
item["started_after_path_change"] = True
except Exception as exc:
item["start_error"] = str(exc)
results.append(item) results.append(item)
return {"ok": True, "count": len(torrent_hashes), "move_data": move_data, "keep_seeding": keep_seeding, "results": results} return {"ok": True, "count": len(torrent_hashes), "move_data": move_data, "keep_seeding": keep_seeding, "results": results}
def action(profile: dict, torrent_hashes: list[str], name: str, payload: dict | None = None) -> dict:
payload = payload or {}
c = client_for(profile)
methods = {
"stop": "d.stop",
"recheck": "d.check_hash",
"reannounce": "d.tracker_announce",
"remove": "d.erase",
}
if name == "set_label":
label = str(payload.get("label") or "").strip()
for h in torrent_hashes:
c.call("d.custom1.set", h, label)
return {"ok": True, "count": len(torrent_hashes), "label": label}
if name == "set_ratio_group":
group = str(payload.get("ratio_group") or "").strip()
for h in torrent_hashes:
c.call("d.custom.set", h, "py_ratio_group", group)
return {"ok": True, "count": len(torrent_hashes), "ratio_group": group}
if name == "move":
# Note: Main move delegates to the shared helper used by automations.
return move_torrents(profile, torrent_hashes, payload)
if name == "pause": if name == "pause":
# Note: The app pause action is now a pure d.pause so later resume works without stop/start. # Note: The app pause action is now a pure d.pause so later resume works without stop/start.
results = [pause_hash(c, h) for h in torrent_hashes] results = [pause_hash(c, h) for h in torrent_hashes]

View File

@@ -132,39 +132,6 @@ def _excluded_hashes(profile_id: int, user_id: int) -> set[str]:
return {r['torrent_hash'] for r in list_exclusions(profile_id, user_id)} return {r['torrent_hash'] for r in list_exclusions(profile_id, user_id)}
def _label_names(value: str | None) -> list[str]:
names: list[str] = []
for part in str(value or '').replace(';', ',').replace('|', ',').split(','):
label = part.strip()
if label and label not in names:
names.append(label)
return names
def _label_value(labels: list[str]) -> str:
out: list[str] = []
for label in labels:
label = str(label or '').strip()
if label and label not in out:
out.append(label)
return ', '.join(out)
def _has_smart_queue_label(value: str | None) -> bool:
return SMART_QUEUE_LABEL in _label_names(value)
def _without_smart_queue_label(value: str | None) -> str:
return _label_value([label for label in _label_names(value) if label != SMART_QUEUE_LABEL])
def _with_smart_queue_label(value: str | None) -> str:
labels = _label_names(value)
if SMART_QUEUE_LABEL not in labels:
labels.append(SMART_QUEUE_LABEL)
return _label_value(labels)
def _remember_auto_label(profile_id: int, torrent_hash: str, previous_label: str) -> None: def _remember_auto_label(profile_id: int, torrent_hash: str, previous_label: str) -> None:
now = utcnow() now = utcnow()
with connect() as conn: with connect() as conn:
@@ -198,17 +165,20 @@ def _restore_auto_label(client: Any, profile_id: int, torrent_hash: str, current
(profile_id, torrent_hash), (profile_id, torrent_hash),
).fetchone() ).fetchone()
live_label = _read_label(client, torrent_hash, current_label or '') live_label = _read_label(client, torrent_hash, current_label or '')
if not row and not _has_smart_queue_label(live_label): if not row:
if live_label != SMART_QUEUE_LABEL:
return False return False
restored = _without_smart_queue_label(live_label)
previous = _without_smart_queue_label((row or {}).get('previous_label') or '')
if not restored and previous:
restored = previous
try: try:
# Note: Smart Queue now removes only its technical label, preserving labels added manually while the torrent was waiting in the queue. # Note: Clear the Smart Queue label even when the torrent was marked earlier but no previous-label entry remains.
if _has_smart_queue_label(live_label) or current_label is None: client.call('d.custom1.set', torrent_hash, '')
client.call('d.custom1.set', torrent_hash, restored) return True
if row: except Exception:
return False
previous = row.get('previous_label') or ''
try:
# Note: On resume, Smart Queue restores the previous label only while it still sees its own technical label.
if live_label == SMART_QUEUE_LABEL or current_label is None:
client.call('d.custom1.set', torrent_hash, previous)
conn.execute('DELETE FROM smart_queue_auto_labels WHERE profile_id=? AND torrent_hash=?', (profile_id, torrent_hash)) conn.execute('DELETE FROM smart_queue_auto_labels WHERE profile_id=? AND torrent_hash=?', (profile_id, torrent_hash))
return True return True
except Exception: except Exception:
@@ -312,12 +282,10 @@ def _read_live_start_state(client: Any, torrent_hash: str) -> dict[str, Any]:
result['started'] = bool(int(result.get('active') or 0)) result['started'] = bool(int(result.get('active') or 0))
return result return result
def _set_smart_queue_label(client: Any, torrent_hash: str, current_label: str | None = None, attempts: int = 3) -> bool: def _set_smart_queue_label(client: Any, torrent_hash: str, attempts: int = 3) -> bool:
# Note: The queue label is appended as a technical label instead of replacing the user's labels.
target = _with_smart_queue_label(current_label if current_label is not None else _read_label(client, torrent_hash, ''))
for attempt in range(max(1, attempts)): for attempt in range(max(1, attempts)):
try: try:
client.call('d.custom1.set', torrent_hash, target) client.call('d.custom1.set', torrent_hash, SMART_QUEUE_LABEL)
return True return True
except Exception: except Exception:
if attempt < attempts - 1: if attempt < attempts - 1:
@@ -330,15 +298,15 @@ def _mark_auto_paused(client: Any, profile_id: int, torrent: dict[str, Any]) ->
if not torrent_hash: if not torrent_hash:
return False return False
previous = str(torrent.get('label') or '') previous = str(torrent.get('label') or '')
if not _has_smart_queue_label(previous): if previous != SMART_QUEUE_LABEL:
_remember_auto_label(profile_id, torrent_hash, _without_smart_queue_label(previous)) _remember_auto_label(profile_id, torrent_hash, previous)
return _set_smart_queue_label(client, torrent_hash, previous) return _set_smart_queue_label(client, torrent_hash)
def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool = True) -> bool: def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool = True) -> bool:
if not torrent or int(torrent.get('complete') or 0): if not torrent or int(torrent.get('complete') or 0):
return False return False
if _has_smart_queue_label(torrent.get('label')): if str(torrent.get('label') or '') == SMART_QUEUE_LABEL:
return True return True
# Note: Paused in rTorrent usually has state=1 and active=0, so state=0 must not be required. # Note: Paused in rTorrent usually has state=1 and active=0, so state=0 must not be required.
# This lets Smart Queue treat paused torrents as pending and fill the queue target later. # This lets Smart Queue treat paused torrents as pending and fill the queue target later.
@@ -351,11 +319,11 @@ def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool =
def _clear_untracked_smart_queue_label(client: Any, torrent_hash: str, current_label: str) -> bool: def _clear_untracked_smart_queue_label(client: Any, torrent_hash: str, current_label: str) -> bool:
if not _has_smart_queue_label(current_label): if current_label != SMART_QUEUE_LABEL:
return False return False
try: try:
# Note: Orphan cleanup removes only the Smart Queue technical label and keeps manual labels intact. # Note: Clear an orphaned Smart Queue label when no previous-label entry exists in the database.
client.call('d.custom1.set', torrent_hash, _without_smart_queue_label(current_label)) client.call('d.custom1.set', torrent_hash, '')
return True return True
except Exception: except Exception:
return False return False
@@ -378,7 +346,7 @@ def _cleanup_auto_labels(client: Any, profile_id: int, torrents: list[dict[str,
if _restore_auto_label(client, profile_id, h, None if t is None else current_label): if _restore_auto_label(client, profile_id, h, None if t is None else current_label):
restored.append(h) restored.append(h)
continue continue
if not _has_smart_queue_label(current_label): if current_label != SMART_QUEUE_LABEL:
_set_smart_queue_label(client, h) _set_smart_queue_label(client, h)
for h, t in by_hash.items(): for h, t in by_hash.items():
@@ -395,7 +363,7 @@ def _is_running_download_slot(t: dict[str, Any]) -> bool:
# Paused can have state=1/open=1, so a slot is counted only after d.is_active=1. # Paused can have state=1/open=1, so a slot is counted only after d.is_active=1.
if int(t.get('complete') or 0): if int(t.get('complete') or 0):
return False return False
if _has_smart_queue_label(t.get('label')): if str(t.get('label') or '') == SMART_QUEUE_LABEL:
return False return False
status = str(t.get('status') or '').lower() status = str(t.get('status') or '').lower()
if status == 'checking' or status == 'paused' or bool(t.get('paused')): if status == 'checking' or status == 'paused' or bool(t.get('paused')):
@@ -407,7 +375,7 @@ def _is_waiting_download_candidate(t: dict[str, Any], manage_stopped: bool) -> b
"""Return True for paused/held torrents Smart Queue may resume later.""" """Return True for paused/held torrents Smart Queue may resume later."""
if int(t.get('complete') or 0): if int(t.get('complete') or 0):
return False return False
if _has_smart_queue_label(t.get('label')): if str(t.get('label') or '') == SMART_QUEUE_LABEL:
return True return True
# Note: Paused items are the primary source for filling the queue, regardless of manage_stopped. # Note: Paused items are the primary source for filling the queue, regardless of manage_stopped.
if bool(t.get('paused')) or str(t.get('status') or '').lower() == 'paused': if bool(t.get('paused')) or str(t.get('status') or '').lower() == 'paused':
@@ -438,7 +406,7 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
excluded = _excluded_hashes(profile_id, user_id) excluded = _excluded_hashes(profile_id, user_id)
manage_stopped = bool(settings.get('manage_stopped')) manage_stopped = bool(settings.get('manage_stopped'))
def is_managed_hold(t: dict[str, Any]) -> bool: def is_managed_hold(t: dict[str, Any]) -> bool:
return _has_smart_queue_label(t.get('label')) return str(t.get('label') or '') == SMART_QUEUE_LABEL
# Note: Count Smart Queue slots by d.is_active because Paused can have state=1/open=1 and must not occupy the limit. # Note: Count Smart Queue slots by d.is_active because Paused can have state=1/open=1 and must not occupy the limit.
downloading = [ downloading = [
@@ -570,7 +538,7 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
keep_labels = ( keep_labels = (
set(paused) set(paused)
| {str(t.get('hash') or '') for t in to_label_waiting} | {str(t.get('hash') or '') for t in to_label_waiting}
| {str(t.get('hash') or '') for t in stopped if _has_smart_queue_label(t.get('label')) and str(t.get('hash') or '') not in set(resumed)} | {str(t.get('hash') or '') for t in stopped if str(t.get('label') or '') == SMART_QUEUE_LABEL and str(t.get('hash') or '') not in set(resumed)}
) )
restored = _cleanup_auto_labels(c, profile_id, torrents, keep_labels, manage_stopped) restored = _cleanup_auto_labels(c, profile_id, torrents, keep_labels, manage_stopped)
details = {'excluded': len(excluded), 'enabled': bool(settings.get('enabled')), 'auto_label': SMART_QUEUE_LABEL, 'labels_restored': restored, 'labels_failed': label_failed, 'start_failed': start_failed, 'start_no_effect': start_no_effect, 'start_results': start_results, 'resume_requested': resume_requested, 'active_verified': active_verified, 'waiting_labeled': len(to_label_waiting), 'manage_stopped': manage_stopped, 'max_active_downloads': max_active, 'active_before': len(downloading), 'active_after_expected': active_after_pause + len(resumed), 'paused_planned': len(to_pause), 'resumed_planned': len(to_resume), 'rtorrent_cap': rtorrent_cap} details = {'excluded': len(excluded), 'enabled': bool(settings.get('enabled')), 'auto_label': SMART_QUEUE_LABEL, 'labels_restored': restored, 'labels_failed': label_failed, 'start_failed': start_failed, 'start_no_effect': start_no_effect, 'start_results': start_results, 'resume_requested': resume_requested, 'active_verified': active_verified, 'waiting_labeled': len(to_label_waiting), 'manage_stopped': manage_stopped, 'max_active_downloads': max_active, 'active_before': len(downloading), 'active_after_expected': active_after_pause + len(resumed), 'paused_planned': len(to_pause), 'resumed_planned': len(to_resume), 'rtorrent_cap': rtorrent_cap}

File diff suppressed because one or more lines are too long

View File

@@ -1238,37 +1238,60 @@ body.mobile-mode .mobile-card {
color: var(--bs-primary-text-emphasis); color: var(--bs-primary-text-emphasis);
} }
.automation-form-grid { .automation-shell {
display: grid;
gap: 0.75rem;
}
.automation-main-card {
padding: 0.75rem;
border: 1px solid var(--bs-border-color);
border-radius: 0.75rem;
background: var(--bs-body-bg);
}
.automation-card-title {
margin-bottom: 0.5rem;
font-weight: 700;
}
.automation-rule-grid,
.automation-builder-grid {
display: grid; display: grid;
grid-template-columns: repeat(4, minmax(160px, 1fr)); grid-template-columns: repeat(4, minmax(160px, 1fr));
gap: 0.5rem; gap: 0.5rem;
align-items: center; align-items: center;
} }
.automation-enabled,
.auto-move-option, .automation-negate {
.auto-condition-option {
gap: 0.45rem;
margin: 0; margin: 0;
} padding: 0.45rem 0.6rem 0.45rem 2.5rem;
.automation-builder-list {
display: grid;
grid-column: 1 / -1;
gap: 0.4rem;
}
.automation-chip {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
padding: 0.4rem 0.55rem;
border: 1px solid var(--bs-border-color); border: 1px solid var(--bs-border-color);
border-radius: 0.55rem; border-radius: 0.5rem;
background: var(--bs-secondary-bg); }
.automation-path-input {
grid-column: span 2;
}
.automation-chip-list {
display: flex;
flex-wrap: wrap;
gap: 0.45rem;
}
.automation-chip {
display: inline-flex;
align-items: center;
gap: 0.35rem;
max-width: 100%;
padding: 0.25rem 0.5rem;
border: 1px solid var(--bs-border-color);
border-radius: 999px;
background: var(--bs-tertiary-bg);
font-size: 0.82rem;
}
.automation-actions,
.automation-row-actions {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
align-items: center;
} }
.automation-row { .automation-row {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
@@ -1280,10 +1303,49 @@ body.mobile-mode .mobile-card {
margin-bottom: 0.45rem; margin-bottom: 0.45rem;
background: var(--bs-body-bg); background: var(--bs-body-bg);
} }
.automation-row-main {
min-width: 0;
}
.automation-action-pill {
display: inline-flex;
max-width: 100%;
margin: 0.1rem;
padding: 0.15rem 0.4rem;
border-radius: 999px;
background: var(--bs-secondary-bg);
font-size: 0.78rem;
white-space: normal;
}
.automation-history-details {
max-width: min(620px, 60vw);
}
.automation-history-details summary {
cursor: pointer;
list-style-position: inside;
}
.automation-history-details pre,
.automation-history-raw {
max-width: 100%;
max-height: 220px;
margin: 0.35rem 0 0;
padding: 0.5rem;
overflow: auto;
border: 1px solid var(--bs-border-color);
border-radius: 0.5rem;
background: var(--bs-tertiary-bg);
white-space: pre-wrap;
word-break: break-word;
}
@media (max-width: 900px) { @media (max-width: 900px) {
.automation-form-grid { .automation-rule-grid,
.automation-builder-grid {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
.automation-path-input,
.automation-history-details {
grid-column: auto;
max-width: 100%;
}
} }
.disk-status { .disk-status {
display: inline-flex; display: inline-flex;
@@ -2171,90 +2233,3 @@ body.mobile-mode .mobile-filter-bar {
width: 100%; width: 100%;
} }
} }
/* Note: Automation history uses compact cards with horizontal scrolling so long torrent names never expand the modal. */
.automation-history-toolbar {
display: flex;
justify-content: flex-end;
margin-bottom: 0.5rem;
}
.automation-history-carousel {
display: grid;
gap: 0.6rem;
max-height: 430px;
overflow-y: auto;
padding-right: 0.25rem;
}
.automation-history-card {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 0.6rem;
padding: 0.65rem;
border: 1px solid var(--bs-border-color);
border-radius: 0.7rem;
background: var(--bs-body-bg);
}
.automation-history-main {
min-width: 0;
}
.automation-history-title {
overflow: hidden;
font-weight: 600;
text-overflow: ellipsis;
white-space: nowrap;
}
.automation-history-buttons,
.automation-action-strip {
display: flex;
flex-wrap: wrap;
gap: 0.35rem;
}
.automation-history-buttons {
align-content: flex-start;
justify-content: flex-end;
}
.automation-history-detail {
grid-column: 1 / -1;
min-width: 0;
}
.automation-name-carousel {
display: flex;
gap: 0.4rem;
overflow-x: auto;
padding-bottom: 0.35rem;
}
.automation-name-carousel span {
flex: 0 0 auto;
max-width: 260px;
overflow: hidden;
padding: 0.25rem 0.45rem;
border: 1px solid var(--bs-border-color);
border-radius: 999px;
background: var(--bs-secondary-bg);
text-overflow: ellipsis;
white-space: nowrap;
}
.automation-action-detail-list {
display: grid;
gap: 0.45rem;
}
.automation-action-detail {
display: grid;
gap: 0.25rem;
min-width: 0;
padding: 0.5rem;
border-radius: 0.55rem;
background: var(--bs-tertiary-bg);
overflow-wrap: anywhere;
}

File diff suppressed because one or more lines are too long