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,
|
||||
}
|
||||
|
||||
trafficTotal := TrafficRate{
|
||||
Received: uint64(client.TrafficDownBytes),
|
||||
Transferred: uint64(client.TrafficUpBytes),
|
||||
}
|
||||
trafficTotal := MakeTrafficRate(
|
||||
uint64(client.TrafficDownBytes),
|
||||
uint64(client.TrafficUpBytes),
|
||||
)
|
||||
|
||||
defer func() {
|
||||
TrafficByClient[uniqueID] = trafficTotal
|
||||
@ -26,14 +26,14 @@ func CalculateClientTraffic(site *omadamodel.Site, client *omadamodel.Client, du
|
||||
lastTotal, ok := TrafficByClient[uniqueID]
|
||||
|
||||
if !ok {
|
||||
return TrafficRate{}
|
||||
return MakeTrafficRate(0, 0)
|
||||
}
|
||||
|
||||
trafficInDur := trafficTotal.Sub(lastTotal)
|
||||
seconds := duration.Seconds()
|
||||
|
||||
return TrafficRate{
|
||||
Received: uint64(float64(trafficInDur.Received) / seconds),
|
||||
Transferred: uint64(float64(trafficInDur.Transferred) / seconds),
|
||||
}
|
||||
return MakeTrafficRate(
|
||||
uint64(float64(trafficInDur.Received)/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/envvars"
|
||||
"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"
|
||||
"git.tordarus.net/tordarus/slices"
|
||||
)
|
||||
@ -36,7 +36,7 @@ var CurrentTraffic = &TrafficStats{}
|
||||
func main() {
|
||||
FlagRefreshInterval = gmath.Max(FlagRefreshInterval, MinRefreshInterval)
|
||||
|
||||
api, err := omadaapi.NewApi(omadaapi.ApiConfig{
|
||||
api, err := omada.NewApi(omada.ApiConfig{
|
||||
BasePath: FlagOmadaURL,
|
||||
OmadaID: FlagOmadaID,
|
||||
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{}
|
||||
|
||||
for _, site := range sites {
|
||||
|
48
types.go
48
types.go
@ -16,20 +16,32 @@ type ClientUniqueID struct {
|
||||
type TrafficRate struct {
|
||||
Received uint64 `json:"received"` // 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 {
|
||||
return TrafficRate{
|
||||
Received: tr.Received + o.Received,
|
||||
Transferred: tr.Transferred + o.Transferred,
|
||||
}
|
||||
return MakeTrafficRate(
|
||||
tr.Received+o.Received,
|
||||
tr.Transferred+o.Transferred,
|
||||
)
|
||||
}
|
||||
|
||||
func (tr TrafficRate) Sub(o TrafficRate) TrafficRate {
|
||||
return TrafficRate{
|
||||
Received: tr.Received - o.Received,
|
||||
Transferred: tr.Transferred - o.Transferred,
|
||||
}
|
||||
return MakeTrafficRate(
|
||||
tr.Received-o.Received,
|
||||
tr.Transferred-o.Transferred,
|
||||
)
|
||||
}
|
||||
|
||||
type SiteTraffic struct {
|
||||
@ -38,7 +50,7 @@ type SiteTraffic struct {
|
||||
}
|
||||
|
||||
func NewSiteTraffic(clients map[string]TrafficRate) *SiteTraffic {
|
||||
total := TrafficRate{}
|
||||
total := MakeTrafficRate(0, 0)
|
||||
for _, rate := range clients {
|
||||
total = total.Add(rate)
|
||||
}
|
||||
@ -55,7 +67,7 @@ type TrafficStats struct {
|
||||
}
|
||||
|
||||
func NewTrafficStats(sites map[string]*SiteTraffic) *TrafficStats {
|
||||
total := TrafficRate{}
|
||||
total := MakeTrafficRate(0, 0)
|
||||
for _, siteTraffic := range sites {
|
||||
total = total.Add(siteTraffic.Total)
|
||||
}
|
||||
@ -67,7 +79,7 @@ func NewTrafficStats(sites map[string]*SiteTraffic) *TrafficStats {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -75,18 +87,16 @@ func (stats TrafficStats) String() string {
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func FormatBytes[T gmath.Integer](bytes T) string {
|
||||
func FormatBytesPerSecond[T gmath.Integer](bytes T) string {
|
||||
value := float64(bytes)
|
||||
|
||||
if value >= 1000000000000 {
|
||||
return fmt.Sprintf("%.02fT", value/1000000000000)
|
||||
} else if value >= 1000000000 {
|
||||
return fmt.Sprintf("%.02fG", value/1000000000)
|
||||
if value >= 1000000000 {
|
||||
return fmt.Sprintf("%.1f GB/s", value/1000000000)
|
||||
} else if value >= 1000000 {
|
||||
return fmt.Sprintf("%.02fM", value/1000000)
|
||||
return fmt.Sprintf("%.1f MB/s", value/1000000)
|
||||
} else if value >= 1000 {
|
||||
return fmt.Sprintf("%.02fK", value/1000)
|
||||
return fmt.Sprintf("%.0f kB/s", value/1000)
|
||||
} else {
|
||||
return fmt.Sprintf("%.02fB", value)
|
||||
return fmt.Sprintf("%.0f B/s", value)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user