func (j *JSONStats) ResetAnnounce() {
	atomic.StoreInt64(&j.announce, 0)
}
ResetAnnounce atomically sets counter to 0
package stats
import (
	"encoding/json"
	"fmt"
	"net/http"
	"sync/atomic"
	log "github.com/sirupsen/logrus"
)
type JSONStats struct {
	invalidFormat int64
	requests      int64
	responses     int64
	listeners     int64
	workers       int64
	readError     int64
	announce      int64
}
func (j *JSONStats) toMap() (export map[string]int64) {
	export = make(map[string]int64)
	export["invalidformat"] = j.invalidFormat
	export["requests"] = j.requests
	export["responses"] = j.responses
	export["listeners"] = j.listeners
	export["workers"] = j.workers
	export["readError"] = j.readError
	export["announce"] = j.announce
	return export
}
func (j *JSONStats) handleRequest(w http.ResponseWriter, r *http.Request) {
	js, err := json.Marshal(j.toMap())
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "application/json")
	if _, err = w.Write(js); err != nil {
		log.Errorf("Failed to reply: %v", err)
	}
}
func (j *JSONStats) Start(port int) {
	http.HandleFunc("/", j.handleRequest)
	addr := fmt.Sprintf(":%d", port)
	log.Debugf("Starting http json server on %s", addr)
	err := http.ListenAndServe(addr, nil)
	if err != nil {
		log.Errorf("Failed to start listener: %v", err)
	}
}
func (j *JSONStats) IncInvalidFormat() {
	atomic.AddInt64(&j.invalidFormat, 1)
}
func (j *JSONStats) IncRequests() {
	atomic.AddInt64(&j.requests, 1)
}
func (j *JSONStats) IncResponses() {
	atomic.AddInt64(&j.responses, 1)
}
func (j *JSONStats) IncListeners() {
	atomic.AddInt64(&j.listeners, 1)
}
func (j *JSONStats) IncWorkers() {
	atomic.AddInt64(&j.workers, 1)
}
func (j *JSONStats) IncReadError() {
	atomic.AddInt64(&j.readError, 1)
}
func (j *JSONStats) DecListeners() {
	atomic.AddInt64(&j.listeners, -1)
}
func (j *JSONStats) DecWorkers() {
	atomic.AddInt64(&j.workers, -1)
}
func (j *JSONStats) SetAnnounce() {
	atomic.StoreInt64(&j.announce, 1)
}