yanic/data/statistics_airtime.go

63 lines
1.8 KiB
Go
Raw Permalink Normal View History

2016-07-13 23:19:03 +00:00
package data
2017-04-08 11:57:33 +00:00
import "math"
// Wireless struct
2016-07-13 23:19:03 +00:00
type Wireless struct {
TxPower24 uint32 `json:"txpower24,omitempty"`
Channel24 uint32 `json:"channel24,omitempty"`
TxPower5 uint32 `json:"txpower5,omitempty"`
Channel5 uint32 `json:"channel5,omitempty"`
}
// WirelessStatistics struct
2016-12-22 02:14:51 +00:00
type WirelessStatistics []*WirelessAirtime
2016-07-13 23:19:03 +00:00
// WirelessAirtime struct
2016-07-13 23:19:03 +00:00
type WirelessAirtime struct {
ChanUtil float32 // Channel utilization
RxUtil float32 // Receive utilization
TxUtil float32 // Transmit utilization
ActiveTime uint64 `json:"active"`
BusyTime uint64 `json:"busy"`
RxTime uint64 `json:"rx"`
TxTime uint64 `json:"tx"`
Noise int32 `json:"noise"`
Frequency uint32 `json:"frequency"`
2016-07-13 23:19:03 +00:00
}
2017-04-08 11:57:33 +00:00
// FrequencyName returns 11g or 11a
2016-12-22 02:14:51 +00:00
func (airtime WirelessAirtime) FrequencyName() string {
if airtime.Frequency < 5000 {
return "11g"
2016-07-14 10:38:32 +00:00
}
return "11a"
2016-12-22 02:14:51 +00:00
}
2017-04-08 11:57:33 +00:00
// SetUtilization calculates the utilization values in regard to the previous values
2016-12-22 02:14:51 +00:00
func (current WirelessStatistics) SetUtilization(previous WirelessStatistics) {
for _, c := range current {
for _, p := range previous {
2017-04-08 11:57:33 +00:00
// Same frequency and time passed?
if c.Frequency == p.Frequency && p.ActiveTime < c.ActiveTime {
c.setUtilization(p)
2016-12-22 02:14:51 +00:00
}
}
2016-07-14 10:38:32 +00:00
}
2016-07-13 23:19:03 +00:00
}
2017-04-08 11:57:33 +00:00
// setUtilization updates the utilization values in regard to the previous values
func (airtime *WirelessAirtime) setUtilization(prev *WirelessAirtime) {
// Calculate deltas
active := float64(airtime.ActiveTime) - float64(prev.ActiveTime)
busy := float64(airtime.BusyTime) - float64(prev.BusyTime)
2017-04-08 11:57:33 +00:00
rx := float64(airtime.RxTime) - float64(prev.RxTime)
tx := float64(airtime.TxTime) - float64(prev.TxTime)
2016-07-13 23:19:03 +00:00
2017-04-08 11:57:33 +00:00
// Update utilizations
airtime.ChanUtil = float32(math.Min(100, 100*busy/active))
airtime.RxUtil = float32(math.Min(100, 100*rx/active))
airtime.TxUtil = float32(math.Min(100, 100*tx/active))
2016-07-13 23:19:03 +00:00
}