mirror of
https://github.com/fosrl/gerbil.git
synced 2026-03-10 04:36:41 +00:00
Sending incremental updates working
This commit is contained in:
58
main.go
58
main.go
@@ -497,8 +497,6 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
|
|
||||||
for _, peer := range device.Peers {
|
for _, peer := range device.Peers {
|
||||||
publicKey := peer.PublicKey.String()
|
publicKey := peer.PublicKey.String()
|
||||||
|
|
||||||
lastReading, exists := lastReadings[publicKey]
|
|
||||||
currentReading := PeerReading{
|
currentReading := PeerReading{
|
||||||
BytesReceived: peer.ReceiveBytes,
|
BytesReceived: peer.ReceiveBytes,
|
||||||
BytesTransmitted: peer.TransmitBytes,
|
BytesTransmitted: peer.TransmitBytes,
|
||||||
@@ -506,27 +504,49 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var bytesInDiff, bytesOutDiff float64
|
var bytesInDiff, bytesOutDiff float64
|
||||||
|
lastReading, exists := lastReadings[publicKey]
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
// Calculate total bytes transferred since last reading
|
timeDiff := currentReading.LastChecked.Sub(lastReading.LastChecked).Seconds()
|
||||||
bytesInDiff = float64(currentReading.BytesReceived - lastReading.BytesReceived)
|
if timeDiff > 0 {
|
||||||
bytesOutDiff = float64(currentReading.BytesTransmitted - lastReading.BytesTransmitted)
|
// Calculate bytes transferred since last reading
|
||||||
|
bytesInDiff = float64(currentReading.BytesReceived - lastReading.BytesReceived)
|
||||||
|
bytesOutDiff = float64(currentReading.BytesTransmitted - lastReading.BytesTransmitted)
|
||||||
|
|
||||||
|
// Handle counter wraparound (if the counter resets or overflows)
|
||||||
|
if bytesInDiff < 0 {
|
||||||
|
bytesInDiff = float64(currentReading.BytesReceived)
|
||||||
|
}
|
||||||
|
if bytesOutDiff < 0 {
|
||||||
|
bytesOutDiff = float64(currentReading.BytesTransmitted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to MB
|
||||||
|
bytesInMB := bytesInDiff / (1024 * 1024)
|
||||||
|
bytesOutMB := bytesOutDiff / (1024 * 1024)
|
||||||
|
|
||||||
|
peerBandwidths = append(peerBandwidths, PeerBandwidth{
|
||||||
|
PublicKey: publicKey,
|
||||||
|
BytesIn: bytesInMB,
|
||||||
|
BytesOut: bytesOutMB,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// If readings are too close together or time hasn't passed, report 0
|
||||||
|
peerBandwidths = append(peerBandwidths, PeerBandwidth{
|
||||||
|
PublicKey: publicKey,
|
||||||
|
BytesIn: 0,
|
||||||
|
BytesOut: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// For first reading, use total bytes as the increment
|
// For first reading of a peer, report 0 to establish baseline
|
||||||
bytesInDiff = float64(currentReading.BytesReceived)
|
peerBandwidths = append(peerBandwidths, PeerBandwidth{
|
||||||
bytesOutDiff = float64(currentReading.BytesTransmitted)
|
PublicKey: publicKey,
|
||||||
|
BytesIn: 0,
|
||||||
|
BytesOut: 0,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to MB
|
|
||||||
bytesInMB := bytesInDiff / (1024 * 1024)
|
|
||||||
bytesOutMB := bytesOutDiff / (1024 * 1024)
|
|
||||||
|
|
||||||
peerBandwidths = append(peerBandwidths, PeerBandwidth{
|
|
||||||
PublicKey: publicKey,
|
|
||||||
BytesIn: bytesInMB,
|
|
||||||
BytesOut: bytesOutMB,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Update the last reading
|
// Update the last reading
|
||||||
lastReadings[publicKey] = currentReading
|
lastReadings[publicKey] = currentReading
|
||||||
}
|
}
|
||||||
@@ -559,6 +579,8 @@ func reportPeerBandwidth(apiURL string) error {
|
|||||||
return fmt.Errorf("failed to marshal bandwidth data: %v", err)
|
return fmt.Errorf("failed to marshal bandwidth data: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info("Reporting bandwidth data: %s", string(jsonData))
|
||||||
|
|
||||||
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(jsonData))
|
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to send bandwidth data: %v", err)
|
return fmt.Errorf("failed to send bandwidth data: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user