add filters to clients, devices, eventgroups

This commit is contained in:
davidnewhall2 2020-07-06 00:44:54 -07:00
parent 3aa0bab6b2
commit a4da890bb0
2 changed files with 29 additions and 6 deletions

View File

@ -69,7 +69,7 @@ func (s *Server) handleOutput(w http.ResponseWriter, r *http.Request) {
default:
s.handleJSON(w, c.Config)
case "eventgroups":
s.handleJSON(w, c.Events.Groups())
s.handleJSON(w, c.Events.Groups(val))
case "events":
switch events, ok := c.Events[val]; {
case val == "":
@ -105,7 +105,7 @@ func (s *Server) handleInput(w http.ResponseWriter, r *http.Request) {
default:
s.handleJSON(w, c.Config)
case "eventgroups":
s.handleJSON(w, c.Events.Groups())
s.handleJSON(w, c.Events.Groups(val))
case "events":
switch events, ok := c.Events[val]; {
case val == "":
@ -118,9 +118,9 @@ func (s *Server) handleInput(w http.ResponseWriter, r *http.Request) {
case "sites":
s.handleJSON(w, c.Sites)
case "devices":
s.handleJSON(w, c.Devices)
s.handleJSON(w, c.Devices.Filter(val))
case "clients":
s.handleJSON(w, c.Clients)
s.handleJSON(w, c.Clients.Filter(val))
case "counters":
if val != "" {
s.handleJSON(w, map[string]int64{val: c.Counter[val]})

View File

@ -1,6 +1,7 @@
package webserver
import (
"strings"
"sync"
"time"
)
@ -64,10 +65,12 @@ type Event struct {
Tags map[string]string `json:"tags,omitempty"`
}
func (e Events) Groups() (groups []string) {
func (e Events) Groups(filter string) (groups []string) {
for n := range e {
if filter == "" || strings.HasPrefix(n, filter) {
groups = append(groups, n)
}
}
return groups
}
@ -105,6 +108,16 @@ type Device struct {
Config interface{} `json:"config,omitempty"`
}
func (c Devices) Filter(siteid string) (devices []*Device) {
for _, n := range c {
if siteid == "" || n.SiteID == siteid {
devices = append(devices, n)
}
}
return devices
}
// Clients is a list of clients with their data.
type Clients []*Client
@ -123,3 +136,13 @@ type Client struct {
Since time.Time `json:"since"`
Last time.Time `json:"last"`
}
func (c Clients) Filter(siteid string) (clients []*Client) {
for _, n := range c {
if siteid == "" || n.SiteID == siteid {
clients = append(clients, n)
}
}
return clients
}