mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(downloadclients): qBit rules add speed threshold condition (#652)
* fix: qbit add rules min check * feat(downloadclients): add check condition * feat(downloadclient): return on rejection
This commit is contained in:
parent
4ae2773dc9
commit
a6c1944df8
6 changed files with 194 additions and 34 deletions
|
@ -104,6 +104,8 @@ func (s *service) prepareQbitOptions(action *domain.Action) (map[string]string,
|
|||
func (s *service) qbittorrentCheckRulesCanDownload(ctx context.Context, action *domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
|
||||
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
|
||||
|
||||
checked := false
|
||||
|
||||
// check for active downloads and other rules
|
||||
if client.Settings.Rules.Enabled && !action.IgnoreRules {
|
||||
activeDownloads, err := qbt.GetTorrentsActiveDownloadsCtx(ctx)
|
||||
|
@ -117,33 +119,16 @@ func (s *service) qbittorrentCheckRulesCanDownload(ctx context.Context, action *
|
|||
// if max active downloads reached, check speed and if lower than threshold add anyway
|
||||
if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads {
|
||||
if client.Settings.Rules.IgnoreSlowTorrents {
|
||||
// check speeds of downloads
|
||||
info, err := qbt.GetTransferInfoCtx(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not get transfer info")
|
||||
if client.Settings.Rules.IgnoreSlowTorrentsCondition == domain.IgnoreSlowTorrentsModeMaxReached {
|
||||
rejections, err := s.qbittorrentCheckIgnoreSlow(ctx, client, qbt)
|
||||
if err != nil {
|
||||
return rejections, err
|
||||
}
|
||||
|
||||
s.log.Debug().Msg("active downloads are slower than set limit, lets add it")
|
||||
|
||||
checked = true
|
||||
}
|
||||
|
||||
// if current transfer speed is more than threshold return out and skip
|
||||
// DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold
|
||||
if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold {
|
||||
rejection := fmt.Sprintf("max active downloads reached and total download speed above threshold: %d, skipping", client.Settings.Rules.DownloadSpeedThreshold)
|
||||
|
||||
s.log.Debug().Msg(rejection)
|
||||
|
||||
return []string{rejection}, nil
|
||||
}
|
||||
|
||||
// if current transfer speed is more than threshold return out and skip
|
||||
// UpInfoSpeed is in bytes so lets convert to KB to match UploadSpeedThreshold
|
||||
if info.UpInfoSpeed/1024 >= client.Settings.Rules.UploadSpeedThreshold {
|
||||
rejection := fmt.Sprintf("max active downloads reached and total upload speed above threshold: %d, skipping", client.Settings.Rules.UploadSpeedThreshold)
|
||||
|
||||
s.log.Debug().Msg(rejection)
|
||||
|
||||
return []string{rejection}, nil
|
||||
}
|
||||
|
||||
s.log.Debug().Msg("active downloads are slower than set limit, lets add it")
|
||||
} else {
|
||||
rejection := "max active downloads reached, skipping"
|
||||
|
||||
|
@ -153,7 +138,56 @@ func (s *service) qbittorrentCheckRulesCanDownload(ctx context.Context, action *
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !checked && client.Settings.Rules.IgnoreSlowTorrentsCondition == domain.IgnoreSlowTorrentsModeAlways {
|
||||
rejections, err := s.qbittorrentCheckIgnoreSlow(ctx, client, qbt)
|
||||
if err != nil {
|
||||
return rejections, err
|
||||
}
|
||||
|
||||
if len(rejections) > 0 {
|
||||
return rejections, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *service) qbittorrentCheckIgnoreSlow(ctx context.Context, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
|
||||
// get transfer info
|
||||
info, err := qbt.GetTransferInfoCtx(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not get transfer info")
|
||||
}
|
||||
|
||||
s.log.Debug().Msgf("checking client ignore slow torrent rules: %+v", info)
|
||||
|
||||
if client.Settings.Rules.DownloadSpeedThreshold > 0 {
|
||||
// if current transfer speed is more than threshold return out and skip
|
||||
// DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold
|
||||
if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold {
|
||||
rejection := fmt.Sprintf("max active downloads reached and total download speed (%d) above threshold: (%d), skipping", info.DlInfoSpeed/1024, client.Settings.Rules.DownloadSpeedThreshold)
|
||||
|
||||
s.log.Debug().Msg(rejection)
|
||||
|
||||
return []string{rejection}, nil
|
||||
}
|
||||
}
|
||||
|
||||
if client.Settings.Rules.UploadSpeedThreshold > 0 {
|
||||
// if current transfer speed is more than threshold return out and skip
|
||||
// UpInfoSpeed is in bytes so lets convert to KB to match UploadSpeedThreshold
|
||||
if info.UpInfoSpeed/1024 >= client.Settings.Rules.UploadSpeedThreshold {
|
||||
rejection := fmt.Sprintf("max active downloads reached and total upload speed (%d) above threshold: (%d), skipping", info.UpInfoSpeed/1024, client.Settings.Rules.UploadSpeedThreshold)
|
||||
|
||||
s.log.Debug().Msg(rejection)
|
||||
|
||||
return []string{rejection}, nil
|
||||
}
|
||||
}
|
||||
|
||||
s.log.Debug().Msg("active downloads are slower than set limit, lets add it")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -44,11 +44,12 @@ type DownloadClientSettings struct {
|
|||
}
|
||||
|
||||
type DownloadClientRules struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
MaxActiveDownloads int `json:"max_active_downloads"`
|
||||
IgnoreSlowTorrents bool `json:"ignore_slow_torrents"`
|
||||
DownloadSpeedThreshold int64 `json:"download_speed_threshold"`
|
||||
UploadSpeedThreshold int64 `json:"upload_speed_threshold"`
|
||||
Enabled bool `json:"enabled"`
|
||||
MaxActiveDownloads int `json:"max_active_downloads"`
|
||||
IgnoreSlowTorrents bool `json:"ignore_slow_torrents"`
|
||||
IgnoreSlowTorrentsCondition IgnoreSlowTorrentsCondition `json:"ignore_slow_torrents_condition,omitempty"`
|
||||
DownloadSpeedThreshold int64 `json:"download_speed_threshold"`
|
||||
UploadSpeedThreshold int64 `json:"upload_speed_threshold"`
|
||||
}
|
||||
|
||||
type BasicAuth struct {
|
||||
|
@ -57,6 +58,13 @@ type BasicAuth struct {
|
|||
Password string `json:"password,omitempty"`
|
||||
}
|
||||
|
||||
type IgnoreSlowTorrentsCondition string
|
||||
|
||||
const (
|
||||
IgnoreSlowTorrentsModeAlways IgnoreSlowTorrentsCondition = "ALWAYS"
|
||||
IgnoreSlowTorrentsModeMaxReached IgnoreSlowTorrentsCondition = "MAX_DOWNLOADS_REACHED"
|
||||
)
|
||||
|
||||
type DownloadClientType string
|
||||
|
||||
const (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue