Merge pull request #836 from prymitive/fix-counters

fix(api): ensure that counters add up to 100%
This commit is contained in:
Łukasz Mierzwa
2019-07-18 23:16:54 +01:00
committed by GitHub
2 changed files with 23 additions and 3 deletions

View File

@@ -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

View File

@@ -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 {