Skip to content

Commit

Permalink
🧹 Add batch stat APIs (#997)
Browse files Browse the repository at this point in the history
This allows me to add aggregated scores more efficiently to those
structs.
  • Loading branch information
jaym authored Dec 14, 2023
1 parent 7ba77fb commit 4a0a690
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
45 changes: 28 additions & 17 deletions policy/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,44 @@ func (r *Report) ComputeStats(resolved *ResolvedPolicy) {

r.Stats = &res
}

func (s *Stats) Add(score *Score) {
s.Total++
s.AddCount(score, 1)
}

func (s *Stats) AddCount(score *Score, count uint32) {
s.Total += count
switch score.Type {
case ScoreType_Unknown:
s.Unknown++
s.Unknown += count
case ScoreType_Result:
if score.Value < 100 {
s.Failed.Add(score)
s.Failed.AddCount(score, count)

if score.Value < s.Worst {
s.Worst = score.Value
}

} else {
s.Passed.Add(score)
s.Passed.AddCount(score, count)
}
case ScoreType_Error:
s.Errors.Add(score)
s.Errors.AddCount(score, count)
case ScoreType_Skip:
s.Skipped++
s.Skipped += count
case ScoreType_Unscored:
s.Unknown++
s.Unknown += count
default:
log.Warn().Uint32("type", score.Type).Str("id", score.QrId).Msg("ran into unknown score type")
}
}

// this function also handles nil scores and updates the score distribution accordingly
func (sd *ScoreDistribution) Add(score *Score) {
sd.AddRating(score.Rating())
sd.AddCount(score, 1)
}

func (sd *ScoreDistribution) AddCount(score *Score, count uint32) {
sd.AddRatingCount(score.Rating(), count)
}

// this function also handles nil scores and updates the score distribution accordingly
Expand All @@ -79,22 +86,26 @@ func (sd *ScoreDistribution) Remove(score *Score) {
}

func (sd *ScoreDistribution) AddRating(scoreRating ScoreRating) {
sd.Total++
sd.AddRatingCount(scoreRating, 1)
}

func (sd *ScoreDistribution) AddRatingCount(scoreRating ScoreRating, count uint32) {
sd.Total += count
switch scoreRating {
case ScoreRating_aPlus, ScoreRating_a, ScoreRating_aMinus:
sd.A++
sd.A += count
case ScoreRating_bPlus, ScoreRating_b, ScoreRating_bMinus:
sd.B++
sd.B += count
case ScoreRating_cPlus, ScoreRating_c, ScoreRating_cMinus:
sd.C++
sd.C += count
case ScoreRating_dPlus, ScoreRating_d, ScoreRating_dMinus:
sd.D++
sd.D += count
case ScoreRating_failed:
sd.F++
sd.F += count
case ScoreRating_error:
sd.Error++
sd.Error += count
case ScoreRating_unrated:
sd.Unrated++
sd.Unrated += count
}
}

Expand Down
17 changes: 17 additions & 0 deletions policy/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ func TestScoreDistributionAdd(t *testing.T) {
require.Equal(t, uint32(16), scoreDist.GetTotal())
}

func TestScoreDistributionAddCount(t *testing.T) {
scoreDist := &policy.ScoreDistribution{}
scoreDist.AddCount(&policy.Score{Value: 100, Type: policy.ScoreType_Result, ScoreCompletion: 100}, 3)
scoreDist.AddCount(&policy.Score{Value: 75, Type: policy.ScoreType_Result, ScoreCompletion: 100}, 4)
scoreDist.AddCount(&policy.Score{Value: 50, Type: policy.ScoreType_Result, ScoreCompletion: 100}, 5)
scoreDist.AddCount(&policy.Score{Type: policy.ScoreType_Error}, 6)
scoreDist.AddCount(&policy.Score{Type: policy.ScoreType_Unscored}, 7)

require.Equal(t, uint32(3), scoreDist.GetA())
require.Equal(t, uint32(4), scoreDist.GetB())
require.Equal(t, uint32(5), scoreDist.GetC())
require.Equal(t, uint32(6), scoreDist.GetError())
require.Equal(t, uint32(7), scoreDist.GetUnrated())

require.Equal(t, uint32(25), scoreDist.GetTotal())
}

func TestScoreDistributionRemove(t *testing.T) {
scoreDist := &policy.ScoreDistribution{
Total: 19,
Expand Down

0 comments on commit 4a0a690

Please sign in to comment.