-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.go
More file actions
69 lines (62 loc) · 1.7 KB
/
Copy pathdetect.go
File metadata and controls
69 lines (62 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"sort"
"time"
)
// Detection in pingping 2.0 serves exactly one master: the ◆ marks on the chart.
// No alerting, no state machine, no thresholds to tune. robust z-score
// (median + MAD) flags loss bursts; the chart shows them; humans decide.
type Detector struct {
store *Store
}
func NewDetector(store *Store) *Detector { return &Detector{store: store} }
const burstZ = 3.5 // Iglewicz-Hoaglin conventional cutoff
// CheckBurst returns the verdict and the z value (hover-tooltip evidence).
func (d *Detector) CheckBurst(name string, r Round) (bool, float64) {
loss := r.S - r.R
if loss < 2 { // a single lost packet on the internet is noise
return false, 0
}
hist := d.store.Recent(name, time.Now().Add(-4*time.Hour).Unix())
if len(hist) < 30 {
return float64(loss)/float64(r.S) >= 0.25, 0 // cold start: absolute floor
}
series := make([]float64, len(hist))
for i, h := range hist {
series[i] = float64(h.S - h.R)
}
z := robustZ(float64(loss), series)
if z < 0 { // MAD=0: the healthy all-zero-loss baseline
return float64(loss)/float64(r.S) >= 0.10, 0
}
return z >= burstZ, float64(int(z*100)) / 100
}
// robustZ = 0.6745*(x-median)/MAD; returns -1 when MAD==0 so callers fall back.
func robustZ(x float64, series []float64) float64 {
med := median(series)
dev := make([]float64, len(series))
for i, v := range series {
if v > med {
dev[i] = v - med
} else {
dev[i] = med - v
}
}
mad := median(dev)
if mad == 0 {
return -1
}
return 0.6745 * (x - med) / mad
}
func median(s []float64) float64 {
if len(s) == 0 {
return 0
}
c := append([]float64(nil), s...)
sort.Float64s(c)
n := len(c)
if n%2 == 1 {
return c[n/2]
}
return (c[n/2-1] + c[n/2]) / 2
}