show pre-formatted values
This commit is contained in:
parent
04d0c01536
commit
64de05523e
@ -14,10 +14,10 @@ func CalculateClientTraffic(site *omadamodel.Site, client *omadamodel.Client, du
|
|||||||
ClientMac: client.MacAddress,
|
ClientMac: client.MacAddress,
|
||||||
}
|
}
|
||||||
|
|
||||||
trafficTotal := TrafficRate{
|
trafficTotal := MakeTrafficRate(
|
||||||
Received: uint64(client.TrafficDownBytes),
|
uint64(client.TrafficDownBytes),
|
||||||
Transferred: uint64(client.TrafficUpBytes),
|
uint64(client.TrafficUpBytes),
|
||||||
}
|
)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
TrafficByClient[uniqueID] = trafficTotal
|
TrafficByClient[uniqueID] = trafficTotal
|
||||||
@ -26,14 +26,14 @@ func CalculateClientTraffic(site *omadamodel.Site, client *omadamodel.Client, du
|
|||||||
lastTotal, ok := TrafficByClient[uniqueID]
|
lastTotal, ok := TrafficByClient[uniqueID]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return TrafficRate{}
|
return MakeTrafficRate(0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
trafficInDur := trafficTotal.Sub(lastTotal)
|
trafficInDur := trafficTotal.Sub(lastTotal)
|
||||||
seconds := duration.Seconds()
|
seconds := duration.Seconds()
|
||||||
|
|
||||||
return TrafficRate{
|
return MakeTrafficRate(
|
||||||
Received: uint64(float64(trafficInDur.Received) / seconds),
|
uint64(float64(trafficInDur.Received)/seconds),
|
||||||
Transferred: uint64(float64(trafficInDur.Transferred) / seconds),
|
uint64(float64(trafficInDur.Transferred)/seconds),
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
6
main.go
6
main.go
@ -9,7 +9,7 @@ import (
|
|||||||
"git.tordarus.net/tordarus/channel"
|
"git.tordarus.net/tordarus/channel"
|
||||||
"git.tordarus.net/tordarus/envvars"
|
"git.tordarus.net/tordarus/envvars"
|
||||||
"git.tordarus.net/tordarus/gmath"
|
"git.tordarus.net/tordarus/gmath"
|
||||||
omadaapi "git.tordarus.net/tordarus/omada-api"
|
omada "git.tordarus.net/tordarus/omada-api"
|
||||||
omadamodel "git.tordarus.net/tordarus/omada-api/model"
|
omadamodel "git.tordarus.net/tordarus/omada-api/model"
|
||||||
"git.tordarus.net/tordarus/slices"
|
"git.tordarus.net/tordarus/slices"
|
||||||
)
|
)
|
||||||
@ -36,7 +36,7 @@ var CurrentTraffic = &TrafficStats{}
|
|||||||
func main() {
|
func main() {
|
||||||
FlagRefreshInterval = gmath.Max(FlagRefreshInterval, MinRefreshInterval)
|
FlagRefreshInterval = gmath.Max(FlagRefreshInterval, MinRefreshInterval)
|
||||||
|
|
||||||
api, err := omadaapi.NewApi(omadaapi.ApiConfig{
|
api, err := omada.NewApi(omada.ApiConfig{
|
||||||
BasePath: FlagOmadaURL,
|
BasePath: FlagOmadaURL,
|
||||||
OmadaID: FlagOmadaID,
|
OmadaID: FlagOmadaID,
|
||||||
ClientID: FlagOmadaClientID,
|
ClientID: FlagOmadaClientID,
|
||||||
@ -72,7 +72,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateSiteTraffic(api *omadaapi.Api, sites []*omadamodel.Site, duration time.Duration) *TrafficStats {
|
func CalculateSiteTraffic(api *omada.Api, sites []*omadamodel.Site, duration time.Duration) *TrafficStats {
|
||||||
siteTraffics := map[string]*SiteTraffic{}
|
siteTraffics := map[string]*SiteTraffic{}
|
||||||
|
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
|
48
types.go
48
types.go
@ -16,20 +16,32 @@ type ClientUniqueID struct {
|
|||||||
type TrafficRate struct {
|
type TrafficRate struct {
|
||||||
Received uint64 `json:"received"` // bytes per second
|
Received uint64 `json:"received"` // bytes per second
|
||||||
Transferred uint64 `json:"transferred"` // bytes per second
|
Transferred uint64 `json:"transferred"` // bytes per second
|
||||||
|
|
||||||
|
ReceivedFormatted string `json:"received_fmt"`
|
||||||
|
TransferredFormatted string `json:"transferred_fmt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeTrafficRate(received, transferred uint64) TrafficRate {
|
||||||
|
return TrafficRate{
|
||||||
|
Received: received,
|
||||||
|
Transferred: transferred,
|
||||||
|
ReceivedFormatted: FormatBytesPerSecond(received),
|
||||||
|
TransferredFormatted: FormatBytesPerSecond(transferred),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr TrafficRate) Add(o TrafficRate) TrafficRate {
|
func (tr TrafficRate) Add(o TrafficRate) TrafficRate {
|
||||||
return TrafficRate{
|
return MakeTrafficRate(
|
||||||
Received: tr.Received + o.Received,
|
tr.Received+o.Received,
|
||||||
Transferred: tr.Transferred + o.Transferred,
|
tr.Transferred+o.Transferred,
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr TrafficRate) Sub(o TrafficRate) TrafficRate {
|
func (tr TrafficRate) Sub(o TrafficRate) TrafficRate {
|
||||||
return TrafficRate{
|
return MakeTrafficRate(
|
||||||
Received: tr.Received - o.Received,
|
tr.Received-o.Received,
|
||||||
Transferred: tr.Transferred - o.Transferred,
|
tr.Transferred-o.Transferred,
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SiteTraffic struct {
|
type SiteTraffic struct {
|
||||||
@ -38,7 +50,7 @@ type SiteTraffic struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewSiteTraffic(clients map[string]TrafficRate) *SiteTraffic {
|
func NewSiteTraffic(clients map[string]TrafficRate) *SiteTraffic {
|
||||||
total := TrafficRate{}
|
total := MakeTrafficRate(0, 0)
|
||||||
for _, rate := range clients {
|
for _, rate := range clients {
|
||||||
total = total.Add(rate)
|
total = total.Add(rate)
|
||||||
}
|
}
|
||||||
@ -55,7 +67,7 @@ type TrafficStats struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewTrafficStats(sites map[string]*SiteTraffic) *TrafficStats {
|
func NewTrafficStats(sites map[string]*SiteTraffic) *TrafficStats {
|
||||||
total := TrafficRate{}
|
total := MakeTrafficRate(0, 0)
|
||||||
for _, siteTraffic := range sites {
|
for _, siteTraffic := range sites {
|
||||||
total = total.Add(siteTraffic.Total)
|
total = total.Add(siteTraffic.Total)
|
||||||
}
|
}
|
||||||
@ -67,7 +79,7 @@ func NewTrafficStats(sites map[string]*SiteTraffic) *TrafficStats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tr TrafficRate) String() string {
|
func (tr TrafficRate) String() string {
|
||||||
return fmt.Sprintf("Rx: %s | Tx: %s", FormatBytes(tr.Received), FormatBytes(tr.Transferred))
|
return fmt.Sprintf("Rx: %s | Tx: %s", FormatBytesPerSecond(tr.Received), FormatBytesPerSecond(tr.Transferred))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stats TrafficStats) String() string {
|
func (stats TrafficStats) String() string {
|
||||||
@ -75,18 +87,16 @@ func (stats TrafficStats) String() string {
|
|||||||
return string(data)
|
return string(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatBytes[T gmath.Integer](bytes T) string {
|
func FormatBytesPerSecond[T gmath.Integer](bytes T) string {
|
||||||
value := float64(bytes)
|
value := float64(bytes)
|
||||||
|
|
||||||
if value >= 1000000000000 {
|
if value >= 1000000000 {
|
||||||
return fmt.Sprintf("%.02fT", value/1000000000000)
|
return fmt.Sprintf("%.1f GB/s", value/1000000000)
|
||||||
} else if value >= 1000000000 {
|
|
||||||
return fmt.Sprintf("%.02fG", value/1000000000)
|
|
||||||
} else if value >= 1000000 {
|
} else if value >= 1000000 {
|
||||||
return fmt.Sprintf("%.02fM", value/1000000)
|
return fmt.Sprintf("%.1f MB/s", value/1000000)
|
||||||
} else if value >= 1000 {
|
} else if value >= 1000 {
|
||||||
return fmt.Sprintf("%.02fK", value/1000)
|
return fmt.Sprintf("%.0f kB/s", value/1000)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Sprintf("%.02fB", value)
|
return fmt.Sprintf("%.0f B/s", value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user