Files
mirror-3x-ui/internal/web/service/server_scope.go
T
Hamed e0c01942ec feat(monitoring): allow selecting the network interface for traffic statistics (#6972)
The panel's network totals and speed add up every interface that is not on a fixed list of virtual names. On hosts where a second interface carries the same traffic, such as an SR-IOV function beside eth0, the dashboard and the bot reports counted it twice (#6906).

Add a traffic mode to General settings: legacy keeps today's sum, auto follows the default-route interface, manual names one. An interface that is not available shows as unavailable in the dashboard and in the Telegram and Discord reports instead of 0 B, and a saved manual choice is only re-checked when it changes.
2026-11-24 19:55:55 +03:30

59 lines
1.5 KiB
Go

package service
import (
"slices"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/util/sys"
)
const containerHistoryPrefix = "lxc-v1/"
func scopedMetric(metric string) bool {
return metric == "cpu" || metric == "swap"
}
func (s *ServerService) systemMetricKey(metric string) string {
if !scopedMetric(metric) {
return metric
}
if status := s.LastStatus(); status != nil {
if status.ResourceScope != "" {
return containerHistoryPrefix + metric
}
return metric
}
if s.lxcMetrics.UseScopedHistory() {
return containerHistoryPrefix + metric
}
return metric
}
// Scope metadata applies only to CPU, swap and uptime.
func applyLXCStats(status *Status, sample *sys.LXCStats) {
status.ResourceScope = sample.Scope
status.UnavailableMetrics = append([]string(nil), sample.Unavailable...)
status.Cpu = sample.CPU
status.CpuCores = sample.CPUs
status.LogicalPro = sample.CPUs
status.Swap.Current = sample.SwapUsed
status.Swap.Total = sample.SwapTotal
status.Uptime = sample.Uptime
}
// MetricAvailable distinguishes an unavailable metric from a measured zero.
// Legacy status responses omit metadata and retain their previous semantics.
func (s *Status) MetricAvailable(metric string) bool {
return s != nil && !slices.Contains(s.UnavailableMetrics, metric)
}
func appendScopedMetric(status *Status, metric string, t time.Time, value float64) {
if !status.MetricAvailable(metric) {
return
}
if status.ResourceScope != "" && scopedMetric(metric) {
metric = containerHistoryPrefix + metric
}
systemMetrics.append(metric, t, value)
}