mirror of
https://github.com/prymitive/karma
synced 2026-05-09 03:36:44 +00:00
Merge pull request #836 from prymitive/fix-counters
fix(api): ensure that counters add up to 100%
This commit is contained in:
12
alerts.go
12
alerts.go
@@ -57,11 +57,21 @@ func countersToLabelStats(counters map[string]map[string]int) models.LabelNameSt
|
||||
}
|
||||
|
||||
// now that we have total hits we can calculate %
|
||||
var totalPercent int
|
||||
for i, value := range nameStats.Values {
|
||||
nameStats.Values[i].Percent = int(math.Round((float64(value.Hits) / float64(nameStats.Hits)) * 100.0))
|
||||
totalPercent += nameStats.Values[i].Percent
|
||||
}
|
||||
|
||||
sort.Sort(nameStats.Values)
|
||||
for totalPercent < 100 {
|
||||
for i := range nameStats.Values {
|
||||
nameStats.Values[i].Percent++
|
||||
totalPercent++
|
||||
if totalPercent >= 100 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now that we have all % and values are sorted we can calculate offsets
|
||||
offset := 0
|
||||
|
||||
14
api_test.go
14
api_test.go
@@ -739,7 +739,7 @@ var countsMap = models.LabelNameStatsList{
|
||||
models.LabelValueStats{
|
||||
Value: "server2",
|
||||
Hits: 4,
|
||||
Percent: 17,
|
||||
Percent: 18,
|
||||
},
|
||||
models.LabelValueStats{
|
||||
Value: "server3",
|
||||
@@ -754,7 +754,7 @@ var countsMap = models.LabelNameStatsList{
|
||||
models.LabelValueStats{
|
||||
Value: "server5",
|
||||
Hits: 4,
|
||||
Percent: 17,
|
||||
Percent: 18,
|
||||
},
|
||||
models.LabelValueStats{
|
||||
Value: "server6",
|
||||
@@ -990,6 +990,16 @@ func TestVerifyAllGroups(t *testing.T) {
|
||||
t.Errorf("[%s] Silences mismatch, expected >0 but got %d", version, len(am))
|
||||
}
|
||||
|
||||
for _, nameStats := range ur.Counters {
|
||||
var totalPercent int
|
||||
for _, valueStats := range nameStats.Values {
|
||||
totalPercent += valueStats.Percent
|
||||
}
|
||||
if totalPercent != 100 {
|
||||
t.Errorf("[%s] Counters %s sum is != 100: %d", version, nameStats.Name, totalPercent)
|
||||
}
|
||||
}
|
||||
|
||||
for _, expectedNameStats := range countsMap {
|
||||
var foundName bool
|
||||
for _, nameStats := range ur.Counters {
|
||||
|
||||
Reference in New Issue
Block a user