-
Notifications
You must be signed in to change notification settings - Fork 11
/
stats.go
48 lines (43 loc) · 1.22 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import "encoding/json"
type stats struct {
Service string `json:"service"`
Source string `json:"source"`
Version string `json:"version"`
Uptime float64 `json:"uptime"`
Timestamp float64 `json:"timestamp"`
TotalConnections float64 `json:"total_connections"`
CurrConnections float64 `json:"curr_connections"`
Pools map[string]*pool
}
func (s *stats) UnmarshalJSON(b []byte) error {
var dat map[string]interface{}
if err := json.Unmarshal(b, &dat); err != nil {
return err
}
s.Service = dat["service"].(string)
delete(dat, "service")
s.Source = dat["source"].(string)
delete(dat, "source")
s.Version = dat["version"].(string)
delete(dat, "version")
s.Uptime = dat["uptime"].(float64)
delete(dat, "uptime")
s.Timestamp = dat["timestamp"].(float64)
delete(dat, "timestamp")
s.TotalConnections = dat["total_connections"].(float64)
delete(dat, "total_connections")
s.CurrConnections = dat["curr_connections"].(float64)
delete(dat, "curr_connections")
pools := make(map[string]*pool, len(dat))
d, err := json.Marshal(dat)
if err != nil {
return err
}
err = json.Unmarshal(d, &pools)
if err != nil {
return err
}
s.Pools = pools
return nil
}