Add current peer stats

This commit is contained in:
Justin Li 2014-07-22 23:29:30 -04:00
parent 727370fedc
commit de69eaeafe
3 changed files with 46 additions and 20 deletions

View file

@ -51,15 +51,17 @@ func init() {
type PeerStats struct { type PeerStats struct {
// Stats for all peers. // Stats for all peers.
Completed uint64 `json:"completed"` Completed uint64 `json:"completed"` // Number of transitions from leech to seed.
Joined uint64 `json:"joined"` Joined uint64 `json:"joined"` // Total peers that announced.
Left uint64 `json:"left"` Left uint64 `json:"left"` // Total peers that paused or stopped.
Reaped uint64 `json:"reaped"` Reaped uint64 `json:"reaped"` // Total peers cleaned up after inactivity.
Current uint64 `json:"current"` // Current total peer count.
// Stats for seeds only (subset of total). // Stats for seeds only (subset of total).
SeedsJoined uint64 `json:"seeds_joined"` SeedsJoined uint64 `json:"seeds_joined"` // Seeds that announced (does not included leechers that completed).
SeedsLeft uint64 `json:"seeds_left"` SeedsLeft uint64 `json:"seeds_left"` // Seeds that paused or stopped.
SeedsReaped uint64 `json:"seeds_reaped"` SeedsReaped uint64 `json:"seeds_reaped"` // Seeds cleaned up after inactivity.
SeedsCurrent uint64 `json:"seeds_current"` // Current seed count.
} }
type PercentileTimes struct { type PercentileTimes struct {
@ -69,10 +71,10 @@ type PercentileTimes struct {
} }
type Stats struct { type Stats struct {
Start time.Time `json:"start_time"` Start time.Time `json:"start_time"` // Time at which Chihaya was booted.
Announces uint64 `json:"announces"` Announces uint64 `json:"announces"` // Total number of announces.
Scrapes uint64 `json:"scrapes"` Scrapes uint64 `json:"scrapes"` // Total number of scrapes.
IPv4Peers PeerStats `json:"ipv4_peers"` IPv4Peers PeerStats `json:"ipv4_peers"`
IPv6Peers PeerStats `json:"ipv6_peers"` IPv6Peers PeerStats `json:"ipv6_peers"`
@ -144,41 +146,61 @@ func (s *Stats) handleEvents() {
case CompletedIPv4: case CompletedIPv4:
s.IPv4Peers.Completed++ s.IPv4Peers.Completed++
s.IPv4Peers.SeedsCurrent++
case NewLeechIPv4: case NewLeechIPv4:
s.IPv4Peers.Joined++ s.IPv4Peers.Joined++
s.IPv4Peers.Current++
case DeletedLeechIPv4: case DeletedLeechIPv4:
s.IPv4Peers.Left++ s.IPv4Peers.Left++
s.IPv4Peers.Current--
case ReapedLeechIPv4: case ReapedLeechIPv4:
s.IPv4Peers.Reaped++ s.IPv4Peers.Reaped++
s.IPv4Peers.Current--
case NewSeedIPv4: case NewSeedIPv4:
s.IPv4Peers.SeedsJoined++ s.IPv4Peers.SeedsJoined++
s.IPv4Peers.SeedsCurrent++
s.IPv4Peers.Joined++ s.IPv4Peers.Joined++
s.IPv4Peers.Current++
case DeletedSeedIPv4: case DeletedSeedIPv4:
s.IPv4Peers.SeedsLeft++ s.IPv4Peers.SeedsLeft++
s.IPv4Peers.SeedsCurrent--
s.IPv4Peers.Left++ s.IPv4Peers.Left++
s.IPv4Peers.Current--
case ReapedSeedIPv4: case ReapedSeedIPv4:
s.IPv4Peers.SeedsReaped++ s.IPv4Peers.SeedsReaped++
s.IPv4Peers.SeedsCurrent--
s.IPv4Peers.Reaped++ s.IPv4Peers.Reaped++
s.IPv4Peers.Current--
case CompletedIPv6: case CompletedIPv6:
s.IPv6Peers.Completed++ s.IPv6Peers.Completed++
s.IPv6Peers.SeedsCurrent++
case NewLeechIPv6: case NewLeechIPv6:
s.IPv6Peers.Joined++ s.IPv6Peers.Joined++
s.IPv6Peers.Current++
case DeletedLeechIPv6: case DeletedLeechIPv6:
s.IPv6Peers.Left++ s.IPv6Peers.Left++
s.IPv6Peers.Current--
case ReapedLeechIPv6: case ReapedLeechIPv6:
s.IPv6Peers.Reaped++ s.IPv6Peers.Reaped++
s.IPv6Peers.Current--
case NewSeedIPv6: case NewSeedIPv6:
s.IPv6Peers.SeedsJoined++ s.IPv6Peers.SeedsJoined++
s.IPv6Peers.SeedsCurrent++
s.IPv6Peers.Joined++ s.IPv6Peers.Joined++
s.IPv6Peers.Current++
case DeletedSeedIPv6: case DeletedSeedIPv6:
s.IPv6Peers.SeedsLeft++ s.IPv6Peers.SeedsLeft++
s.IPv6Peers.SeedsCurrent--
s.IPv6Peers.Left++ s.IPv6Peers.Left++
s.IPv6Peers.Current--
case ReapedSeedIPv6: case ReapedSeedIPv6:
s.IPv6Peers.SeedsReaped++ s.IPv6Peers.SeedsReaped++
s.IPv6Peers.SeedsCurrent--
s.IPv6Peers.Reaped++ s.IPv6Peers.Reaped++
s.IPv6Peers.Current--
case NewTorrent: case NewTorrent:
s.TorrentsAdded++ s.TorrentsAdded++

View file

@ -196,11 +196,6 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
} }
} }
snatched = true snatched = true
if p.IPv4() {
stats.RecordEvent(stats.CompletedIPv4)
} else {
stats.RecordEvent(stats.CompletedIPv6)
}
case t.InLeecherPool(p) && ann.Left == 0: case t.InLeecherPool(p) && ann.Left == 0:
// A leecher completed but the event was never received. // A leecher completed but the event was never received.
@ -208,7 +203,6 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
if err != nil { if err != nil {
return return
} }
// TODO Should this return snatched=true and stats for completed?
} }
return return

View file

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker/models" "github.com/chihaya/chihaya/tracker/models"
) )
@ -99,10 +100,19 @@ type Conn interface {
// leecherFinished moves a peer from the leeching pool to the seeder pool. // leecherFinished moves a peer from the leeching pool to the seeder pool.
func leecherFinished(c Conn, infohash string, p *models.Peer) error { func leecherFinished(c Conn, infohash string, p *models.Peer) error {
err := c.DeleteLeecher(infohash, p.ID) if err := c.DeleteLeecher(infohash, p.ID); err != nil {
if err != nil {
return err return err
} }
err = c.PutSeeder(infohash, p)
if err := c.PutSeeder(infohash, p); err != nil {
return err return err
}
if p.IPv4() {
stats.RecordEvent(stats.CompletedIPv4)
} else {
stats.RecordEvent(stats.CompletedIPv6)
}
return nil
} }