Skip to content

Commit

Permalink
Merge pull request #6 from akramer/master
Browse files Browse the repository at this point in the history
Fixes for device uplink counters
  • Loading branch information
mdlayher committed Aug 5, 2017
2 parents dbb64f4 + e91fa6d commit 3b086ee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
33 changes: 25 additions & 8 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type DeviceStats struct {
Uplink *WiredStats
}

func (s *DeviceStats) String() string {
return fmt.Sprintf("%v", *s)
}

// WirelessStats contains wireless device network activity statistics.
type WirelessStats struct {
ReceiveBytes float64
Expand All @@ -88,6 +92,10 @@ type WirelessStats struct {
TransmitPackets float64
}

func (s *WirelessStats) String() string {
return fmt.Sprintf("%v", *s)
}

// WiredStats contains wired device network activity statistics.
type WiredStats struct {
ReceiveBytes float64
Expand All @@ -96,6 +104,10 @@ type WiredStats struct {
TransmitPackets float64
}

func (s *WiredStats) String() string {
return fmt.Sprintf("%v", *s)
}

const (
radioNA = "na"
radioNG = "ng"
Expand Down Expand Up @@ -197,10 +209,10 @@ func (d *Device) UnmarshalJSON(b []byte) error {
TransmitPackets: dev.Stat.UserTxPackets,
},
Uplink: &WiredStats{
ReceiveBytes: dev.Stat.UplinkRxBytes,
ReceivePackets: dev.Stat.UplinkRxPackets,
TransmitBytes: dev.Stat.UplinkTxBytes,
TransmitPackets: dev.Stat.UplinkTxPackets,
ReceiveBytes: dev.Uplink.RxBytes,
ReceivePackets: dev.Uplink.RxPackets,
TransmitBytes: dev.Uplink.TxBytes,
TransmitPackets: dev.Uplink.TxPackets,
},
},
}
Expand Down Expand Up @@ -281,10 +293,6 @@ type device struct {
TxBytes float64 `json:"tx_bytes"`
TxDropped float64 `json:"tx_dropped"`
TxPackets float64 `json:"tx_packets"`
UplinkRxBytes float64 `json:"uplink-rx_bytes"`
UplinkRxPackets float64 `json:"uplink-rx_packets"`
UplinkTxBytes float64 `json:"uplink-tx_bytes"`
UplinkTxPackets float64 `json:"uplink-tx_packets"`
UserNgRxBytes float64 `json:"user-ng-rx_bytes"`
UserNgRxPackets float64 `json:"user-ng-rx_packets"`
UserNgTxBytes float64 `json:"user-ng-tx_bytes"`
Expand All @@ -296,6 +304,15 @@ type device struct {
UserTxDropped float64 `json:"user-tx_dropped"`
UserTxPackets float64 `json:"user-tx_packets"`
} `json:"stat"`
Uplink struct {
RxBytes float64 `json:"rx_bytes"`
RxPackets float64 `json:"rx_packets"`
RxErrors float64 `json:"rx_errors"`
TxBytes float64 `json:"tx_bytes"`
TxPackets float64 `json:"tx_packets"`
TxErrors float64 `json:"tx_errors"`
Type string `json:"type"`
} `json:"uplink"`
State int `json:"state"`
TxBytes float64 `json:"tx_bytes"`
Type string `json:"type"`
Expand Down
42 changes: 31 additions & 11 deletions devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,30 @@ func TestDeviceUnmarshalJSON(t *testing.T) {
"user-rx_packets": 4,
"user-tx_bytes": 20,
"user-tx_dropped": 1,
"user-tx_packets": 1,
"uplink-rx_bytes": 80,
"uplink-rx_packets": 4,
"uplink-tx_bytes": 20,
"uplink-tx_packets": 1
"user-tx_packets": 1
},
"uptime": "61",
"uplink": {
"full_duplex": true,
"ip": "0.0.0.0",
"mac": "de:ad:be:ef:00:00",
"max_speed": 1000,
"name": "eth0",
"netmask": "0.0.0.0",
"num_port": 2,
"rx_bytes": 81,
"rx_dropped": 11023,
"rx_errors": 0,
"rx_multicast": 0,
"rx_packets": 5,
"speed": 1000,
"tx_bytes": 21,
"tx_dropped": 0,
"tx_errors": 0,
"tx_packets": 2,
"type": "wire",
"up": true
},
"uptime": 61,
"version": "1.0.0"
}
`)),
Expand Down Expand Up @@ -233,10 +250,10 @@ func TestDeviceUnmarshalJSON(t *testing.T) {
TransmitPackets: 1,
},
Uplink: &WiredStats{
ReceiveBytes: 80,
ReceivePackets: 4,
TransmitBytes: 20,
TransmitPackets: 1,
ReceiveBytes: 81,
ReceivePackets: 5,
TransmitBytes: 21,
TransmitPackets: 2,
},
},
Uptime: 61 * time.Second,
Expand All @@ -253,9 +270,12 @@ func TestDeviceUnmarshalJSON(t *testing.T) {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v",
want, got)
}
if err != nil {
if tt.err != nil {
return
}
if err != nil {
t.Fatalf("Error parsing json: %v", err)
}

if want, got := tt.d, d; !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected Device:\n- want: %+v\n- got: %+v",
Expand Down

0 comments on commit 3b086ee

Please sign in to comment.