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:
ze0s 2023-01-17 23:34:03 +01:00 committed by GitHub
parent 4ae2773dc9
commit a6c1944df8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 194 additions and 34 deletions

View file

@ -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
}