add new field to proxy metrics

This commit is contained in:
Abhishek Kondur 2023-03-14 06:55:25 +04:00
parent 6712824763
commit 959ca622e3
2 changed files with 18 additions and 9 deletions

View file

@ -15,15 +15,16 @@ type Metrics struct {
// Metric - holds a metric for data between nodes
type Metric struct {
NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"`
Uptime int64 `json:"uptime" bson:"uptime" yaml:"uptime"`
TotalTime int64 `json:"totaltime" bson:"totaltime" yaml:"totaltime"`
Latency int64 `json:"latency" bson:"latency" yaml:"latency"`
TotalReceived int64 `json:"totalreceived" bson:"totalreceived" yaml:"totalreceived"`
TotalSent int64 `json:"totalsent" bson:"totalsent" yaml:"totalsent"`
ActualUptime time.Duration `json:"actualuptime" bson:"actualuptime" yaml:"actualuptime"`
PercentUp float64 `json:"percentup" bson:"percentup" yaml:"percentup"`
Connected bool `json:"connected" bson:"connected" yaml:"connected"`
NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"`
Uptime int64 `json:"uptime" bson:"uptime" yaml:"uptime"`
TotalTime int64 `json:"totaltime" bson:"totaltime" yaml:"totaltime"`
Latency int64 `json:"latency" bson:"latency" yaml:"latency"`
TotalReceived int64 `json:"totalreceived" bson:"totalreceived" yaml:"totalreceived"`
TotalSent int64 `json:"totalsent" bson:"totalsent" yaml:"totalsent"`
ActualUptime time.Duration `json:"actualuptime" bson:"actualuptime" yaml:"actualuptime"`
PercentUp float64 `json:"percentup" bson:"percentup" yaml:"percentup"`
Connected bool `json:"connected" bson:"connected" yaml:"connected"`
CollectedByProxy bool `json:"collected_by_proxy" bson:"collected_by_proxy" yaml:"collected_by_proxy"`
}
// IDandAddr - struct to hold ID and primary Address

View file

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
@ -362,6 +363,13 @@ func updateNodeMetrics(currentNode *models.Node, newMetrics *models.Metrics) boo
oldMetric := oldMetrics.Connectivity[k]
currMetric.TotalTime += oldMetric.TotalTime
currMetric.Uptime += oldMetric.Uptime // get the total uptime for this connection
if currMetric.CollectedByProxy {
currMetric.TotalReceived += oldMetric.TotalReceived
currMetric.TotalSent += oldMetric.TotalSent
} else {
currMetric.TotalReceived += int64(math.Abs(float64(currMetric.TotalReceived) - float64(oldMetric.TotalReceived)))
currMetric.TotalSent += int64(math.Abs(float64(currMetric.TotalSent) - float64(oldMetric.TotalSent)))
}
if currMetric.Uptime == 0 || currMetric.TotalTime == 0 {
currMetric.PercentUp = 0
} else {