package datascience import "sort" // MetricsDrift calculates the relative drift of a current measurement against // a historical baseline computed at the given percentile. // // historical is a window of past measurements (e.g. duration_ms, bytes). // percentile selects the baseline: 0.5 = median, 0.95 = p95. // drift = (current - baseline) / baseline, e.g. 0.47 means +47% above baseline. // // Returns drift=0, baseline=0 when historical is empty or baseline is zero. func MetricsDrift(historical []int64, current int64, percentile float64) (drift float64, baseline int64) { if len(historical) == 0 { return 0, 0 } sorted := make([]int64, len(historical)) copy(sorted, historical) sort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] }) baseline = Percentile(sorted, percentile) if baseline == 0 { return 0, 0 } drift = float64(current-baseline) / float64(baseline) return drift, baseline }