smart queue logs

This commit is contained in:
Mateusz Gruszczyński
2026-05-20 21:55:29 +02:00
parent 6ab330f583
commit f4d8611240
3 changed files with 31 additions and 3989 deletions

View File

@@ -311,6 +311,33 @@ def count_history(profile_id: int, user_id: int | None = None) -> int:
).fetchone()
return int((row or {}).get('count') or 0)
def _latest_history_event(profile_id: int, user_id: int | None = None) -> str:
"""Return the newest Smart Queue history event for duplicate suppression."""
# Note: Disabled Smart Queue should leave one waiting marker, not a poller-generated log stream.
user_id = user_id or default_user_id()
with connect() as conn:
row = conn.execute(
'SELECT event FROM smart_queue_history WHERE user_id=? AND profile_id=? ORDER BY created_at DESC LIMIT 1',
(user_id, profile_id),
).fetchone()
return str((row or {}).get('event') or '')
def _record_disabled_waiting_once(profile_id: int, user_id: int, details: dict[str, Any] | None = None) -> bool:
"""Record one disabled-state history row until Smart Queue runs or changes state again."""
# Note: This keeps the UI audit trail useful without creating repeated disabled logs on every poll.
if _latest_history_event(profile_id, user_id) in {'disabled_waiting_start', 'auto_stopped_idle'}:
return False
payload = {
'decision': 'Smart Queue disabled, waiting for start',
'enabled': False,
**(details or {}),
}
add_history(profile_id, 'disabled_waiting_start', [], [], 0, payload, user_id)
return True
def _excluded_hashes(profile_id: int, user_id: int) -> set[str]:
return {r['torrent_hash'] for r in list_exclusions(profile_id, user_id)}
@@ -1105,8 +1132,9 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
restored = _cleanup_auto_labels(rtorrent.client_for(profile), profile_id, torrents, set(), True)
except Exception:
restored = []
add_history(profile_id, 'skipped_disabled', [], [], 0, {'enabled': False, 'labels_restored': restored}, user_id)
return {'ok': True, 'enabled': False, 'paused': [], 'resumed': [], 'stopped': [], 'started': [], 'labels_restored': restored, 'message': 'Smart Queue disabled'}
# Note: Disabled checks are frequent poller passes; record only the first waiting-state row.
disabled_log_recorded = _record_disabled_waiting_once(profile_id, user_id, {'labels_restored': restored})
return {'ok': True, 'enabled': False, 'paused': [], 'resumed': [], 'stopped': [], 'started': [], 'labels_restored': restored, 'disabled_log_recorded': disabled_log_recorded, 'message': 'Smart Queue disabled, waiting for start'}
torrents = rtorrent.list_torrents(profile)
# Note: Stalled labels block automatic starting only; a manually started Stalled item still counts as a running slot.

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff