updates to admin stats

This commit is contained in:
AJ ONeal 2020-07-22 06:35:53 +00:00
parent 025fa5da6b
commit d5b2837033
1 changed files with 51 additions and 24 deletions

View File

@ -80,7 +80,8 @@ func InitAdmin(authURL string) {
})
})
r.Get("/subscribers", getSubscribers)
r.Get("/subscribers", getAllSubscribers)
r.Get("/subscribers/{subject}", getSubscribers)
r.Delete("/subscribers/{subject}", delSubscribers)
r.NotFound(apiNotFoundHandler)
})
@ -115,17 +116,51 @@ type SubscriberStatus struct {
// TODO bytes read
}
func getSubscribers(w http.ResponseWriter, r *http.Request) {
func getAllSubscribers(w http.ResponseWriter, r *http.Request) {
statuses := []*SubscriberStatus{}
table.Servers.Range(func(key, value interface{}) bool {
srvMap := value.(*sync.Map)
status := getSubscribersHelper(srvMap)
statuses = append(statuses, status)
return true
})
_ = json.NewEncoder(w).Encode(&struct {
Success bool `json:"success"`
Subscribers []*SubscriberStatus `json:"subscribers"`
}{
Success: true,
Subscribers: statuses,
})
}
func getSubscribers(w http.ResponseWriter, r *http.Request) {
subject := chi.URLParam(r, "subject")
statuses := &struct {
Success bool `json:"success"`
Subscribers []*SubscriberStatus `json:"subscribers"`
}{
Success: true,
Subscribers: []*SubscriberStatus{},
}
var srvMap *sync.Map
srvMapX, ok := table.Servers.Load(subject)
if ok {
srvMap = srvMapX.(*sync.Map)
statuses.Subscribers = append(statuses.Subscribers, getSubscribersHelper(srvMap))
}
_ = json.NewEncoder(w).Encode(statuses)
}
func getSubscribersHelper(srvMap *sync.Map) *SubscriberStatus {
status := &SubscriberStatus{
Since: nil,
Subject: "",
Sockets: []string{},
Clients: 0,
}
//subject := key.(string)
srvMap := value.(*sync.Map)
srvMap.Range(func(k, v interface{}) bool {
status.Sockets = append(status.Sockets, k.(string))
srv := v.(*table.SubscriberConn)
@ -141,16 +176,8 @@ func getSubscribers(w http.ResponseWriter, r *http.Request) {
return true
})
statuses = append(statuses, status)
return true
})
_ = json.NewEncoder(w).Encode(&struct {
Success bool `json:"success"`
Subscribers []*SubscriberStatus `json:"subscribers"`
}{
Success: true,
Subscribers: statuses,
})
return status
}
func delSubscribers(w http.ResponseWriter, r *http.Request) {