From fa191de6d33221c03412f4a6eaa56c9bec49fda9 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 22 Jul 2014 01:12:41 -0400 Subject: [PATCH] serve stats via /check --- http/routes.go | 5 ++++- stats/stats.go | 40 ++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/http/routes.go b/http/routes.go index bffc418..8fc6797 100644 --- a/http/routes.go +++ b/http/routes.go @@ -20,7 +20,10 @@ import ( const jsonContentType = "application/json; charset=UTF-8" func (s *Server) check(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { - _, err := w.Write([]byte("An easter egg goes here.")) + w.Header().Set("Content-Type", jsonContentType) + + e := json.NewEncoder(w) + err := e.Encode(stats.DefaultStats) if err != nil { return http.StatusInternalServerError, err } diff --git a/stats/stats.go b/stats/stats.go index 4476668..715c9bc 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -49,36 +49,36 @@ func init() { type PeerStats struct { // Stats for all peers. - Completed uint64 - Joined uint64 - Left uint64 - Reaped uint64 + Completed uint64 `json:"completed"` + Joined uint64 `json:"joined"` + Left uint64 `json:"left"` + Reaped uint64 `json:"reaped"` // Stats for seeds only (subset of total). - SeedsJoined uint64 - SeedsLeft uint64 - SeedsReaped uint64 + SeedsJoined uint64 `json:"seeds_joined"` + SeedsLeft uint64 `json:"seeds_left"` + SeedsReaped uint64 `json:"seeds_reaped"` } type Stats struct { - Start time.Time + Start time.Time `json:"time"` - Announces uint64 - Scrapes uint64 + Announces uint64 `json:"announces"` + Scrapes uint64 `json:"scrapes"` - IPv4Peers PeerStats - IPv6Peers PeerStats + IPv4Peers PeerStats `json:"ipv4_peers"` + IPv6Peers PeerStats `json:"ipv6_peers"` - TorrentsAdded uint64 - TorrentsRemoved uint64 - TorrentsReaped uint64 + TorrentsAdded uint64 `json:"torrents_added"` + TorrentsRemoved uint64 `json:"torrents_removed"` + TorrentsReaped uint64 `json:"torrents_reaped"` - ActiveConnections uint64 - ConnectionsAccepted uint64 - BytesTransmitted uint64 + ActiveConnections uint64 `json:"active_connections"` + ConnectionsAccepted uint64 `json:"connections_accepted"` + BytesTransmitted uint64 `json:"bytes_transmitted"` - RequestsHandled uint64 - RequestsErrored uint64 + RequestsHandled uint64 `json:"requests_handled"` + RequestsErrored uint64 `json:"requests_errored"` events chan int }