2015-11-16 13:07:02 -08:00
|
|
|
package server
|
|
|
|
|
2015-11-16 13:25:25 -08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2015-11-16 13:07:02 -08:00
|
|
|
type StatsData struct {
|
2015-11-16 20:35:03 -08:00
|
|
|
CurrentClientCount int64
|
|
|
|
|
|
|
|
ClientConnectsTotal int64
|
|
|
|
ClientDisconnectsTotal int64
|
2015-11-16 13:07:02 -08:00
|
|
|
|
2015-11-16 13:25:25 -08:00
|
|
|
DisconnectCodes map[string]int64
|
2015-11-16 13:07:02 -08:00
|
|
|
DisconnectReasons map[string]int64
|
|
|
|
|
|
|
|
CommandsIssuedTotal int64
|
2015-11-16 13:25:25 -08:00
|
|
|
CommandsIssuedMap map[Command]int64
|
2015-11-16 13:07:02 -08:00
|
|
|
|
|
|
|
MessagesSent int64
|
2015-11-16 13:25:25 -08:00
|
|
|
|
|
|
|
Version int
|
2015-11-16 13:07:02 -08:00
|
|
|
}
|
|
|
|
|
2015-11-16 13:25:25 -08:00
|
|
|
const StatsDataVersion = 1
|
|
|
|
|
2015-11-16 13:07:02 -08:00
|
|
|
func newStatsData() *StatsData {
|
|
|
|
return &StatsData{
|
|
|
|
CommandsIssuedMap: make(map[Command]int64),
|
2015-11-16 13:25:25 -08:00
|
|
|
DisconnectCodes: make(map[string]int64),
|
2015-11-16 13:07:02 -08:00
|
|
|
DisconnectReasons: make(map[string]int64),
|
2015-11-16 13:25:25 -08:00
|
|
|
Version: StatsDataVersion,
|
2015-11-16 13:07:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statistics is several variables that get incremented during normal operation of the server.
|
2015-11-16 13:25:25 -08:00
|
|
|
// Its structure should be versioned as it is exposed via JSON.
|
2015-11-16 13:07:02 -08:00
|
|
|
var Statistics = newStatsData()
|
2015-11-16 13:25:25 -08:00
|
|
|
|
|
|
|
func HTTPShowStatistics(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
jsonBytes, _ := json.Marshal(Statistics)
|
|
|
|
outBuf := bytes.NewBuffer(nil)
|
|
|
|
json.Indent(outBuf, jsonBytes, "", "\t")
|
|
|
|
|
|
|
|
outBuf.WriteTo(w)
|
|
|
|
}
|