rc: add add short parameter to core/stats to not return transferring and checking

This commit is contained in:
Nick Craig-Wood 2025-03-13 11:32:52 +00:00
parent 63c4fef27a
commit e47f59e1f9
4 changed files with 40 additions and 12 deletions

View File

@ -92,7 +92,9 @@ func NewStats(ctx context.Context) *StatsInfo {
} }
// RemoteStats returns stats for rc // RemoteStats returns stats for rc
func (s *StatsInfo) RemoteStats() (out rc.Params, err error) { //
// If short is true then the transfers and checkers won't be added.
func (s *StatsInfo) RemoteStats(short bool) (out rc.Params, err error) {
// NB if adding values here - make sure you update the docs in // NB if adding values here - make sure you update the docs in
// stats_groups.go // stats_groups.go
@ -128,10 +130,10 @@ func (s *StatsInfo) RemoteStats() (out rc.Params, err error) {
} }
s.mu.RUnlock() s.mu.RUnlock()
if !s.checking.empty() { if !short && !s.checking.empty() {
out["checking"] = s.checking.remotes() out["checking"] = s.checking.remotes()
} }
if !s.transferring.empty() { if !short && !s.transferring.empty() {
out["transferring"] = s.transferring.rcStats(s.inProgress) out["transferring"] = s.transferring.rcStats(s.inProgress)
} }
if s.errors > 0 { if s.errors > 0 {
@ -561,7 +563,7 @@ func (s *StatsInfo) Transferred() []TransferSnapshot {
// Log outputs the StatsInfo to the log // Log outputs the StatsInfo to the log
func (s *StatsInfo) Log() { func (s *StatsInfo) Log() {
if s.ci.UseJSONLog { if s.ci.UseJSONLog {
out, _ := s.RemoteStats() out, _ := s.RemoteStats(false)
fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v%v\n", s, fs.LogValueHide("stats", out)) fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v%v\n", s, fs.LogValueHide("stats", out))
} else { } else {
fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v\n", s) fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v\n", s)

View File

@ -56,11 +56,13 @@ func rcRemoteStats(ctx context.Context, in rc.Params) (rc.Params, error) {
if rc.NotErrParamNotFound(err) { if rc.NotErrParamNotFound(err) {
return rc.Params{}, err return rc.Params{}, err
} }
short, _ := in.GetBool("short")
if group != "" { if group != "" {
return StatsGroup(ctx, group).RemoteStats() return StatsGroup(ctx, group).RemoteStats(short)
} }
return groups.sum(ctx).RemoteStats() return groups.sum(ctx).RemoteStats(short)
} }
func init() { func init() {
@ -78,7 +80,8 @@ returned.
Parameters Parameters
- group - name of the stats group (string) - group - name of the stats group (string, optional)
- short - if true will not return the transferring and checking arrays (boolean, optional)
Returns the following values: Returns the following values:

View File

@ -2,6 +2,7 @@ package accounting
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"runtime" "runtime"
"testing" "testing"
@ -10,6 +11,7 @@ import (
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/fserrors" "github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/fs/rc" "github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fstest/mockobject"
"github.com/rclone/rclone/fstest/testy" "github.com/rclone/rclone/fstest/testy"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -141,14 +143,35 @@ func TestStatsGroupOperations(t *testing.T) {
}) })
t.Run("core/stats", func(t *testing.T) { t.Run("core/stats", func(t *testing.T) {
tr := Stats(ctx).NewCheckingTransfer(mockobject.New("core-check"), "deleting")
// defer tr.Done(ctx, nil)
_ = tr // don't finish the transfer so we don't mess up the other tests
tr2 := Stats(ctx).NewTransfer(mockobject.New("core-transfer"), nil)
//defer tr2.Done(ctx, nil)
_ = tr2 // don't finish the transfer so we don't mess up the other tests
call := rc.Calls.Get("core/stats") call := rc.Calls.Get("core/stats")
require.NotNil(t, call) require.NotNil(t, call)
gotNoGroup, err := call.Fn(ctx, rc.Params{})
got, err := call.Fn(ctx, rc.Params{})
require.NoError(t, err) require.NoError(t, err)
gotGroup, err := call.Fn(ctx, rc.Params{"group": "test-group"}) assert.Equal(t, int64(42), got["deletes"])
data, err := json.Marshal(got["transferring"])
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(42), gotNoGroup["deletes"]) assert.Contains(t, string(data), "core-transfer")
assert.Equal(t, int64(1), gotGroup["deletes"]) data, err = json.Marshal(got["checking"])
require.NoError(t, err)
assert.Contains(t, string(data), "core-check")
got, err = call.Fn(ctx, rc.Params{"short": true})
require.NoError(t, err)
assert.Equal(t, int64(42), got["deletes"])
assert.Nil(t, got["transferring"])
assert.Nil(t, got["checking"])
got, err = call.Fn(ctx, rc.Params{"group": "test-group"})
require.NoError(t, err)
assert.Equal(t, int64(1), got["deletes"])
}) })
t.Run("core/transferred", func(t *testing.T) { t.Run("core/transferred", func(t *testing.T) {

View File

@ -265,7 +265,7 @@ func TestRemoteStats(t *testing.T) {
} }
s.AddTransfer(tr1) s.AddTransfer(tr1)
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
rs, err := s.RemoteStats() rs, err := s.RemoteStats(false)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, float64(10), rs["transferTime"]) assert.Equal(t, float64(10), rs["transferTime"])