diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-07-07 07:37:57 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-07-07 07:44:48 +0530 |
commit | a1692f732078adad272256639f7a3ff14c9e643a (patch) | |
tree | 8e3507ebd905e6fec65ac63bb6260b21489c2278 /api/bandwidth.go | |
parent | 22ae2f9b78dfdd2f59340d272ef598deb56cc245 (diff) |
api/bandwidth: init
Diffstat (limited to 'api/bandwidth.go')
-rw-r--r-- | api/bandwidth.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/api/bandwidth.go b/api/bandwidth.go new file mode 100644 index 0000000..5f23221 --- /dev/null +++ b/api/bandwidth.go @@ -0,0 +1,41 @@ +package api + +import ( + "encoding/json" + "fmt" + "log" + "net" + + "github.com/cilium/cilium/pkg/mac" + "github.com/dustin/go-humanize" + "sinanmohd.com/redq/usage" +) + +type BandwidthStat struct { + Ingress string `json:"ingress"` + Egress string `json:"egress"` +} + +type BandwidthResp map[string]BandwidthStat + +func handleBandwidth(conn net.Conn, u *usage.Usage) { + resp := make(BandwidthResp) + + u.Mutex.RLock() + for key, value := range u.Data { + m := mac.Uint64MAC(key) + resp[m.String()] = BandwidthStat{ + Ingress: fmt.Sprintf("%s/s", humanize.Bytes(value.BandwidthIngress)), + Egress: fmt.Sprintf("%s/s", humanize.Bytes(value.BandwidthEgress)), + } + } + u.Mutex.RUnlock() + + buf, err := json.Marshal(resp) + if err != nil { + log.Printf("marshaling json: %s", err) + return + } + + conn.Write(buf) +} |