Replace piece availability with frequencies in Torrent status

This commit is contained in:
Matt Joiner 2021-12-23 17:01:39 +11:00
parent dbbe2fa71e
commit 9d07f3ea77
1 changed files with 29 additions and 6 deletions

View File

@ -593,6 +593,14 @@ func (t *Torrent) pieceAvailabilityRuns() (ret []pieceAvailabilityRun) {
return
}
func (t *Torrent) pieceAvailabilityFrequencies() (freqs []int) {
freqs = make([]int, t.numActivePeers()+1)
for i := range t.pieces {
freqs[t.piece(i).availability()]++
}
return
}
func (t *Torrent) pieceStateRuns() (ret PieceStateRuns) {
rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
ret = append(ret, PieceStateRun{
@ -677,6 +685,9 @@ func (t *Torrent) writeStatus(w io.Writer) {
if t.info != nil {
fmt.Fprintf(w, "Num Pieces: %d (%d completed)\n", t.numPieces(), t.numPiecesCompleted())
fmt.Fprintf(w, "Piece States: %s\n", t.pieceStateRuns())
// Generates a huge, unhelpful listing when piece availability is very scattered. Prefer
// availability frequencies instead.
if false {
fmt.Fprintf(w, "Piece availability: %v\n", strings.Join(func() (ret []string) {
for _, run := range t.pieceAvailabilityRuns() {
ret = append(ret, run.String())
@ -684,6 +695,18 @@ func (t *Torrent) writeStatus(w io.Writer) {
return
}(), " "))
}
fmt.Fprintf(w, "Piece availability frequency: %v\n", strings.Join(
func() (ret []string) {
for avail, freq := range t.pieceAvailabilityFrequencies() {
if freq == 0 {
continue
}
ret = append(ret, fmt.Sprintf("%v: %v", avail, freq))
}
return
}(),
", "))
}
fmt.Fprintf(w, "Reader Pieces:")
t.forReaderOffsetPieces(func(begin, end pieceIndex) (again bool) {
fmt.Fprintf(w, " %d:%d", begin, end)