| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | package metricsimport (	"sync"	"time"	"github.com/gravitl/netmaker/models")// lock for metrics mapvar metricsMapLock = &sync.RWMutex{}// metrics data mapvar metricsPeerMap = make(map[string]map[string]*models.ProxyMetric)// GetMetricByServer - get metric data of peers by serverfunc GetMetricByServer(server string) map[string]*models.ProxyMetric {	metricsMapLock.RLock()	defer metricsMapLock.RUnlock()	if _, ok := metricsPeerMap[server]; !ok {		return nil	}	return metricsPeerMap[server]}// GetMetric - fetches the metric data for the peerfunc GetMetric(server, peerKey string) models.ProxyMetric {	metric := models.ProxyMetric{}	peerMetricMap := GetMetricByServer(server)	metricsMapLock.RLock()	defer metricsMapLock.RUnlock()	if peerMetricMap == nil {		return metric	}	if m, ok := peerMetricMap[peerKey]; ok && m != nil {		metric = *m	}	return metric}// UpdateMetric - updates metric data for the peerfunc UpdateMetric(server, peerKey string, metric *models.ProxyMetric) {	metricsMapLock.Lock()	defer metricsMapLock.Unlock()	if metricsPeerMap[server] == nil {		metricsPeerMap[server] = make(map[string]*models.ProxyMetric)	}	metricsPeerMap[server][peerKey] = metric}// UpdateMetricByPeer - updates metrics data by peer public keyfunc UpdateMetricByPeer(peerKey string, metric *models.ProxyMetric, onlyTraffic bool) {	metricsMapLock.Lock()	defer metricsMapLock.Unlock()	for server, peerKeyMap := range metricsPeerMap {		if peerMetric, ok := peerKeyMap[peerKey]; ok {			peerMetric.TrafficRecieved += metric.TrafficRecieved			peerMetric.TrafficSent += metric.TrafficSent			if !onlyTraffic {				peerMetric.LastRecordedLatency = metric.LastRecordedLatency			}			metricsPeerMap[server][peerKey] = peerMetric		}	}}// ResetMetricsForPeer - reset metrics for peerfunc ResetMetricsForPeer(server, peerKey string) {	metricsMapLock.Lock()	defer metricsMapLock.Unlock()	delete(metricsPeerMap[server], peerKey)}// ResetMetricForNode - resets node level metricsfunc ResetMetricForNode(server, peerKey, peerID string) {	metric := GetMetric(server, peerKey)	delete(metric.NodeConnectionStatus, peerID)	UpdateMetric(server, peerKey, &metric)}// MetricCollectionInterval - collection interval for metricsconst MetricCollectionInterval = time.Second * 25
 |