| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package stats
-
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "sync"
- "time"
-
- humanize "github.com/dustin/go-humanize"
-
- "github.com/9seconds/mtg/config"
- )
-
- type uptime time.Time
-
- func (u uptime) MarshalJSON() ([]byte, error) {
- duration := time.Since(time.Time(u))
- value := map[string]string{
- "seconds": strconv.Itoa(int(duration.Seconds())),
- "human": humanize.Time(time.Time(u)),
- }
-
- return json.Marshal(value)
- }
-
- type trafficValue uint64
-
- func (t trafficValue) MarshalJSON() ([]byte, error) {
- tv := uint64(t)
- value := map[string]interface{}{
- "bytes": tv,
- "human": humanize.Bytes(tv),
- }
-
- return json.Marshal(value)
- }
-
- type trafficSpeedValue uint64
-
- func (t trafficSpeedValue) MarshalJSON() ([]byte, error) {
- speed := uint64(t)
- value := map[string]interface{}{
- "bytes/s": speed,
- "human": fmt.Sprintf("%s/S", humanize.Bytes(speed)),
- }
-
- return json.Marshal(value)
- }
-
- type connections struct {
- All connectionType `json:"all"`
- Abridged connectionType `json:"abridged"`
- Intermediate connectionType `json:"intermediate"`
- }
-
- func (c connections) MarshalJSON() ([]byte, error) {
- c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4
- c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6
-
- value := struct {
- All connectionType `json:"all"`
- Abridged connectionType `json:"abridged"`
- Intermediate connectionType `json:"intermediate"`
- }{
- All: c.All,
- Abridged: c.Abridged,
- Intermediate: c.Intermediate,
- }
-
- return json.Marshal(value)
- }
-
- type connectionType struct {
- IPv6 uint32 `json:"ipv6"`
- IPv4 uint32 `json:"ipv4"`
- }
-
- type traffic struct {
- Ingress trafficValue `json:"ingress"`
- Egress trafficValue `json:"egress"`
- }
-
- type speed struct {
- Ingress trafficSpeedValue `json:"ingress"`
- Egress trafficSpeedValue `json:"egress"`
- }
-
- type stats struct {
- URLs config.IPURLs `json:"urls"`
- ActiveConnections connections `json:"active_connections"`
- AllConnections connections `json:"all_connections"`
- Traffic traffic `json:"traffic"`
- Speed speed `json:"speed"`
- Uptime uptime `json:"uptime"`
- Crashes uint32 `json:"crashes"`
-
- speedCurrent speed
- mutex *sync.RWMutex
- }
|