send min/median connection score along with avg (#1720)

* send min/median connection score along with avg
* guard against divide by zero for avg score calculation
* update median calculation

Signed-off-by: shishir gowda <shishir@livekit.io>
This commit is contained in:
shishirng
2023-05-18 13:50:54 -04:00
committed by GitHub
parent 1d3faefc5e
commit 2e93d386fe
4 changed files with 72 additions and 31 deletions
+22 -5
View File
@@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
@@ -143,7 +144,9 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
}
// find aggregates across streams
score := float32(0.0)
scoreSum := float32(0.0) // used for average
minScore := float32(0.0) // min score in batched stats
var scores []float32 // used for median
maxRtt := uint32(0)
maxJitter := uint32(0)
coalescedVideoLayers := make(map[int32]*livekit.AnalyticsVideoLayer)
@@ -154,7 +157,15 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
continue
}
score += stat.Score
// only consider non-zero scores
if stat.Score > 0 {
if stat.Score < minScore {
minScore = stat.Score
}
scoreSum += stat.Score
scores = append(scores, stat.Score)
}
for _, analyticsStream := range stat.Streams {
if analyticsStream.Rtt > maxRtt {
maxRtt = analyticsStream.Rtt
@@ -201,10 +212,16 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
}
}
return &livekit.AnalyticsStat{
Score: score / float32(len(stats)),
Streams: []*livekit.AnalyticsStream{coalescedStream},
stat := &livekit.AnalyticsStat{
MinScore: minScore,
MedianScore: utils.MedianFloat32(scores),
Streams: []*livekit.AnalyticsStream{coalescedStream},
}
numScores := len(scores)
if numScores > 0 {
stat.Score = scoreSum / float32(numScores)
}
return stat
}
func isValid(stat *livekit.AnalyticsStat) bool {
+22
View File
@@ -0,0 +1,22 @@
package utils
import "sort"
// MedianFloat32 gets median value for an array of float32
func MedianFloat32(input []float32) float32 {
num := len(input)
if num == 0 {
return 0
} else if num == 1 {
return input[0]
}
sort.Slice(input, func(i, j int) bool {
return input[i] < input[j]
})
if num%2 != 0 {
return input[num/2]
}
left := input[num/2-1]
right := input[num/2]
return (left + right) / 2
}