1
0
Fork 0

Rates API

This commit is contained in:
Gregory Eremin 2015-01-27 13:57:44 +07:00
parent 442ba2218d
commit 41d2efea8b
3 changed files with 34 additions and 20 deletions

View File

@ -90,12 +90,9 @@ func (h *Hub) Info() map[string]map[string]int64 {
info := make(map[string]map[string]int64)
for queue, size := range h.storage.QueueSizes() {
inRate, outRate := h.statistics.Rates(queue)
info[queue] = map[string]int64{
"messages": size,
"subscriptions": 0,
"in_rate": inRate,
"out_rate": outRate,
}
}
for _, sub := range h.subscribers {
@ -113,13 +110,12 @@ func (h *Hub) Info() map[string]map[string]int64 {
return info
}
func (h *Hub) RateHistory() map[string]map[string][]int64 {
hist := map[string]map[string][]int64{}
for queue, _ := range h.storage.QueueSizes() {
hist[queue] = h.statistics.RateHistory(queue)
}
func (h *Hub) Rates(queue string) (in, out int64) {
return h.statistics.Rates(queue)
}
return hist
func (h *Hub) RateHistory(queue string) (in, out []int64) {
return h.statistics.RateHistory(queue)
}
func (h *Hub) StorageInfo() map[string]interface{} {

View File

@ -48,9 +48,29 @@ func (s *Server) Start() {
}
func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) {
info := s.hub.Info()
jsn, _ := json.Marshal(info)
var (
res = map[string]map[string]interface{}{}
info = s.hub.Info()
withRates = (r.FormValue("rates") != "")
)
for queue, meta := range info {
res[queue] = map[string]interface{}{}
for key, val := range meta {
res[queue][key] = val
}
if withRates {
inRate, outRate := s.hub.Rates(queue)
inHist, outHist := s.hub.RateHistory(queue)
res[queue]["in_rate"] = inRate
res[queue]["out_rate"] = outRate
res[queue]["in_rate_history"] = inHist
res[queue]["out_rate_history"] = outHist
}
}
jsn, _ := json.Marshal(res)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(jsn)
}

View File

@ -24,7 +24,7 @@ type (
func New() *Stats {
s := &Stats{
q: map[string]*meta{},
queues: map[string]*meta{},
}
go s.loopCollectSeconds()
@ -50,18 +50,16 @@ func (s *Stats) Rates(queue string) (in, out int64) {
return p.in, p.out
}
func (s *Stats) RateHistory(queue string) map[string][]int64 {
hist := map[string][]int64{
"in": []int64{},
"out": []int64{},
}
func (s *Stats) RateHistory(queue string) (in, out []int64) {
in = []int64{}
out = []int64{}
for _, p := range s.metaFor(queue).points {
hist["in"] = append(hist["in"], p.in)
hist["out"] = append(hist["out"], p.out)
in = append(in, p.in)
out = append(out, p.out)
}
return hist
return in, out
}
func (s *Stats) loopCollectSeconds() {