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

View File

@ -1,6 +1,7 @@
package webserver package webserver
import ( import (
"strings"
"sync" "sync"
"time" "time"
) )
@ -64,9 +65,11 @@ type Event struct {
Tags map[string]string `json:"tags,omitempty"` 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 { for n := range e {
groups = append(groups, n) if filter == "" || strings.HasPrefix(n, filter) {
groups = append(groups, n)
}
} }
return groups return groups
@ -105,6 +108,16 @@ type Device struct {
Config interface{} `json:"config,omitempty"` 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. // Clients is a list of clients with their data.
type Clients []*Client type Clients []*Client
@ -123,3 +136,13 @@ type Client struct {
Since time.Time `json:"since"` Since time.Time `json:"since"`
Last time.Time `json:"last"` 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
}