Compare commits

...

10 Commits

Author SHA1 Message Date
henrygd
3534552d37 updates 2026-04-29 20:06:51 -04:00
henrygd
723401819f update 2026-04-29 18:41:42 -04:00
henrygd
2ea576c989 updates 2026-04-29 18:38:09 -04:00
henrygd
526a2c6aab updates 2026-04-29 18:21:39 -04:00
henrygd
aaa8eb773f updates 2026-04-29 18:05:40 -04:00
henrygd
099935e78e updates 2026-04-29 17:59:30 -04:00
henrygd
d2eb3b259a updates 2026-04-29 15:49:43 -04:00
henrygd
b89314889d update collections 2026-04-28 19:20:27 -04:00
henrygd
04e2b8b974 updates 2026-04-28 18:29:41 -04:00
henrygd
891b03426f updates 2026-04-28 17:46:56 -04:00
21 changed files with 804 additions and 484 deletions

View File

@@ -221,6 +221,5 @@ func (h *SyncNetworkProbesHandler) Handle(hctx *HandlerContext) error {
if err != nil {
return err
}
slog.Info("network probes synced", "action", req.Action)
return hctx.SendResponse(resp, hctx.RequestID)
}

View File

@@ -17,21 +17,20 @@ import (
"github.com/henrygd/beszel/internal/entities/probe"
)
// Probe functionality overview:
// Probes run at user-defined intervals (e.g., every 10s).
// To keep memory usage low and constant, data is stored in two layers:
// 1. Raw samples: The most recent individual results (kept for probeRawRetention).
// 2. Minute buckets: A fixed-size ring buffer of 61 buckets, each representing one
// 2. Minute buckets: A ring buffer of 61 buckets, each representing one
// wall-clock minute. Samples collected within the same minute are aggregated
// (sum, min, max, count) into a single bucket.
//
// Short-term requests (<= 2m) use raw samples for perfect accuracy.
// Short-term requests (<= 70s) use raw samples.
// Long-term requests (up to 1h) use the minute buckets to avoid storing thousands
// of individual data points.
const (
// probeRawRetention is the duration to keep individual samples for high-precision short-term requests
probeRawRetention = 70 * time.Second
// probeRawRetention is the duration to keep individual samples
probeRawRetention = 61 * time.Second
// probeMinuteBucketLen is the number of 1-minute buckets to keep (1 hour + 1 for partials)
probeMinuteBucketLen int32 = 61
)
@@ -147,27 +146,27 @@ func (agg probeAggregate) hasData() bool {
return agg.totalCount > 0
}
// result converts the aggregate into the probe result slice format.
// result converts the aggregate into the probe result format.
func (agg probeAggregate) result() probe.Result {
avg := agg.avgResponse()
minUs := 0.0
if agg.successCount > 0 {
minUs = float64(agg.minUs)
result := probe.Result{
AvgResponse: avg,
MinResponse: agg.minUs,
MaxResponse: agg.maxUs,
PacketLoss: agg.lossPercentage(),
}
return probe.Result{
avg,
minUs,
float64(agg.maxUs),
agg.lossPercentage(),
if agg.successCount == 0 {
result.MinResponse, result.MaxResponse = 0, 0
}
return result
}
// avgResponse returns the rounded average of successful samples.
func (agg probeAggregate) avgResponse() float64 {
func (agg probeAggregate) avgResponse() int64 {
if agg.successCount == 0 {
return 0
}
return float64(agg.sumUs / agg.successCount)
return agg.sumUs / agg.successCount
}
@@ -333,7 +332,8 @@ func (pm *ProbeManager) runProbe(task *probeTask, runNow bool) {
}
stagger := getStagger(interval.Milliseconds())
slog.Info("starting probe task", "id", task.config.ID, "initial_delay", stagger.String(), "interval", interval.String())
slog.Debug("starting probe task", "target", task.config.Target, "delay", stagger.String(), "interval", interval.String())
if runNow {
pm.executeProbe(task)
@@ -341,10 +341,9 @@ func (pm *ProbeManager) runProbe(task *probeTask, runNow bool) {
select {
case <-task.cancel:
slog.Info("removed probe", "id", task.config.ID)
// slog.Info("removed probe", "target", task.config.Target)
return
case <-time.After(stagger):
slog.Info("initial probe execution", "id", task.config.ID)
pm.executeProbe(task)
}
@@ -353,16 +352,15 @@ func (pm *ProbeManager) runProbe(task *probeTask, runNow bool) {
for {
select {
case <-task.cancel:
slog.Info("removed probe", "id", task.config.ID)
// slog.Info("removed probe", "target", task.config.Target)
return
case <-ticker:
slog.Info("running probe in main loop", "id", task.config.ID, "interval", interval.String())
pm.executeProbe(task)
}
}
}
// getStagger returns a random duration between intervalSeconds/2 and intervalSeconds to stagger probe executions
// getStagger returns a random duration between intervalSeconds/2 and intervalSeconds to stagger initial probe executions
func getStagger(intervalMilli int64) time.Duration {
intervalMilliInt := int(intervalMilli)
randomDelayInt := rand.Intn(intervalMilliInt)
@@ -383,6 +381,27 @@ func (pm *ProbeManager) runProbeNow(task *probeTask) *probe.Result {
return &result
}
// resultLocked returns the aggregated probe result for the requested duration along with a bool indicating whether any data was available.
func (task *probeTask) resultLocked(duration time.Duration, now time.Time) (probe.Result, bool) {
agg := task.aggregateLocked(duration, now)
hourAgg := task.aggregateLocked(time.Hour, now)
if !agg.hasData() {
return probe.Result{}, false
}
result := agg.result()
result.AvgResponse1h = hourAgg.avgResponse()
result.MinResponse1h = hourAgg.minUs
result.MaxResponse1h = hourAgg.maxUs
result.PacketLoss1h = hourAgg.lossPercentage()
if hourAgg.successCount == 0 {
result.MinResponse1h, result.MaxResponse1h = 0, 0
}
return result, true
}
// aggregateLocked collects probe data for the requested time window.
func (task *probeTask) aggregateLocked(duration time.Duration, now time.Time) probeAggregate {
cutoff := now.Add(-duration)
@@ -393,31 +412,6 @@ func (task *probeTask) aggregateLocked(duration time.Duration, now time.Time) pr
return aggregateBucketsSince(task.buckets[:], cutoff, now)
}
// resultLocked returns the aggregated probe result for the requested duration along with a bool indicating whether any data was available.
func (task *probeTask) resultLocked(duration time.Duration, now time.Time) (probe.Result, bool) {
agg := task.aggregateLocked(duration, now)
hourAgg := task.aggregateLocked(time.Hour, now)
if !agg.hasData() {
return nil, false
}
result := agg.result()
loss1m := result[3]
response1h := hourAgg.avgResponse()
loss1h := hourAgg.lossPercentage()
if hourAgg.successCount > 0 {
return probe.Result{
result[0],
response1h,
float64(hourAgg.minUs),
float64(hourAgg.maxUs),
loss1m,
loss1h,
}, true
}
return probe.Result{result[0], response1h, 0, 0, loss1m, loss1h}, true
}
// aggregateSamplesSince aggregates raw samples newer than the cutoff.
func aggregateSamplesSince(samples []probeSample, cutoff time.Time) probeAggregate {
agg := newProbeAggregate()
@@ -476,20 +470,26 @@ func (task *probeTask) addSampleLocked(sample probeSample) {
// executeProbe runs the configured probe and records the sample.
func (pm *ProbeManager) executeProbe(task *probeTask) {
// slog.Info("running probe", "id", task.config.ID, "interval", task.config.Interval)
var responseUs int64
var err error
switch task.config.Protocol {
case "icmp":
responseUs = probeICMP(task.config.Target)
responseUs, err = probeICMP(task.config.Target)
case "tcp":
responseUs = probeTCP(task.config.Target, task.config.Port)
responseUs, err = probeTCP(task.config.Target, task.config.Port)
case "http":
responseUs = probeHTTP(pm.httpClient, task.config.Target)
responseUs, err = probeHTTP(pm.httpClient, task.config.Target)
default:
slog.Warn("unknown probe protocol", "protocol", task.config.Protocol)
return
}
if err != nil {
slog.Warn("probe failed", "err", err, "target", task.config.Target, "protocol", task.config.Protocol)
}
sample := probeSample{
responseUs: responseUs,
timestamp: time.Now(),
@@ -501,12 +501,12 @@ func (pm *ProbeManager) executeProbe(task *probeTask) {
}
// probeTCP measures pure TCP handshake response (excluding DNS resolution).
// Returns -1 on failure.
func probeTCP(target string, port uint16) int64 {
// Returns -1 and an error on failure.
func probeTCP(target string, port uint16) (int64, error) {
// Resolve DNS first, outside the timing window
ips, err := net.LookupHost(target)
if err != nil || len(ips) == 0 {
return -1
return -1, err
}
addr := net.JoinHostPort(ips[0], fmt.Sprintf("%d", port))
@@ -514,25 +514,25 @@ func probeTCP(target string, port uint16) int64 {
start := time.Now()
conn, err := net.DialTimeout("tcp", addr, 3*time.Second)
if err != nil {
return -1
return -1, err
}
conn.Close()
return time.Since(start).Microseconds()
return time.Since(start).Microseconds(), nil
}
// probeHTTP measures HTTP GET request response in microseconds. Returns -1 on failure.
func probeHTTP(client *http.Client, url string) int64 {
// probeHTTP measures HTTP GET request response in microseconds. Returns -1 and an error on failure.
func probeHTTP(client *http.Client, url string) (int64, error) {
if client == nil {
client = http.DefaultClient
}
start := time.Now()
resp, err := client.Get(url)
if err != nil {
return -1
return -1, err
}
resp.Body.Close()
if resp.StatusCode >= 400 {
return -1
return -1, fmt.Errorf("HTTP error: %s", resp.Status)
}
return time.Since(start).Microseconds()
return time.Since(start).Microseconds(), nil
}

View File

@@ -1,6 +1,7 @@
package agent
import (
"errors"
"math"
"net"
"os"
@@ -27,7 +28,7 @@ type icmpPacketConn interface {
// icmpMethod tracks which ICMP approach to use. Once a method succeeds or
// all native methods fail, the choice is cached so subsequent probes skip
// the trial-and-error overhead.
type icmpMethod int
type icmpMethod uint8
const (
icmpUntried icmpMethod = iota // haven't tried yet
@@ -76,11 +77,11 @@ var (
// Supports both IPv4 and IPv6 targets. The ICMP method (raw socket,
// unprivileged datagram, or exec fallback) is detected once per address
// family and cached for subsequent probes.
// Returns response in microseconds, or -1 on failure.
func probeICMP(target string) int64 {
family, ip := resolveICMPTarget(target)
if family == nil {
return -1
// Returns response in microseconds, or -1 and an error on failure.
func probeICMP(target string) (int64, error) {
family, ip, err := resolveICMPTarget(target)
if err != nil {
return -1, err
}
icmpModeMu.Lock()
@@ -98,30 +99,30 @@ func probeICMP(target string) int64 {
case icmpExecFallback:
return probeICMPExec(target, family.isIPv6)
default:
return -1
return -1, errors.New("unsupported ICMP mode")
}
}
// resolveICMPTarget resolves a target hostname or IP to determine the address
// family and concrete IP address. Prefers IPv4 for dual-stack hostnames.
func resolveICMPTarget(target string) (*icmpFamily, net.IP) {
func resolveICMPTarget(target string) (*icmpFamily, net.IP, error) {
if ip := net.ParseIP(target); ip != nil {
if ip.To4() != nil {
return &icmpV4, ip.To4()
return &icmpV4, ip.To4(), nil
}
return &icmpV6, ip
return &icmpV6, ip, nil
}
ips, err := net.LookupIP(target)
if err != nil || len(ips) == 0 {
return nil, nil
return nil, nil, err
}
for _, ip := range ips {
if v4 := ip.To4(); v4 != nil {
return &icmpV4, v4
return &icmpV4, v4, nil
}
}
return &icmpV6, ips[0]
return &icmpV6, ips[0], nil
}
func detectICMPMode(family *icmpFamily, listen func(network, listenAddr string) (icmpPacketConn, error)) icmpMethod {
@@ -130,31 +131,28 @@ func detectICMPMode(family *icmpFamily, listen func(network, listenAddr string)
label = "IPv6"
}
if conn, err := listen(family.rawNetwork, family.listenAddr); err == nil {
conn, err := listen(family.rawNetwork, family.listenAddr)
slog.Debug("ICMP raw socket test", "family", label, "err", err)
if err == nil {
conn.Close()
slog.Info("ICMP probe using raw socket", "family", label)
return icmpRaw
} else {
slog.Debug("ICMP raw socket unavailable", "family", label, "err", err)
}
if conn, err := listen(family.dgramNetwork, family.listenAddr); err == nil {
conn, err = listen(family.dgramNetwork, family.listenAddr)
slog.Debug("ICMP datagram socket test", "family", label, "err", err)
if err == nil {
conn.Close()
slog.Info("ICMP probe using unprivileged datagram socket", "family", label)
return icmpDatagram
} else {
slog.Debug("ICMP datagram socket unavailable", "family", label, "err", err)
}
slog.Info("ICMP probe falling back to system ping command", "family", label)
return icmpExecFallback
}
// probeICMPNative sends an ICMP echo request using Go's x/net/icmp package.
func probeICMPNative(network string, family *icmpFamily, dst net.Addr) int64 {
func probeICMPNative(network string, family *icmpFamily, dst net.Addr) (int64, error) {
conn, err := icmp.ListenPacket(network, family.listenAddr)
if err != nil {
return -1
return -1, err
}
defer conn.Close()
@@ -170,7 +168,7 @@ func probeICMPNative(network string, family *icmpFamily, dst net.Addr) int64 {
}
msgBytes, err := msg.Marshal(nil)
if err != nil {
return -1
return -1, err
}
// Set deadline before sending
@@ -178,7 +176,7 @@ func probeICMPNative(network string, family *icmpFamily, dst net.Addr) int64 {
start := time.Now()
if _, err := conn.WriteTo(msgBytes, dst); err != nil {
return -1
return -1, err
}
// Read reply
@@ -186,23 +184,23 @@ func probeICMPNative(network string, family *icmpFamily, dst net.Addr) int64 {
for {
n, _, err := conn.ReadFrom(buf)
if err != nil {
return -1
return -1, err
}
reply, err := icmp.ParseMessage(family.proto, buf[:n])
if err != nil {
return -1
return -1, err
}
if reply.Type == family.replyType {
return time.Since(start).Microseconds()
return time.Since(start).Microseconds(), nil
}
// Ignore non-echo-reply messages (e.g. destination unreachable) and keep reading
}
}
// probeICMPExec falls back to the system ping command. Returns -1 on failure.
func probeICMPExec(target string, isIPv6 bool) int64 {
// probeICMPExec falls back to the system ping command. Returns -1 and an error on failure.
func probeICMPExec(target string, isIPv6 bool) (int64, error) {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
@@ -211,7 +209,7 @@ func probeICMPExec(target string, isIPv6 bool) int64 {
} else {
cmd = exec.Command("ping", "-n", "1", "-w", "3000", target)
}
default: // linux, darwin, freebsd
default:
if isIPv6 {
cmd = exec.Command("ping", "-6", "-c", "1", "-W", "3", target)
} else {
@@ -224,20 +222,20 @@ func probeICMPExec(target string, isIPv6 bool) int64 {
if err != nil {
// If ping fails but we got output, still try to parse
if len(output) == 0 {
return -1
return -1, err
}
}
matches := pingTimeRegex.FindSubmatch(output)
if len(matches) >= 2 {
if ms, err := strconv.ParseFloat(string(matches[1]), 64); err == nil {
return int64(math.Round(ms * 1000))
return int64(math.Round(ms * 1000)), nil
}
}
// Fallback: use wall clock time if ping succeeded but parsing failed
if err == nil {
return time.Since(start).Microseconds()
return time.Since(start).Microseconds(), nil
}
return -1
return -1, err
}

View File

@@ -96,21 +96,24 @@ func TestDetectICMPMode(t *testing.T) {
func TestResolveICMPTarget(t *testing.T) {
t.Run("IPv4 literal", func(t *testing.T) {
family, ip := resolveICMPTarget("127.0.0.1")
family, ip, err := resolveICMPTarget("127.0.0.1")
require.NoError(t, err)
require.NotNil(t, family)
assert.False(t, family.isIPv6)
assert.Equal(t, "127.0.0.1", ip.String())
})
t.Run("IPv6 literal", func(t *testing.T) {
family, ip := resolveICMPTarget("::1")
family, ip, err := resolveICMPTarget("::1")
require.NoError(t, err)
require.NotNil(t, family)
assert.True(t, family.isIPv6)
assert.Equal(t, "::1", ip.String())
})
t.Run("IPv4-mapped IPv6 resolves as IPv4", func(t *testing.T) {
family, ip := resolveICMPTarget("::ffff:127.0.0.1")
family, ip, err := resolveICMPTarget("::ffff:127.0.0.1")
require.NoError(t, err)
require.NotNil(t, family)
assert.False(t, family.isIPv6)
assert.Equal(t, "127.0.0.1", ip.String())

View File

@@ -24,10 +24,11 @@ func TestProbeTaskAggregateLockedUsesRawSamplesForShortWindows(t *testing.T) {
require.True(t, agg.hasData())
assert.Equal(t, int64(2), agg.totalCount)
assert.Equal(t, int64(1), agg.successCount)
assert.Equal(t, 20.0, agg.result()[0])
assert.Equal(t, 20.0, agg.result()[1])
assert.Equal(t, 20.0, agg.result()[2])
assert.Equal(t, 50.0, agg.result()[3])
result := agg.result()
assert.Equal(t, int64(20), result.AvgResponse)
assert.Equal(t, int64(20), result.MinResponse)
assert.Equal(t, int64(20), result.MaxResponse)
assert.Equal(t, 50.0, result.PacketLoss)
}
func TestProbeTaskAggregateLockedUsesMinuteBucketsForLongWindows(t *testing.T) {
@@ -44,10 +45,11 @@ func TestProbeTaskAggregateLockedUsesMinuteBucketsForLongWindows(t *testing.T) {
require.True(t, agg.hasData())
assert.Equal(t, int64(4), agg.totalCount)
assert.Equal(t, int64(3), agg.successCount)
assert.Equal(t, 30.0, agg.result()[0])
assert.Equal(t, 20.0, agg.result()[1])
assert.Equal(t, 40.0, agg.result()[2])
assert.Equal(t, 25.0, agg.result()[3])
result := agg.result()
assert.Equal(t, int64(30), result.AvgResponse)
assert.Equal(t, int64(20), result.MinResponse)
assert.Equal(t, int64(40), result.MaxResponse)
assert.Equal(t, 25.0, result.PacketLoss)
}
func TestProbeTaskAddSampleLockedTrimsRawSamplesButKeepsBucketHistory(t *testing.T) {
@@ -64,10 +66,11 @@ func TestProbeTaskAddSampleLockedTrimsRawSamplesButKeepsBucketHistory(t *testing
require.True(t, agg.hasData())
assert.Equal(t, int64(2), agg.totalCount)
assert.Equal(t, int64(2), agg.successCount)
assert.Equal(t, 15.0, agg.result()[0])
assert.Equal(t, 10.0, agg.result()[1])
assert.Equal(t, 20.0, agg.result()[2])
assert.Equal(t, 0.0, agg.result()[3])
result := agg.result()
assert.Equal(t, int64(15), result.AvgResponse)
assert.Equal(t, int64(10), result.MinResponse)
assert.Equal(t, int64(20), result.MaxResponse)
assert.Equal(t, 0.0, result.PacketLoss)
}
func TestProbeManagerGetResultsIncludesHourResponseRange(t *testing.T) {
@@ -84,13 +87,14 @@ func TestProbeManagerGetResultsIncludesHourResponseRange(t *testing.T) {
results := pm.GetResults(uint16(time.Minute / time.Millisecond))
result, ok := results["probe-1"]
require.True(t, ok)
require.Len(t, result, 6)
assert.Equal(t, 30.0, result[0])
assert.Equal(t, 25.0, result[1])
assert.Equal(t, 10.0, result[2])
assert.Equal(t, 40.0, result[3])
assert.Equal(t, 50.0, result[4])
assert.Equal(t, 20.0, result[5])
assert.Equal(t, int64(30), result.AvgResponse)
assert.Equal(t, int64(25), result.AvgResponse1h)
assert.Equal(t, int64(30), result.MinResponse)
assert.Equal(t, int64(10), result.MinResponse1h)
assert.Equal(t, int64(30), result.MaxResponse)
assert.Equal(t, int64(40), result.MaxResponse1h)
assert.Equal(t, 50.0, result.PacketLoss)
assert.Equal(t, 20.0, result.PacketLoss1h)
}
func TestProbeManagerGetResultsIncludesLossOnlyHourData(t *testing.T) {
@@ -104,13 +108,14 @@ func TestProbeManagerGetResultsIncludesLossOnlyHourData(t *testing.T) {
results := pm.GetResults(uint16(time.Minute / time.Millisecond))
result, ok := results["probe-1"]
require.True(t, ok)
require.Len(t, result, 6)
assert.Equal(t, 0.0, result[0])
assert.Equal(t, 0.0, result[1])
assert.Equal(t, 0.0, result[2])
assert.Equal(t, 0.0, result[3])
assert.Equal(t, 100.0, result[4])
assert.Equal(t, 100.0, result[5])
assert.Equal(t, int64(0), result.AvgResponse)
assert.Equal(t, int64(0), result.AvgResponse1h)
assert.Equal(t, int64(0), result.MinResponse)
assert.Equal(t, int64(0), result.MinResponse1h)
assert.Equal(t, int64(0), result.MaxResponse)
assert.Equal(t, int64(0), result.MaxResponse1h)
assert.Equal(t, 100.0, result.PacketLoss)
assert.Equal(t, 100.0, result.PacketLoss1h)
}
func TestProbeConfigResultKeyUsesSyncedID(t *testing.T) {
@@ -207,10 +212,9 @@ func TestProbeManagerApplySyncUpsertRunsImmediatelyAndReturnsResult(t *testing.T
defer pm.Stop()
require.NoError(t, err)
require.Len(t, resp.Result, 6)
assert.GreaterOrEqual(t, resp.Result[0], 0.0)
assert.Equal(t, 0.0, resp.Result[4])
assert.Equal(t, 0.0, resp.Result[5])
assert.GreaterOrEqual(t, resp.Result.AvgResponse, int64(0))
assert.Equal(t, 0.0, resp.Result.PacketLoss)
assert.Equal(t, 0.0, resp.Result.PacketLoss1h)
task := pm.probes["probe-1"]
require.NotNil(t, task)
@@ -252,7 +256,7 @@ func TestProbeManagerUpsertProbeKeepsHistoryWhenOnlyIntervalChanges(t *testing.T
require.True(t, agg.hasData())
assert.Equal(t, int64(2), agg.totalCount)
assert.Equal(t, int64(2), agg.successCount)
assert.Equal(t, 18.0, agg.avgResponse())
assert.Equal(t, int64(18), agg.avgResponse())
select {
case <-existingTask.cancel:
@@ -299,7 +303,8 @@ func TestProbeHTTP(t *testing.T) {
}))
defer server.Close()
responseUs := probeHTTP(server.Client(), server.URL)
responseUs, err := probeHTTP(server.Client(), server.URL)
require.NoError(t, err)
assert.GreaterOrEqual(t, responseUs, int64(0))
})
@@ -309,7 +314,9 @@ func TestProbeHTTP(t *testing.T) {
}))
defer server.Close()
assert.Equal(t, int64(-1), probeHTTP(server.Client(), server.URL))
responseUs, err := probeHTTP(server.Client(), server.URL)
assert.Equal(t, int64(-1), responseUs)
require.Error(t, err)
})
}
@@ -329,7 +336,8 @@ func TestProbeTCP(t *testing.T) {
}()
port := uint16(listener.Addr().(*net.TCPAddr).Port)
responseUs := probeTCP("127.0.0.1", port)
responseUs, err := probeTCP("127.0.0.1", port)
require.NoError(t, err)
assert.GreaterOrEqual(t, responseUs, int64(0))
<-accepted
})
@@ -341,6 +349,8 @@ func TestProbeTCP(t *testing.T) {
port := uint16(listener.Addr().(*net.TCPAddr).Port)
require.NoError(t, listener.Close())
assert.Equal(t, int64(-1), probeTCP("127.0.0.1", port))
responseUs, err := probeTCP("127.0.0.1", port)
assert.Equal(t, int64(-1), responseUs)
require.Error(t, err)
})
}

View File

@@ -38,21 +38,46 @@ type SyncResponse struct {
//
// 0: avg response in microseconds
//
// 1: average response over the last hour in microseconds
// 1: 1h average response in microseconds
//
// 2: min response over the last hour in microseconds
// 2: min response in microseconds
//
// 3: max response over the last hour in microseconds
// 3: 1h min response in microseconds
//
// 4: packet loss percentage (0-100)
// 4: max response in microseconds
//
// 5: packet loss percentage over the last hour (0-100)
type Result []float64
// Get returns the value at the specified index or 0 if the index is out of range.
func (r Result) Get(index int) float64 {
if index < len(r) {
return r[index]
}
return 0
// 5: 1h max response in microseconds
//
// 6: packet loss percentage (0-100)
//
// 7: 1h packet loss percentage (0-100)
type Result struct {
AvgResponse int64 `cbor:"0,keyasint,omitempty"`
AvgResponse1h int64 `cbor:"1,keyasint,omitempty"`
MinResponse int64 `cbor:"2,keyasint,omitempty"`
MinResponse1h int64 `cbor:"3,keyasint,omitempty"`
MaxResponse int64 `cbor:"4,keyasint,omitempty"`
MaxResponse1h int64 `cbor:"5,keyasint,omitempty"`
PacketLoss float64 `cbor:"6,keyasint,omitempty"`
PacketLoss1h float64 `cbor:"7,keyasint,omitempty"`
}
// Stats holds only 1m values for a single target, which are used for charts.
//
// 0: avg response in microseconds
//
// 1: min response in microseconds
//
// 2: max response in microseconds
//
// 3: packet loss percentage (0-100)
type Stats []float64
func (s Stats) FromResult(result Result) Stats {
return Stats{
float64(result.AvgResponse),
float64(result.MinResponse),
float64(result.MaxResponse),
result.PacketLoss,
}
}

View File

@@ -12,7 +12,12 @@ import (
// generateProbeID creates a stable hash ID for a probe based on its configuration and the system it belongs to.
func generateProbeID(systemId string, config probe.Config) string {
return systems.MakeStableHashId(systemId, config.Target, config.Protocol, strconv.FormatUint(uint64(config.Port), 10))
args := []string{systemId, config.Target, config.Protocol}
// only use port for TCP probes, since for other protocols it's not relevant as standalone value
if config.Protocol == "tcp" {
args = append(args, strconv.FormatUint(uint64(config.Port), 10))
}
return systems.MakeStableHashId(args...)
}
// bindNetworkProbesEvents keeps probe records and agent probe state in sync.
@@ -36,30 +41,22 @@ func bindNetworkProbesEvents(hub *Hub) {
return nil
}
// if system connected, run the probe immediately
// if not, return and wait for the system to connect and sync probes then
// if not, return and wait for the system to connect and sync probes on reg schedule
system, err := hub.sm.GetSystem(e.Record.GetString("system"))
if err != nil || system.Status != "up" {
return nil
if err == nil && system.Status == "up" {
go hub.upsertNetworkProbe(e.Record, true)
}
result, err := hub.upsertNetworkProbe(e.Record, true)
if err != nil {
hub.Logger().Warn("failed to sync probe to agent", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
return nil
}
if result == nil {
return nil
}
setProbeResultFields(e.Record, *result)
if err := e.App.SaveNoValidate(e.Record); err != nil {
hub.Logger().Warn("failed to save initial probe result", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
}
return e.Next()
return err
})
// On API update requests, if the probe config changed in a way that requires a new ID, we will create a new
// record with the new ID and delete the old one. Otherwise, we will just update the existing probe on the agent.
// On API update requests, if the probe config changed in a way that requires a new ID, create a new
// record with the new ID and delete the old one. Otherwise, just update the existing probe on the agent.
hub.OnRecordUpdateRequest("network_probes").BindFunc(func(e *core.RecordRequestEvent) error {
systemID := e.Record.GetString("system")
// only tcp uses port - set other protocols port to zero
if e.Record.GetString("protocol") != "tcp" {
e.Record.Set("port", 0)
}
ID := generateProbeID(systemID, *probeConfigFromRecord(e.Record))
if ID != e.Record.Id {
newRecord := copyProbeToNewRecord(e.Record, ID)
@@ -73,18 +70,15 @@ func bindNetworkProbesEvents(hub *Hub) {
}
err := e.Next()
if e.Record.GetBool("enabled") {
var result *probe.Result
// if the probe is enabled, sync the updated config to the agent now
runNow := !e.Record.Original().GetBool("enabled")
result, err = hub.upsertNetworkProbe(e.Record, runNow)
if result != nil {
setProbeResultFields(e.Record, *result)
_ = e.App.SaveNoValidate(e.Record)
}
err = hub.upsertNetworkProbe(e.Record, runNow)
} else {
// if the probe is paused, remove it from the agent
err = hub.deleteNetworkProbe(e.Record)
}
if err != nil {
hub.Logger().Warn("failed to sync updated probe", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
hub.Logger().Warn("failed to sync updated probe", "system", systemID, "probe", e.Record.Id, "err", err)
}
return nil
})
@@ -111,14 +105,12 @@ func probeConfigFromRecord(record *core.Record) *probe.Config {
// setProbeResultFields stores the latest probe result values on the record.
func setProbeResultFields(record *core.Record, result probe.Result) {
now := time.Now().UTC()
nowString := now.Format(types.DefaultDateLayout)
record.Set("res", result.Get(0))
record.Set("resAvg1h", result.Get(1))
record.Set("resMin1h", result.Get(2))
record.Set("resMax1h", result.Get(3))
record.Set("loss", result.Get(4))
record.Set("loss1h", result.Get(5))
nowString := time.Now().UTC().Format(types.DefaultDateLayout)
record.Set("res", result.AvgResponse)
record.Set("resAvg1h", result.AvgResponse1h)
record.Set("resMin1h", result.MinResponse1h)
record.Set("resMax1h", result.MaxResponse1h)
record.Set("loss1h", result.PacketLoss1h)
record.Set("updated", nowString)
}
@@ -128,19 +120,28 @@ func setProbeResultFields(record *core.Record, result probe.Result) {
func copyProbeToNewRecord(oldRecord *core.Record, newID string) *core.Record {
collection := oldRecord.Collection()
newRecord := core.NewRecord(collection)
newRecord.Load(oldRecord.FieldsData())
newRecord.Set("id", newID)
newRecord.Id = newID
fields := []string{"system", "name", "target", "protocol", "port", "interval", "enabled"}
for _, field := range fields {
newRecord.Set(field, oldRecord.Get(field))
}
return newRecord
}
// upsertNetworkProbe applies the record's probe config to the target system.
func (h *Hub) upsertNetworkProbe(record *core.Record, runNow bool) (*probe.Result, error) {
// upsertNetworkProbe creates or updates the record's probe on the target system. If runNow
// is true, it will also trigger an immediate probe run and update the record with the result.
func (h *Hub) upsertNetworkProbe(record *core.Record, runNow bool) error {
systemID := record.GetString("system")
system, err := h.sm.GetSystem(systemID)
if err != nil {
return nil, err
return err
}
return system.UpsertNetworkProbe(*probeConfigFromRecord(record), runNow)
result, err := system.UpsertNetworkProbe(*probeConfigFromRecord(record), runNow)
if err != nil || result == nil {
return err
}
setProbeResultFields(record, *result)
return h.App.SaveNoValidate(record)
}
// deleteNetworkProbe removes the record's probe from the target system.

View File

@@ -4,7 +4,9 @@ import (
"testing"
"github.com/henrygd/beszel/internal/entities/probe"
"github.com/pocketbase/pocketbase/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenerateProbeID(t *testing.T) {
@@ -20,10 +22,21 @@ func TestGenerateProbeID(t *testing.T) {
config: probe.Config{
Protocol: "http",
Target: "example.com",
Port: 80,
Port: 0,
Interval: 60,
},
expected: "de7b3647",
expected: "a20a5827",
},
{
name: "HTTP probe on example.com with different port",
systemID: "sys123",
config: probe.Config{
Protocol: "http",
Target: "example.com",
Port: 8080,
Interval: 60,
},
expected: "a20a5827",
},
{
name: "HTTP probe on example.com with different system ID",
@@ -34,7 +47,7 @@ func TestGenerateProbeID(t *testing.T) {
Port: 80,
Interval: 60,
},
expected: "be9e2707",
expected: "ab602ae7",
},
{
name: "Same probe, different interval",
@@ -45,7 +58,7 @@ func TestGenerateProbeID(t *testing.T) {
Port: 80,
Interval: 120,
},
expected: "be9e2707",
expected: "ab602ae7",
},
{
name: "ICMP probe on 1.1.1.1",
@@ -56,7 +69,7 @@ func TestGenerateProbeID(t *testing.T) {
Port: 0,
Interval: 10,
},
expected: "49ec14fc",
expected: "6d13a4a4",
}, {
name: "ICMP probe on 1.1.1.1 with different system ID",
systemID: "sys4567",
@@ -66,7 +79,7 @@ func TestGenerateProbeID(t *testing.T) {
Port: 0,
Interval: 10,
},
expected: "84921aa3",
expected: "ddd6c81",
},
{
name: "TCP probe on example.com with port 443",
@@ -99,3 +112,44 @@ func TestGenerateProbeID(t *testing.T) {
})
}
}
func TestCopyProbeToNewRecordDropsResultFields(t *testing.T) {
hub, testApp, err := createTestHub(t)
require.NoError(t, err)
defer cleanupTestHub(hub, testApp)
collection, err := hub.FindCachedCollectionByNameOrId("network_probes")
require.NoError(t, err)
oldRecord := core.NewRecord(collection)
oldRecord.Load(map[string]any{
"system": "sys123",
"name": "Example",
"target": "https://example.com",
"protocol": "http",
"port": 443,
"interval": 60,
"enabled": true,
"res": 1200,
"resAvg1h": 1300,
"resMin1h": 900,
"resMax1h": 1600,
"loss1h": 5,
"updated": "2026-04-29 12:00:00.000Z",
})
newRecord := copyProbeToNewRecord(oldRecord, "next12345")
assert.Equal(t, "next12345", newRecord.Id)
assert.Equal(t, "Example", newRecord.GetString("name"))
assert.Equal(t, "https://example.com", newRecord.GetString("target"))
assert.Equal(t, "http", newRecord.GetString("protocol"))
assert.Equal(t, 443, newRecord.GetInt("port"))
assert.True(t, newRecord.GetBool("enabled"))
assert.Zero(t, newRecord.GetFloat("res"))
assert.Zero(t, newRecord.GetFloat("resAvg1h"))
assert.Zero(t, newRecord.GetFloat("resMin1h"))
assert.Zero(t, newRecord.GetFloat("resMax1h"))
assert.Zero(t, newRecord.GetFloat("loss1h"))
assert.Equal(t, "", newRecord.GetString("updated"))
}

View File

@@ -30,6 +30,7 @@ import (
"github.com/lxzan/gws"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/security"
"github.com/pocketbase/pocketbase/tools/types"
"golang.org/x/crypto/ssh"
)
@@ -314,16 +315,16 @@ func createSystemdStatsRecords(app core.App, data []*systemd.Service, systemId s
return err
}
func updateNetworkProbesRecords(app core.App, data map[string]probe.Result, systemId string) error {
if len(data) == 0 {
func updateNetworkProbesRecords(app core.App, probeResults map[string]probe.Result, systemId string) error {
if len(probeResults) == 0 {
return nil
}
var err error
collectionName := "network_probes"
const probeCollectionName = "network_probes"
// If realtime updates are active, we save via PocketBase records to trigger realtime events.
// Otherwise we can do a more efficient direct update via SQL
realtimeActive := utils.RealtimeActiveForCollection(app, collectionName, func(filterQuery string) bool {
realtimeActive := utils.RealtimeActiveForCollection(app, probeCollectionName, func(filterQuery string) bool {
return !strings.Contains(filterQuery, "system") || strings.Contains(filterQuery, systemId)
})
@@ -334,63 +335,68 @@ func updateNetworkProbesRecords(app core.App, data map[string]probe.Result, syst
var updateQuery *dbx.Query
if !realtimeActive {
db = app.DB()
sql := fmt.Sprintf("UPDATE %s SET res={:res}, resMin1h={:resMin1h}, resMax1h={:resMax1h}, resAvg1h={:resAvg1h}, loss={:loss}, loss1h={:loss1h}, updated={:updated} WHERE id={:id}", collectionName)
updateQuery = db.NewQuery(sql)
probeFields := []string{"res", "resMin1h", "resMax1h", "resAvg1h", "loss1h", "updated"}
setClauses := make([]string, len(probeFields))
for i, f := range probeFields {
setClauses[i] = fmt.Sprintf("%s={:%s}", f, f)
}
queryString := fmt.Sprintf("UPDATE %s SET %s WHERE id={:id}", probeCollectionName, strings.Join(setClauses, ", "))
updateQuery = db.NewQuery(queryString)
}
// update network_probes records
for id, values := range data {
for id, result := range probeResults {
probeData := map[string]any{
"id": id,
"res": result.AvgResponse,
"resAvg1h": result.AvgResponse1h,
"resMin1h": result.MinResponse1h,
"resMax1h": result.MaxResponse1h,
"loss1h": result.PacketLoss1h,
"updated": nowString,
}
switch realtimeActive {
case true:
var record *core.Record
record, err = app.FindRecordById(collectionName, id)
record, err = app.FindRecordById(probeCollectionName, id)
if err == nil {
record.Set("res", values.Get(0))
record.Set("resAvg1h", values.Get(1))
record.Set("resMin1h", values.Get(2))
record.Set("resMax1h", values.Get(3))
record.Set("loss", values.Get(4))
record.Set("loss1h", values.Get(5))
record.Set("updated", nowString)
record.Load(probeData)
err = app.SaveNoValidate(record)
}
default:
_, err = updateQuery.Bind(dbx.Params{
"id": id,
"res": values.Get(0),
"resAvg1h": values.Get(1),
"resMin1h": values.Get(2),
"resMax1h": values.Get(3),
"loss": values.Get(4),
"loss1h": values.Get(5),
"updated": nowString,
}).Execute()
_, err = updateQuery.Bind(dbx.Params(probeData)).Execute()
}
if err != nil {
app.Logger().Warn("Failed to update probe", "system", systemId, "probe", id, "err", err)
}
}
// insert network probe stats records
switch realtimeActive {
case true:
collection, _ := app.FindCachedCollectionByNameOrId("network_probe_stats")
record := core.NewRecord(collection)
record.Set("system", systemId)
record.Set("stats", data)
record.Set("type", "1m")
record.Set("created", nowMilli)
err = app.SaveNoValidate(record)
default:
var statsJson types.JSONRaw
if err := statsJson.Scan(data); err == nil {
insertQuery := db.NewQuery("INSERT INTO network_probe_stats (system, stats, type, created) VALUES ({:system}, {:stats}, {:type}, {:created})")
_, err = insertQuery.Bind(dbx.Params{
"system": systemId,
"stats": statsJson,
"type": "1m",
"created": nowMilli,
}).Execute()
// handle stats collection as well
const statsCollectionName = "network_probe_stats"
// we don't need the hour values for the stats collection
stats := make(map[string]probe.Stats, len(probeResults))
for key, result := range probeResults {
stats[key] = probe.Stats{}.FromResult(result)
}
statsRecordData := map[string]any{
"system": systemId,
"type": "1m",
"created": nowMilli,
}
var statsJson types.JSONRaw
if err = statsJson.Scan(stats); err == nil {
statsRecordData["stats"] = statsJson
switch realtimeActive {
case true:
collection, _ := app.FindCachedCollectionByNameOrId(statsCollectionName)
record := core.NewRecord(collection)
record.Load(statsRecordData)
err = app.SaveNoValidate(record)
default:
statsRecordData["id"] = security.PseudorandomStringWithAlphabet(10, core.DefaultIdAlphabet)
_, err = db.Insert(statsCollectionName, dbx.Params(statsRecordData)).Execute()
}
}
if err != nil {

View File

@@ -24,7 +24,7 @@ func (sys *System) UpsertNetworkProbe(config probe.Config, runNow bool) (*probe.
if err != nil {
return nil, err
}
if len(resp.Result) == 0 {
if resp.Result == (probe.Result{}) {
return nil, nil
}
result := resp.Result

View File

@@ -1701,21 +1701,15 @@ func init() {
"viewRule": null
},
{
"id": "np_probes_001",
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"name": "network_probes",
"type": "base",
"fields": [
{
"autogeneratePattern": "[a-z0-9]{15}",
"autogeneratePattern": "[a-z0-9]{10}",
"hidden": false,
"id": "text3208210256",
"max": 15,
"min": 15,
"max": 10,
"min": 6,
"name": "id",
"pattern": "^[a-z0-9]+$",
"presentable": false,
@@ -1738,6 +1732,7 @@ func init() {
"type": "relation"
},
{
"autogeneratePattern": "",
"hidden": false,
"id": "np_name",
"max": 200,
@@ -1751,6 +1746,7 @@ func init() {
"type": "text"
},
{
"autogeneratePattern": "",
"hidden": false,
"id": "np_target",
"max": 500,
@@ -1772,7 +1768,11 @@ func init() {
"required": true,
"system": false,
"type": "select",
"values": ["icmp", "tcp", "http"]
"values": [
"icmp",
"tcp",
"http"
]
},
{
"hidden": false,
@@ -1798,6 +1798,66 @@ func init() {
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "number926446584",
"max": null,
"min": null,
"name": "res",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "number1006954605",
"max": null,
"min": null,
"name": "resAvg1h",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "number4267669802",
"max": null,
"min": null,
"name": "resMin1h",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "number591433223",
"max": null,
"min": null,
"name": "resMax1h",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "number3726709001",
"max": null,
"min": null,
"name": "loss1h",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"hidden": false,
"id": "np_enabled",
@@ -1819,36 +1879,37 @@ func init() {
},
{
"hidden": false,
"id": "autodate3332085495",
"id": "date3332085495",
"max": "",
"min": "",
"name": "updated",
"onCreate": true,
"onUpdate": true,
"presentable": false,
"required": false,
"system": false,
"type": "autodate"
"type": "date"
}
],
"id": "np_probes_001",
"indexes": [
"CREATE INDEX ` + "`" + `idx_np_system_enabled` + "`" + ` ON ` + "`" + `network_probes` + "`" + ` (\n ` + "`" + `system` + "`" + `,\n ` + "`" + `enabled` + "`" + `\n)"
"CREATE INDEX ` + "`" + `idx_np_system_enabled` + "`" + ` ON ` + "`" + `network_probes` + "`" + ` (` + "`" + `system` + "`" + `)"
],
"system": false
"listRule": null,
"name": "network_probes",
"system": false,
"type": "base",
"updateRule": null,
"viewRule": null
},
{
"id": "np_stats_001",
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"name": "network_probe_stats",
"type": "base",
"fields": [
{
"autogeneratePattern": "[a-z0-9]{15}",
"autogeneratePattern": "[a-z0-9]{10}",
"hidden": false,
"id": "text3208210256",
"max": 15,
"min": 15,
"max": 10,
"min": 10,
"name": "id",
"pattern": "^[a-z0-9]+$",
"presentable": false,
@@ -1889,33 +1950,37 @@ func init() {
"required": true,
"system": false,
"type": "select",
"values": ["1m", "10m", "20m", "120m", "480m"]
"values": [
"1m",
"10m",
"20m",
"120m",
"480m"
]
},
{
"hidden": false,
"id": "autodate2990389176",
"id": "number2990389176",
"max": null,
"min": null,
"name": "created",
"onCreate": true,
"onUpdate": false,
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "autodate"
},
{
"hidden": false,
"id": "autodate3332085495",
"name": "updated",
"onCreate": true,
"onUpdate": true,
"presentable": false,
"system": false,
"type": "autodate"
"type": "number"
}
],
"id": "np_stats_001",
"indexes": [
"CREATE INDEX ` + "`" + `idx_nps_system_type_created` + "`" + ` ON ` + "`" + `network_probe_stats` + "`" + ` (\n ` + "`" + `system` + "`" + `,\n ` + "`" + `type` + "`" + `,\n ` + "`" + `created` + "`" + `\n)"
],
"system": false
"listRule": null,
"name": "network_probe_stats",
"system": false,
"type": "base",
"updateRule": null,
"viewRule": null
}
]`

View File

@@ -32,13 +32,13 @@ func TestAverageProbeStats(t *testing.T) {
recordA, err := tests.CreateRecord(hub, "network_probe_stats", map[string]any{
"system": system.Id,
"type": "1m",
"stats": `{"icmp:1.1.1.1":[10,80,8,14,1]}`,
"stats": `{"icmp:1.1.1.1":[10,5,20,1.5]}`,
})
require.NoError(t, err)
recordB, err := tests.CreateRecord(hub, "network_probe_stats", map[string]any{
"system": system.Id,
"type": "1m",
"stats": `{"icmp:1.1.1.1":[40,100,9,50,5]}`,
"stats": `{"icmp:1.1.1.1":[22.5,10,60,0]}`,
})
require.NoError(t, err)
@@ -49,10 +49,9 @@ func TestAverageProbeStats(t *testing.T) {
stats, ok := result["icmp:1.1.1.1"]
require.True(t, ok)
require.Len(t, stats, 5)
assert.Equal(t, 25.0, stats[0])
assert.Equal(t, 90.0, stats[1])
assert.Equal(t, 8.0, stats[2])
assert.Equal(t, 50.0, stats[3])
assert.Equal(t, 3.0, stats[4])
require.Len(t, stats, 4)
assert.InDelta(t, 16.25, stats[0], 0.001) // avg of avg
assert.InDelta(t, 5, stats[1], 0.001) // min of mins
assert.InDelta(t, 60, stats[2], 0.001) // max of maxes
assert.InDelta(t, 0.75, stats[3], 0.001) // avg of packet loss
}

View File

@@ -174,7 +174,7 @@ func (rm *RecordManager) CreateLongerRecords() {
return nil
})
// log.Println("finished creating longer records", "time (ms)", time.Since(start).Milliseconds())
// slog.Info("finished creating longer records", "time (ms)", time.Since(now).Milliseconds())
}
func getCreatedTimeField(collectionName string, period time.Time) any {
@@ -532,9 +532,9 @@ func AverageContainerStatsSlice(records [][]container.Stats) []container.Stats {
// AverageProbeStats averages probe stats across multiple records.
// For each probe key: avg of average fields, min of mins, and max of maxes.
func (rm *RecordManager) AverageProbeStats(db dbx.Builder, records RecordIds) map[string]probe.Result {
func (rm *RecordManager) AverageProbeStats(db dbx.Builder, records RecordIds) map[string]probe.Stats {
type probeValues struct {
sums probe.Result
sums probe.Stats
counts []int
}
@@ -546,18 +546,18 @@ func (rm *RecordManager) AverageProbeStats(db dbx.Builder, records RecordIds) ma
for _, rec := range records {
row.Stats = row.Stats[:0]
query.Bind(dbx.Params{"id": rec.Id}).One(&row)
var rawStats map[string]probe.Result
var rawStats map[string]probe.Stats
if err := json.Unmarshal(row.Stats, &rawStats); err != nil {
continue
}
for key, vals := range rawStats {
s, ok := sums[key]
if !ok {
s = &probeValues{sums: make(probe.Result, len(vals)), counts: make([]int, len(vals))}
s = &probeValues{sums: make(probe.Stats, len(vals)), counts: make([]int, len(vals))}
sums[key] = s
}
if len(vals) > len(s.sums) {
expandedSums := make(probe.Result, len(vals))
expandedSums := make(probe.Stats, len(vals))
copy(expandedSums, s.sums)
s.sums = expandedSums
@@ -567,11 +567,11 @@ func (rm *RecordManager) AverageProbeStats(db dbx.Builder, records RecordIds) ma
}
for i := range vals {
switch i {
case 2: // min fields
case 1: // min fields
if s.counts[i] == 0 || vals[i] < s.sums[i] {
s.sums[i] = vals[i]
}
case 3: // max fields
case 2: // max fields
if s.counts[i] == 0 || vals[i] > s.sums[i] {
s.sums[i] = vals[i]
}
@@ -584,14 +584,14 @@ func (rm *RecordManager) AverageProbeStats(db dbx.Builder, records RecordIds) ma
}
// compute final averages
result := make(map[string]probe.Result, len(sums))
result := make(map[string]probe.Stats, len(sums))
for key, s := range sums {
if len(s.counts) == 0 {
continue
}
for i := range s.sums {
switch i {
case 2, 3: // min and max fields should not be averaged
case 1, 2: // min and max fields should not be averaged
continue
default:
if s.counts[i] > 0 {

View File

@@ -1,6 +1,6 @@
import type { CellContext, Column, ColumnDef } from "@tanstack/react-table"
import { Button } from "@/components/ui/button"
import { cn, formatMicroseconds, hourWithSeconds } from "@/lib/utils"
import { cn, copyToClipboard, decimalString, formatMicroseconds, hourWithSeconds } from "@/lib/utils"
import {
GlobeIcon,
TimerIcon,
@@ -15,6 +15,7 @@ import {
PenBoxIcon,
PauseCircleIcon,
PlayCircleIcon,
CopyIcon,
} from "lucide-react"
import { t } from "@lingui/core/macro"
import type { NetworkProbeRecord, SystemRecord } from "@/types"
@@ -31,11 +32,13 @@ import { useStore } from "@nanostores/react"
import { SystemStatus } from "@/lib/enums"
import { Checkbox } from "@/components/ui/checkbox"
import { useMemo } from "react"
import { formatBulkProbeLine } from "@/components/network-probes-table/probe-dialog"
import { Badge } from "../ui/badge"
const protocolColors: Record<string, string> = {
icmp: "bg-blue-500/15 text-blue-400",
tcp: "bg-purple-500/15 text-purple-400",
http: "bg-green-500/15 text-green-400",
icmp: "bg-blue-500/15! text-blue-600 dark:text-blue-400",
tcp: "bg-purple-500/15! text-purple-600 dark:text-purple-400",
http: "bg-green-500/15! text-green-700 dark:text-green-400",
}
const SYSTEM_STATUS_COLORS = {
@@ -95,9 +98,17 @@ export function getProbeColumns(
header: ({ column }) => <HeaderButton column={column} name={t`Name`} Icon={NetworkIcon} />,
cell: ({ row, getValue }) => {
const probe = row.original
const { status } = useStore($allSystemsById)[probe.system] || {}
let color = "bg-green-500"
if (!probe.enabled || status === SystemStatus.Paused) {
color = "bg-primary/40"
} else if (status === SystemStatus.Down || status === SystemStatus.Pending) {
color = "bg-yellow-500"
}
return (
<div className="ms-1.5 max-w-40 flex gap-2 items-center tabular-nums">
<span className={cn("shrink-0 size-2 rounded-full", probe.enabled ? "bg-green-500" : "bg-primary/40")} />
<span className={cn("shrink-0 size-2 rounded-full", color)} />
<div className="relative w-fit min-w-0 max-w-full">
<span className="invisible block overflow-hidden whitespace-nowrap" aria-hidden="true">
{longestName}
@@ -115,7 +126,11 @@ export function getProbeColumns(
const allSystems = $allSystemsById.get()
const systemNameA = allSystems[a.original.system]?.name ?? ""
const systemNameB = allSystems[b.original.system]?.name ?? ""
return systemNameA.localeCompare(systemNameB)
const primary = systemNameA.localeCompare(systemNameB)
if (primary !== 0) {
return primary
}
return (a.original.name || a.original.target).localeCompare(b.original.name || b.original.target)
},
header: ({ column }) => <HeaderButton column={column} name={t`System`} Icon={ServerIcon} />,
cell: ({ getValue }) => {
@@ -160,16 +175,13 @@ export function getProbeColumns(
header: ({ column }) => <HeaderButton column={column} name={t`Protocol`} Icon={ArrowLeftRightIcon} />,
cell: ({ getValue }) => {
const protocol = getValue() as string
return (
<span className={cn("ms-1.5 px-2 py-0.5 rounded text-xs font-medium uppercase", protocolColors[protocol])}>
{protocol}
</span>
)
return <Badge className={cn("uppercase", protocolColors[protocol])}>{protocol}</Badge>
},
},
{
id: "interval",
accessorFn: (record) => record.interval,
invertSorting: true,
header: ({ column }) => <HeaderButton column={column} name={t`Interval`} Icon={RefreshCwIcon} />,
cell: ({ getValue }) => <span className="ms-1.5 tabular-nums">{getValue() as number}s</span>,
},
@@ -224,7 +236,7 @@ export function getProbeColumns(
return (
<span className="ms-1.5 tabular-nums flex gap-2 items-center">
<span className={cn("shrink-0 size-2 rounded-full", color)} />
{loss1h}%
{loss1h === 100 ? loss1h : decimalString(loss1h, loss1h >= 10 ? 1 : 2)}%
</span>
)
},
@@ -256,6 +268,7 @@ export function getProbeColumns(
: [row.original]
const isBulkAction = actionRows.length > 1
const shouldPause = actionRows.some((probe) => probe.enabled)
const bulkCopyContent = actionRows.map((probe) => formatBulkProbeLine(probe)).join("\n")
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
@@ -269,8 +282,7 @@ export function getProbeColumns(
<DropdownMenuContent align="end" onClick={(event) => event.stopPropagation()}>
{!isBulkAction && (
<DropdownMenuItem
onClick={(event) => {
event.stopPropagation()
onClick={() => {
onEdit?.(row.original)
}}
>
@@ -279,8 +291,7 @@ export function getProbeColumns(
</DropdownMenuItem>
)}
<DropdownMenuItem
onClick={(event) => {
event.stopPropagation()
onClick={() => {
onSetEnabled?.(actionRows, !shouldPause)
}}
>
@@ -296,10 +307,17 @@ export function getProbeColumns(
</>
)}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
copyToClipboard(bulkCopyContent)
}}
>
<CopyIcon className="me-2.5 size-4" />
<Trans>Bulk copy</Trans>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(event) => {
event.stopPropagation()
onClick={() => {
onDelete?.(actionRows)
}}
>

View File

@@ -34,18 +34,20 @@ import { useToast } from "@/components/ui/use-toast"
import { isReadOnlyUser } from "@/lib/api"
import { pb } from "@/lib/api"
import { $allSystemsById, $chartTime, $direction } from "@/lib/stores"
import { cn, useBrowserStorage } from "@/lib/utils"
import { cn, isVisuallyLonger, useBrowserStorage } from "@/lib/utils"
import type { NetworkProbeRecord } from "@/types"
import { AddProbeDialog, EditProbeDialog } from "./probe-dialog"
import { XIcon } from "lucide-react"
import { ArrowLeftRightIcon, EthernetPortIcon, GlobeIcon, ServerIcon, XIcon } from "lucide-react"
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "@/components/ui/sheet"
import ChartTimeSelect from "@/components/charts/chart-time-select"
import { ResponseChart, LossChart } from "@/components/routes/system/charts/probes-charts"
import { LossChart, AvgMinMaxResponseChart } from "@/components/routes/system/charts/probes-charts"
import { useNetworkProbeStats } from "@/lib/use-network-probes"
import { useStore } from "@nanostores/react"
import type { ChartData } from "@/types"
import { parseSemVer } from "@/lib/utils"
import { Separator } from "../ui/separator"
import { $router, Link } from "../router"
import { getPagePath } from "@nanostores/router"
export default function NetworkProbesTableNew({
systemId,
@@ -74,10 +76,10 @@ export default function NetworkProbesTableNew({
let longestTarget = ""
for (const p of probes) {
const name = p.name || p.target
if (name.length > longestName.length) {
if (isVisuallyLonger(name, longestName)) {
longestName = name
}
if (p.target.length > longestTarget.length) {
if (isVisuallyLonger(p.target, longestTarget)) {
longestTarget = p.target
}
}
@@ -266,7 +268,7 @@ export default function NetworkProbesTableNew({
)}
</div>
)}
{canManageProbes ? <AddProbeDialog systemId={systemId} /> : null}
{canManageProbes ? <AddProbeDialog systemId={systemId} probes={probes} /> : null}
{canManageProbes ? (
<EditProbeDialog
systemId={systemId}
@@ -488,7 +490,7 @@ function NetworkProbeSheetContent({
orientation: direction === "rtl" ? "right" : "left",
chartTime,
}),
[chartTime]
[probeStats]
)
const hasProbeStats = probeStats.some((record) => record.stats?.[probe.id] != null)
const probeLabel = probe.name || probe.target
@@ -499,14 +501,20 @@ function NetworkProbeSheetContent({
<SheetHeader className="mb-0 border-b p-0 pb-4">
<SheetTitle>{probeLabel}</SheetTitle>
<SheetDescription className="flex flex-wrap items-center gap-x-2 gap-y-1">
{system?.name ?? ""}
<ServerIcon className="size-3.5 text-muted-foreground" />
<Link className="hover:underline" href={getPagePath($router, "system", { id: system?.id ?? "" })}>
{system?.name ?? ""}
</Link>
<Separator orientation="vertical" className="h-2.5 bg-muted-foreground opacity-70" />
<ArrowLeftRightIcon className="size-3.5 text-muted-foreground" />
{probe.protocol.toUpperCase()}
<Separator orientation="vertical" className="h-2.5 bg-muted-foreground opacity-70" />
<GlobeIcon className="size-3.5 text-muted-foreground" />
{probe.target}
{probe.port > 0 && (
{probe.protocol === "tcp" && probe.port > 0 && (
<>
<Separator orientation="vertical" className="h-2.5 bg-muted-foreground opacity-70" />
<EthernetPortIcon className="size-3.5 text-muted-foreground" />
<span>{probe.port}</span>
</>
)}
@@ -514,14 +522,7 @@ function NetworkProbeSheetContent({
</SheetHeader>
<div className="grid gap-4">
<ChartTimeSelect className="bg-card" agentVersion={chartData.agentVersion} />
<ResponseChart
probeStats={probeStats}
grid={false}
probes={[probe]}
chartData={chartData}
empty={!hasProbeStats}
showFilter={false}
/>
<AvgMinMaxResponseChart probeStats={probeStats} probe={probe} chartData={chartData} empty={!hasProbeStats} />
<LossChart
probeStats={probeStats}
grid={false}

View File

@@ -17,7 +17,7 @@ import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { Textarea } from "@/components/ui/textarea"
import { ChevronDownIcon, ListIcon } from "lucide-react"
import { ChevronDownIcon, ListIcon, ServerIcon } from "lucide-react"
import { useToast } from "@/components/ui/use-toast"
import { $systems } from "@/lib/stores"
import type { NetworkProbeRecord } from "@/types"
@@ -38,6 +38,10 @@ type NormalizedProbeValues = Omit<ProbeValues, "system" | "interval"> & {
interval: number
}
type BulkProbeLineSource = Pick<NetworkProbeRecord, "target" | "protocol" | "port" | "interval" | "name">
const defaultInterval = 30
const ProbeProtocolSchema = v.picklist(["icmp", "tcp", "http"])
const ProbeIntervalSchema = v.pipe(v.string(), v.toNumber(), v.minValue(1), v.maxValue(3600))
@@ -54,15 +58,19 @@ const NormalizedProbeValuesSchema = v.pipe(
}),
v.transform((input): NormalizedProbeValues => {
let { protocol, port } = input
if (protocol === "icmp") {
let httpTarget = input.target
if (protocol === "icmp" || protocol === "http") {
if (protocol === "http") {
httpTarget = normalizeHttpTarget(input.target, port)
}
port = 0
} else if ((protocol === "tcp" || protocol === "http") && !port) {
} else if (protocol === "tcp" && !port) {
port = 443
}
return {
// HTTP probes may be entered as bare hostnames, so normalize them to a
// scheme-bearing URL before the payload is sent to PocketBase.
target: protocol === "http" ? normalizeHttpTarget(input.target, port) : input.target,
target: protocol === "http" ? httpTarget : input.target,
protocol,
port,
interval: input.interval,
@@ -71,7 +79,7 @@ const NormalizedProbeValuesSchema = v.pipe(
}),
v.forward(
v.check((input) => {
if (input.protocol === "icmp") {
if (input.protocol === "icmp" || input.protocol === "http") {
return input.port === 0
}
@@ -91,15 +99,42 @@ const BulkProbeSchema = v.object({
name: v.optional(v.pipe(v.string(), v.trim())),
})
function normalizeHttpTarget(target: string, port: number) {
if (/^https?:\/\//i.test(target)) {
function normalizeHttpTarget(target: string, port = 0) {
const useExplicitPort = port > 0 && port !== 80 && port !== 443
const hasOriginOnlyTarget = /^https?:\/\/[^/?#]+$/i.test(target)
if (!/^https?:\/\//i.test(target)) {
const scheme = port === 80 ? "http" : "https"
return `${scheme}://${target}${useExplicitPort ? `:${port}` : ""}`
}
let parsedUrl: URL
try {
parsedUrl = new URL(target)
} catch {
return target
}
return `${port === 443 ? "https" : "http"}://${target}`
if (!parsedUrl.port && useExplicitPort) {
parsedUrl.port = `${port}`
}
// avoid converting "http://localhost:8090" to "http://localhost:8090/" - keep the original formatting if the URL is just an origin
if (hasOriginOnlyTarget && parsedUrl.pathname === "/" && !parsedUrl.search && !parsedUrl.hash) {
return parsedUrl.origin
}
return parsedUrl.toString()
}
function buildProbePayload(values: ProbeValues) {
function trimTrailingEmptyFields(fields: string[]) {
let lastValueIndex = fields.length - 1
while (lastValueIndex > 0 && !fields[lastValueIndex]) {
lastValueIndex--
}
return fields.slice(0, lastValueIndex + 1)
}
function buildProbePayload(values: ProbeValues, enabled = true) {
const normalizedValues = v.safeParse(NormalizedProbeValuesSchema, values)
if (!normalizedValues.success) {
throw new Error(normalizedValues.issues[0]?.message || "Invalid probe")
@@ -107,7 +142,7 @@ function buildProbePayload(values: ProbeValues) {
const payload = {
system: values.system,
enabled: true,
enabled,
...normalizedValues.output,
}
@@ -123,6 +158,11 @@ function buildProbePayload(values: ProbeValues) {
return payload
}
type ProbeIdentity = Pick<ProbeValues, "system" | "target" | "protocol" | "port">
function getProbeIdentityKey({ system, target, protocol, port }: ProbeIdentity) {
return `${system}${target}${protocol}${port}`
}
function parseBulkProbeLine(line: string, lineNumber: number, system: string) {
const [rawTarget = "", rawProtocol = "", rawPort = "", rawInterval = "", ...rawName] = line.split(",")
const parsed = v.safeParse(BulkProbeSchema, {
@@ -135,19 +175,26 @@ function parseBulkProbeLine(line: string, lineNumber: number, system: string) {
if (!parsed.success) {
throw new Error(`Line ${lineNumber}: ${parsed.issues[0]?.message || "invalid probe entry"}`)
}
const protocol = (parsed.output.protocol?.toLowerCase() ||
(/^https?:\/\//i.test(parsed.output.target) ? "http" : "icmp")) as ProbeProtocol
return buildProbePayload({
system,
target: parsed.output.target,
protocol: (parsed.output.protocol?.toLowerCase() ||
(/^https?:\/\//i.test(parsed.output.target) ? "http" : "icmp")) as ProbeProtocol,
protocol,
port: parsed.output.port ? Number(parsed.output.port) : 0,
interval: parsed.output.interval || "30",
interval: parsed.output.interval || `${defaultInterval}`,
name: parsed.output.name || undefined,
})
}
export function AddProbeDialog({ systemId }: { systemId?: string }) {
export function formatBulkProbeLine(probe: BulkProbeLineSource) {
const port = probe.protocol !== "tcp" || probe.port === 443 ? "" : `${probe.port}`
const interval = probe.interval === defaultInterval ? "" : `${probe.interval}`
return trimTrailingEmptyFields([probe.target, probe.protocol, port, interval, probe.name?.trim() || ""]).join(",")
}
export function AddProbeDialog({ systemId, probes }: { systemId?: string; probes: NetworkProbeRecord[] }) {
const [open, setOpen] = useState(false)
const [bulkOpen, setBulkOpen] = useState(false)
const [bulkInput, setBulkInput] = useState("")
@@ -192,10 +239,29 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
}
const payloads = rawLines.map((line, index) => parseBulkProbeLine(line, index + 1, system))
const existingProbeKeys = new Set(
probes.filter((probe) => probe.system === system).map((probe) => getProbeIdentityKey(probe))
)
const newPayloads = [] as typeof payloads
for (const payload of payloads) {
const probeKey = getProbeIdentityKey(payload)
if (existingProbeKeys.has(probeKey)) {
continue
}
existingProbeKeys.add(probeKey)
newPayloads.push(payload)
}
if (!newPayloads.length) {
throw new Error("No new probes. All entries exist.")
}
closedForSubmit = true
let batch = pb.createBatch()
let inBatch = 0
for (const payload of payloads) {
for (const payload of newPayloads) {
batch.collection("network_probes").create(payload)
inBatch++
if (inBatch > 20) {
@@ -209,7 +275,7 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
}
resetBulkForm()
toast({ title: t`Probes created`, description: `${payloads.length} probe(s) added.` })
toast({ title: t`Probes created`, description: `${newPayloads.length} probe(s) added.` })
} catch (err: unknown) {
if (closedForSubmit) {
setBulkOpen(true)
@@ -265,19 +331,18 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
<SheetTitle>
<Trans>Bulk Add {{ foo: t`Network Probes` }}</Trans>
</SheetTitle>
<SheetDescription>
target[,protocol[,port[,interval[,name]]]] - TCP/HTTP default to port 443.
</SheetDescription>
<SheetDescription>target[,protocol[,port[,interval[,name]]]]</SheetDescription>
</SheetHeader>
<form ref={bulkFormRef} onSubmit={handleBulkSubmit} className="flex h-full flex-col overflow-hidden">
<div className="flex-1 space-y-4 overflow-auto p-4">
<div className="flex-1 flex flex-col space-y-4 overflow-auto p-4">
{!systemId && (
<div className="grid gap-2">
<Label>
<Label className="sr-only">
<Trans>System</Trans>
</Label>
<Select value={bulkSelectedSystemId} onValueChange={setBulkSelectedSystemId} required>
<SelectTrigger>
<SelectTrigger className="relative ps-10 pe-5 bg-card">
<ServerIcon className="size-3.5 absolute start-4 top-1/2 -translate-y-1/2 opacity-85" />
<SelectValue placeholder={t`Select a system`} />
</SelectTrigger>
<SelectContent>
@@ -290,7 +355,7 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
</Select>
</div>
)}
<div className="grid gap-2">
<div className="grow flex flex-col gap-2">
<Label htmlFor="bulk-probes" className="sr-only">
Entries
</Label>
@@ -304,14 +369,11 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
bulkFormRef.current?.requestSubmit()
}
}}
className="h-200 font-mono text-sm bg-muted/40"
style={{ maxHeight: `calc(100vh - 20rem)` }}
placeholder={["1.1.1.1", "example.com,tcp", "https://example.com,http,,60,Homepage"].join("\n")}
className="font-mono grow text-sm bg-card"
placeholder={["1.1.1.1", "example.com,tcp", "https://example.com,http,,60,Example"].join("\n")}
required
/>
<p className="text-xs text-muted-foreground">
target[,protocol[,port[,interval[,name]]]] TCP and HTTP default to port 443.
</p>
<p className="text-xs text-muted-foreground">target[,protocol[,port[,interval[,name]]]]</p>
</div>
</div>
<SheetFooter className="border-t">
@@ -337,10 +399,11 @@ export function EditProbeDialog({
systemId?: string
probe?: NetworkProbeRecord
}) {
if (!probe) {
const hasOpened = useRef(false)
if (!probe && !hasOpened.current) {
return null
}
hasOpened.current = true
return (
<Dialog open={open} onOpenChange={setOpen}>
<ProbeDialogContent open={open} setOpen={setOpen} systemId={systemId} probe={probe} />
@@ -363,10 +426,8 @@ function ProbeDialogContent({
}) {
const [protocol, setProtocol] = useState<ProbeProtocol>(probe?.protocol ?? "icmp")
const [target, setTarget] = useState(probe?.target ?? "")
const [port, setPort] = useState(
(probe?.protocol === "tcp" || probe?.protocol === "http") && probe.port ? String(probe.port) : ""
)
const [probeInterval, setProbeInterval] = useState(String(probe?.interval ?? 30))
const [port, setPort] = useState(probe?.protocol === "tcp" && probe.port ? String(probe.port) : "")
const [probeInterval, setProbeInterval] = useState(String(probe?.interval ?? defaultInterval))
const [name, setName] = useState(probe?.name ?? "")
const [loading, setLoading] = useState(false)
const [selectedSystemId, setSelectedSystemId] = useState(probe?.system ?? "")
@@ -384,8 +445,8 @@ function ProbeDialogContent({
setProtocol(probe?.protocol ?? "icmp")
setTarget(probe?.target ?? "")
setPort((probe?.protocol === "tcp" || probe?.protocol === "http") && probe.port ? String(probe.port) : "")
setProbeInterval(String(probe?.interval ?? 30))
setPort(probe?.protocol === "tcp" && probe.port ? String(probe.port) : "")
setProbeInterval(String(probe?.interval ?? defaultInterval))
setName(probe?.name ?? "")
setSelectedSystemId(probe?.system ?? "")
setLoading(false)
@@ -400,14 +461,17 @@ function ProbeDialogContent({
if (!selectedSystem) {
throw new Error("Select a system.")
}
const payload = buildProbePayload({
system: selectedSystem,
target,
protocol,
port: protocol === "tcp" || protocol === "http" ? Number(port) : 0,
interval: probeInterval,
name,
})
const payload = buildProbePayload(
{
system: selectedSystem,
target,
protocol,
port: protocol === "tcp" ? Number(port) : 0,
interval: probeInterval,
name,
},
probe ? probe.enabled : true
)
if (probe) {
await pb.collection("network_probes").update(probe.id, payload)
} else {
@@ -458,7 +522,7 @@ function ProbeDialogContent({
<Input
value={target}
onChange={(e) => setTarget(e.target.value)}
placeholder={protocol === "http" ? "https://example.com" : "1.1.1.1"}
placeholder={protocol === "http" ? "http://localhost:8090" : "1.1.1.1"}
required
/>
</div>
@@ -478,7 +542,7 @@ function ProbeDialogContent({
</SelectContent>
</Select>
</div>
{(protocol === "tcp" || protocol === "http") && (
{protocol === "tcp" && (
<div className="grid gap-2">
<Label>
<Trans>Port</Trans>
@@ -490,7 +554,6 @@ function ProbeDialogContent({
placeholder="443"
min={1}
max={65535}
required={protocol === "tcp"}
/>
</div>
)}

View File

@@ -81,7 +81,7 @@ function ProbeChart({
return probeStats.filter((record) => visibleKeys.some((id) => record.stats?.[id] != null))
}, [probeStats, visibleKeys])
const legend = dataPoints.length < 10 && dataPoints.length > 1
const legend = dataPoints.length < 10 && showFilter
return (
<ChartCard
@@ -132,27 +132,82 @@ export function ResponseChart({ probeStats, grid, probes, chartData, empty }: Pr
)
}
export function MaxResponseChart({ probeStats, grid, probes, chartData, empty }: ProbeChartProps) {
interface AvgMinMaxResponseChartProps {
probeStats: NetworkProbeStatsRecord[]
probe: NetworkProbeRecord | null
chartData: ChartData
empty: boolean
}
export function AvgMinMaxResponseChart({ probeStats, probe, chartData, empty }: AvgMinMaxResponseChartProps) {
const { t } = useLingui()
const { chartTime } = chartData
const hasLongInterval = (probe?.interval ?? 61) > 60
// only one probe is relevant for this chart
const dataPoints: DataPoint<NetworkProbeStatsRecord>[] = useMemo(() => {
const dataFn = (index: number) => (record: NetworkProbeStatsRecord) =>
record.stats?.[probe?.id ?? ""]?.[index] ?? "-"
const avgPoint = {
label: "Avg",
dataKey: dataFn(0),
color: 1,
order: 0,
}
if (chartTime === "1m" || (hasLongInterval && chartTime === "1h")) {
// avg, min, max are all the same for 1m interval, so just show avg
return [avgPoint]
}
return [
{
label: "Max",
dataKey: dataFn(2),
color: 3,
order: 0,
},
avgPoint,
{
label: "Min",
dataKey: dataFn(1),
color: 2,
order: 2,
},
]
}, [chartTime, hasLongInterval])
const data = useMemo(() => {
if (!probe) return []
return probeStats.filter((record) => record.stats && probe.id in record.stats)
}, [probe, probeStats])
const legend = dataPoints.length > 1
return (
<ProbeChart
probeStats={probeStats}
grid={grid}
probes={probes}
chartData={chartData}
<ChartCard
legend={true}
empty={empty}
valueIndex={0}
title={t`Response`}
description={t`Average response time`}
tickFormatter={(value) => formatMicroseconds(value, false)}
contentFormatter={({ value }) => {
if (typeof value !== "number") {
return value
}
return formatMicroseconds(value)
}}
/>
description={t`Average, minimum, and maximum response time`}
grid={false}
>
<LineChartDefault
truncate
chartData={chartData}
customData={data}
dataPoints={dataPoints}
domain={["auto", "auto"]}
connectNulls
legend={legend}
tickFormatter={(value) => formatMicroseconds(value, false)}
contentFormatter={({ value }) => {
if (typeof value !== "number") {
return value
}
return formatMicroseconds(value)
}}
/>
</ChartCard>
)
}
@@ -166,7 +221,7 @@ export function LossChart({ probeStats, grid, probes, chartData, empty }: ProbeC
probes={probes}
chartData={chartData}
empty={empty}
valueIndex={4}
valueIndex={3}
title={t`Loss`}
description={t`Packet loss (%)`}
domain={[0, 100]}

View File

@@ -9,7 +9,7 @@ import {
$pausedSystems,
$upSystems,
} from "@/lib/stores"
import { updateFavicon } from "@/lib/utils"
import { isVisuallyLonger, updateFavicon } from "@/lib/utils"
import type { SystemRecord } from "@/types"
import { SystemStatus } from "./enums"
@@ -41,7 +41,7 @@ export function init() {
}
if (!newSystem) {
onSystemsChanged(newSystems, undefined)
onSystemsChanged(newSystems, newSystem, oldSystem)
return
}
@@ -65,23 +65,28 @@ export function init() {
}
// run things that need to be done when systems change
onSystemsChanged(newSystems, newSystem)
onSystemsChanged(newSystems, newSystem, oldSystem)
})
}
/** Update the longest system name string and favicon based on system status */
function onSystemsChanged(systems: Record<string, SystemRecord>, _changedSystem: SystemRecord | undefined) {
function onSystemsChanged(systems: Record<string, SystemRecord>, newSystem?: SystemRecord, oldSystem?: SystemRecord) {
const downSystemsStore = $downSystems.get()
const downSystems = Object.values(downSystemsStore)
let longestName = ""
for (const system of Object.values(systems)) {
if (system.name.length > longestName.length) {
longestName = system.name
// if the old system's old name was the longest, we need to find the new longest name
// otherwise, if the changed system's new name is longer than the current longest, update it
const longestName = $longestSystemName.get()
if (oldSystem?.name === longestName && oldSystem.name !== newSystem?.name) {
let newLongest = ""
for (const id in systems) {
if (isVisuallyLonger(systems[id].name, newLongest)) {
newLongest = systems[id].name
}
}
}
if ($longestSystemName.get() !== longestName) {
$longestSystemName.set(longestName)
$longestSystemName.set(newLongest)
} else if (newSystem && newSystem.name !== longestName && isVisuallyLonger(newSystem.name, longestName)) {
$longestSystemName.set(newSystem.name)
}
updateFavicon(downSystems.length)

View File

@@ -116,63 +116,17 @@ export function useNetworkProbeStats(props: UseNetworkProbeStatsProps) {
const [probeStats, setProbeStats] = useState<NetworkProbeStatsRecord[]>([])
const requestID = useRef(0)
// Subscribe to new probe stats
useEffect(() => {
if (!systemId) {
setProbeStats([])
return
}
let unsubscribe: (() => void) | undefined
const pbOptions = {
fields: "stats,created,type",
filter: pb.filter("system = {:system}", { system: systemId }),
if (chartTime === "1m") {
setProbeStats(getCacheValue(systemId, "rt"))
return
}
;(async () => {
try {
unsubscribe = await pb.collection<NetworkProbeStatsRecord>("network_probe_stats").subscribe(
"*",
(event) => {
if (!chartTime || event.action !== "create") {
return
}
// if (typeof event.record.created === "string") {
// event.record.created = new Date(event.record.created).getTime()
// }
// return if not current chart time
// we could append to other chart times, but we would need to check the timestamps
// to make sure they fit in correctly, so for simplicity just ignore non-chart-time updates
// and fetch them via API when the user switches to that chart time
const chartTimeRecordType = chartTimeData[chartTime].type as ChartTimes
if (event.record.type !== chartTimeRecordType) {
// const lastCreated = getCacheValue(systemId, chartTime)?.at(-1)?.created ?? 0
// if (lastCreated) {
// // if the new record is close enough to the last cached record, append it to the cache so it's available immediately if the user switches to that chart time
// const { expectedInterval } = chartTimeData[chartTime]
// if (event.record.created - lastCreated < expectedInterval * 1.5) {
// console.log(
// `Caching out-of-chart-time probe stats record for chart time ${chartTime} (record type: ${event.record.type})`
// )
// const newStats = appendCacheValue(systemId, chartTime, [event.record])
// cache.set(`${systemId}${chartTime}`, newStats)
// }
// }
// console.log(`Received probe stats for non-current chart time (${event.record.type}), ignoring for now`)
return
}
// console.log("Appending new probe stats to chart:", event.record)
const newStats = appendCacheValue(systemId, chartTime, [event.record])
setProbeStats(newStats)
},
pbOptions
)
} catch (error) {
console.error("Failed to subscribe to probe stats:", error)
}
})()
return () => unsubscribe?.()
}, [systemId])
setProbeStats(getCacheValue(systemId, chartTime))
}, [systemId, chartTime])
// fetch missing probe stats on load and when chart time changes
useEffect(() => {
@@ -206,7 +160,40 @@ export function useNetworkProbeStats(props: UseNetworkProbeStatsProps) {
setProbeStats(newStats)
}
)
}, [chartTime])
}, [systemId, chartTime])
// Subscribe to new probe stats on non-1m chart times (1h, 12h, etc)
useEffect(() => {
if (!systemId || !chartTime || chartTime === "1m") {
return
}
let unsubscribe: (() => void) | undefined
const pbOptions = {
fields: "stats,created,type",
filter: pb.filter("system={:system} && type={:type}", { system: systemId, type: chartTimeData[chartTime].type }),
}
;(async () => {
try {
unsubscribe = await pb.collection<NetworkProbeStatsRecord>("network_probe_stats").subscribe(
"*",
(event) => {
if (event.action !== "create") {
return
}
// console.log("Appending new probe stats to chart:", event.record)
const newStats = appendCacheValue(systemId, chartTime, [event.record])
setProbeStats(newStats)
},
pbOptions
)
} catch (error) {
console.error("Failed to subscribe to probe stats:", error)
}
})()
return () => unsubscribe?.()
}, [systemId, chartTime])
// subscribe to realtime metrics if chart time is 1m
useEffect(() => {
@@ -241,20 +228,11 @@ export function useNetworkProbeStats(props: UseNetworkProbeStatsProps) {
return probeStats
}
// function probesToStats(probes: NetworkProbeRecord[]): NetworkProbeStatsRecord["stats"] {
// const stats: NetworkProbeStatsRecord["stats"] = {}
// for (const probe of probes) {
// // TODO: include only if probe.updated < charttime
// stats[probe.id] = [probe.res, probe.resAvg1h, probe.resMin1h, probe.resMax1h, probe.loss, probe.loss1h]
// }
// return stats
// }
async function fetchProbes(systemId?: string) {
async function fetchProbes(system?: string) {
try {
const res = await pb.collection<NetworkProbeRecord>("network_probes").getList(0, 2000, {
fields: NETWORK_PROBE_FIELDS,
filter: systemId ? pb.filter("system={:system}", { system: systemId }) : undefined,
filter: system ? pb.filter("system={:system}", { system }) : undefined,
})
return res.items
} catch (error) {

View File

@@ -443,6 +443,48 @@ export function runOnce<T extends (...args: any[]) => any>(fn: T): T {
}) as T
}
const visualWidthCache = new Map<string, number>()
/** Get the visual width of a string, accounting for full-width and narrow punctuation characters.
* Don't use for monospaced fonts, use .length instead
*/
function getVisualStringWidth(str: string): number {
const cached = visualWidthCache.get(str)
if (cached !== undefined) {
return cached
}
let width = 0
for (const char of str) {
if (char === ".") {
width += 0.7
continue
}
const code = char.codePointAt(0) || 0
// Hangul Jamo and Syllables are often slightly thinner than Hanzi/Kanji
if ((code >= 0x1100 && code <= 0x115f) || (code >= 0xac00 && code <= 0xd7af)) {
width += 1.8
continue
}
// Count CJK and other full-width characters as 2 units, others as 1
// Arabic and Cyrillic are counted as 1
const isFullWidth =
(code >= 0x2e80 && code <= 0x9fff) || // CJK Radicals, Symbols, and Ideographs
(code >= 0xf900 && code <= 0xfaff) || // CJK Compatibility Ideographs
(code >= 0xfe30 && code <= 0xfe6f) || // CJK Compatibility Forms
(code >= 0xff00 && code <= 0xff60) || // Fullwidth Forms
(code >= 0xffe0 && code <= 0xffe6) || // Fullwidth Symbols
code > 0xffff // Emojis and other supplementary plane characters
width += isFullWidth ? 2 : 1
}
visualWidthCache.set(str, width)
return width
}
/** Compare the visual width of two strings imprecisely */
export function isVisuallyLonger(str1: string, str2: string): boolean {
return getVisualStringWidth(str1) > getVisualStringWidth(str2)
}
/** Format seconds to hours, minutes, or seconds */
export function secondsToString(seconds: number, unit: "hour" | "minute" | "day"): string {
const count = Math.floor(seconds / (unit === "hour" ? 3600 : unit === "minute" ? 60 : 86400))

View File

@@ -564,23 +564,21 @@ export interface NetworkProbeRecord {
}
/**
* 0: avg 1 minute response in microseconds
* Stats holds only 1m values for a single target, which are used for charts.
*
* 1: avg response over 1 hour in microseconds
* 0: avg response in microseconds
*
* 2: min response over the last hour in microseconds
* 1: min response in microseconds
*
* 3: max response over the last hour in microseconds
* 2: max response in microseconds
*
* 4: packet loss %
*
* 5: packet loss over the last hour in %
* 3: packet loss percentage (0-100)
*/
type ProbeResult = number[]
type ProbeStats = number[]
export interface NetworkProbeStatsRecord {
id?: string
type?: string
stats: Record<string, ProbeResult>
stats: Record<string, ProbeStats>
created: number // unix timestamp (ms) for Recharts xAxis
}