mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
ef99e67507
Expose sampled CPU topology, absolute RAM, TCP/UDP counts, load averages and network throughput through the existing heartbeat and descendant contracts, preserving measured zero versus unavailable values. Add an explicit nullable telemetry migration and freshness/availability checks.
Provide read-only All nodes and Master rows with deduplicated host/client totals, grouped SQL aggregation and reporting coverage. Preserve known descendants during parent outages and prefer the freshest valid observation of shared GUIDs. Keep the ordinary heartbeat broadcast free of fleet-wide totals.
Implement the approved compact Penpot-inspired Nodes layout, responsive stacked details, status diagnostics and existing remote actions. Preserve cached rows, search and controls on overview refresh failures. By maintainer decision, hide both system rows when no remote nodes are registered.
Final review of 44ed457985: no outstanding HIGH or MEDIUM findings. CI and Docs CI passed (13 jobs; 2342 frontend tests in 269 files), including Go/race and mixed-version node E2E. Feature-specific PostgreSQL regression execution was not independently verified in the selected CI logs. See https://github.com/MHSanaei/3x-ui/pull/7342#issuecomment-7363599190.
Closes #7335
Closes #7336
Closes #7337
124 lines
4.3 KiB
Go
124 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
const nodeTelemetryFreshness = 30 * time.Second
|
|
|
|
func validNodeLoad(load float64) bool {
|
|
return load >= 0 && !math.IsInf(load, 0)
|
|
}
|
|
|
|
type nodeStatusMetrics struct {
|
|
Cpu json.RawMessage `json:"cpu"`
|
|
CpuCores json.RawMessage `json:"cpuCores"`
|
|
LogicalPro json.RawMessage `json:"logicalPro"`
|
|
Mem json.RawMessage `json:"mem"`
|
|
TcpCount json.RawMessage `json:"tcpCount"`
|
|
UdpCount json.RawMessage `json:"udpCount"`
|
|
Loads json.RawMessage `json:"loads"`
|
|
Uptime json.RawMessage `json:"uptime"`
|
|
NetIO json.RawMessage `json:"netIO"`
|
|
ResourceScope string `json:"resourceScope"`
|
|
SampleAgeMillis *int64 `json:"sampleAgeMillis"`
|
|
UnavailableMetrics []string `json:"unavailableMetrics"`
|
|
}
|
|
|
|
type nodeMetricNumber interface {
|
|
int | uint64 | float64
|
|
}
|
|
|
|
func readNodeMetric[T nodeMetricNumber](raw json.RawMessage, unavailable []string, key string) *T {
|
|
var v *T
|
|
if slices.Contains(unavailable, key) || json.Unmarshal(raw, &v) != nil || v == nil || *v < 0 {
|
|
return nil
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (m nodeStatusMetrics) telemetry(received time.Time) *model.NodeTelemetry {
|
|
unavailable := m.UnavailableMetrics
|
|
var memory struct {
|
|
Current json.RawMessage `json:"current"`
|
|
Total json.RawMessage `json:"total"`
|
|
Available json.RawMessage `json:"available"`
|
|
}
|
|
var network struct {
|
|
Up json.RawMessage `json:"up"`
|
|
Down json.RawMessage `json:"down"`
|
|
}
|
|
var loads [3]json.RawMessage
|
|
_ = json.Unmarshal(m.Mem, &memory)
|
|
_ = json.Unmarshal(m.NetIO, &network)
|
|
_ = json.Unmarshal(m.Loads, &loads)
|
|
|
|
age := int64(0)
|
|
if m.SampleAgeMillis != nil {
|
|
age = min(max(0, *m.SampleAgeMillis), nodeTelemetryFreshness.Milliseconds())
|
|
}
|
|
sample := &model.NodeTelemetry{
|
|
SampledAt: received.UnixMilli() - age,
|
|
ResourceScope: m.ResourceScope,
|
|
CpuPct: readNodeMetric[float64](m.Cpu, unavailable, "cpu"),
|
|
PhysicalCores: readNodeMetric[int](m.CpuCores, unavailable, "cpuCores"),
|
|
LogicalCores: readNodeMetric[int](m.LogicalPro, unavailable, "logicalPro"),
|
|
MemUsedBytes: readNodeMetric[uint64](memory.Current, unavailable, "mem"),
|
|
MemTotalBytes: readNodeMetric[uint64](memory.Total, unavailable, "mem"),
|
|
MemAvailableBytes: readNodeMetric[uint64](memory.Available, unavailable, "mem"),
|
|
TCPCount: readNodeMetric[int](m.TcpCount, unavailable, "tcpCount"),
|
|
UDPCount: readNodeMetric[int](m.UdpCount, unavailable, "udpCount"),
|
|
Load1: readNodeMetric[float64](loads[0], unavailable, "loads"),
|
|
Load5: readNodeMetric[float64](loads[1], unavailable, "loads"),
|
|
Load15: readNodeMetric[float64](loads[2], unavailable, "loads"),
|
|
UptimeSecs: readNodeMetric[uint64](m.Uptime, unavailable, "uptime"),
|
|
NetUp: readNodeMetric[uint64](network.Up, unavailable, "netUp"),
|
|
NetDown: readNodeMetric[uint64](network.Down, unavailable, "netDown"),
|
|
}
|
|
if sample.CpuPct != nil && *sample.CpuPct > 100 {
|
|
sample.CpuPct = nil
|
|
}
|
|
if sample.ResourceScope != "" || (sample.PhysicalCores != nil && *sample.PhysicalCores == 0) {
|
|
sample.PhysicalCores = nil
|
|
}
|
|
if sample.LogicalCores != nil && *sample.LogicalCores == 0 {
|
|
sample.LogicalCores = nil
|
|
}
|
|
if sample.MemTotalBytes == nil || *sample.MemTotalBytes == 0 ||
|
|
sample.MemUsedBytes == nil || *sample.MemUsedBytes > *sample.MemTotalBytes {
|
|
sample.MemUsedBytes, sample.MemTotalBytes, sample.MemAvailableBytes = nil, nil, nil
|
|
} else if sample.MemAvailableBytes != nil && *sample.MemAvailableBytes > *sample.MemTotalBytes {
|
|
sample.MemAvailableBytes = nil
|
|
}
|
|
return sample
|
|
}
|
|
|
|
func liveNodeTelemetry(node *model.Node, now time.Time) *model.NodeTelemetry {
|
|
sample := node.Telemetry
|
|
if sample == nil || !node.Enable || node.Status != "online" {
|
|
return nil
|
|
}
|
|
age := now.UnixMilli() - sample.SampledAt
|
|
if age < 0 || age >= nodeTelemetryFreshness.Milliseconds() {
|
|
return nil
|
|
}
|
|
view := *sample
|
|
view.SampleAgeMillis = &age
|
|
return &view
|
|
}
|
|
|
|
// StatusWithSampleAge publishes age relative to the sampler's clock.
|
|
func StatusWithSampleAge(status *Status) *Status {
|
|
if status == nil {
|
|
return nil
|
|
}
|
|
result := *status
|
|
result.SampleAgeMillis = new(max(0, time.Since(status.T).Milliseconds()))
|
|
return &result
|
|
}
|