mirror of
https://github.com/unpoller/unpoller.git
synced 2026-10-21 01:58:06 +02:00
add filters to clients, devices, eventgroups
This commit is contained in:
@@ -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]})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package webserver
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -64,9 +65,11 @@ 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 {
|
||||
groups = append(groups, n)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user