fs/accounting: sort transfers by start time

This commit is contained in:
Max Sum 2020-06-16 23:18:32 +08:00 committed by Nick Craig-Wood
parent e2183ad661
commit bfa5715017
2 changed files with 23 additions and 11 deletions

View file

@ -72,8 +72,8 @@ func (s *StatsInfo) RemoteStats() (out rc.Params, err error) {
var c []string var c []string
s.checking.mu.RLock() s.checking.mu.RLock()
defer s.checking.mu.RUnlock() defer s.checking.mu.RUnlock()
for name := range s.checking.items { for _, tr := range s.checking.sortedSlice() {
c = append(c, name) c = append(c, tr.remote)
} }
out["checking"] = c out["checking"] = c
} }
@ -81,8 +81,8 @@ func (s *StatsInfo) RemoteStats() (out rc.Params, err error) {
s.transferring.mu.RLock() s.transferring.mu.RLock()
var t []rc.Params var t []rc.Params
for name, tr := range s.transferring.items { for _, tr := range s.transferring.sortedSlice() {
if acc := s.inProgress.get(name); acc != nil { if acc := s.inProgress.get(tr.remote); acc != nil {
t = append(t, acc.RemoteStats()) t = append(t, acc.RemoteStats())
} else { } else {
t = append(t, s.transferRemoteStats(tr)) t = append(t, s.transferRemoteStats(tr))

View file

@ -63,36 +63,48 @@ func (tm *transferMap) count() int {
return len(tm.items) return len(tm.items)
} }
// sortedSlice returns all transfers sorted by start time
func (tm *transferMap) sortedSlice() []*Transfer {
tm.mu.RLock()
defer tm.mu.RUnlock()
s := make([]*Transfer, 0, len(tm.items))
for _, tr := range tm.items {
s = append(s, tr)
}
sort.Slice(s, func(i, j int) bool {
return s[i].startedAt.Before(s[j].startedAt)
})
return s
}
// String returns string representation of map items excluding any in // String returns string representation of map items excluding any in
// exclude (if set). // exclude (if set).
func (tm *transferMap) String(progress *inProgress, exclude *transferMap) string { func (tm *transferMap) String(progress *inProgress, exclude *transferMap) string {
tm.mu.RLock() tm.mu.RLock()
defer tm.mu.RUnlock() defer tm.mu.RUnlock()
strngs := make([]string, 0, len(tm.items)) strngs := make([]string, 0, len(tm.items))
for name, _ := range tm.items { for _, tr := range tm.sortedSlice() {
if exclude != nil { if exclude != nil {
exclude.mu.RLock() exclude.mu.RLock()
_, found := exclude.items[name] _, found := exclude.items[tr.remote]
exclude.mu.RUnlock() exclude.mu.RUnlock()
if found { if found {
continue continue
} }
} }
var out string var out string
if acc := progress.get(name); acc != nil { if acc := progress.get(tr.remote); acc != nil {
out = acc.String() out = acc.String()
} else { } else {
out = fmt.Sprintf("%*s: %s", out = fmt.Sprintf("%*s: %s",
fs.Config.StatsFileNameLength, fs.Config.StatsFileNameLength,
shortenName(name, fs.Config.StatsFileNameLength), shortenName(tr.remote, fs.Config.StatsFileNameLength),
tm.name, tm.name,
) )
} }
strngs = append(strngs, " * "+out) strngs = append(strngs, " * "+out)
} }
sorted := sort.StringSlice(strngs) return strings.Join(strngs, "\n")
sorted.Sort()
return strings.Join(sorted, "\n")
} }
// progress returns total bytes read as well as the size. // progress returns total bytes read as well as the size.