feat(BE-281): add types uniformity

This commit is contained in:
ragilap
2026-01-08 12:40:36 +07:00
parent 8a64300ddd
commit 6c08fe23ca
2 changed files with 44 additions and 2 deletions
@@ -346,17 +346,31 @@ func buildChartWeekSummary(weights []float64) utypes.UniformityChartWeek {
minBucket := bucket
maxBucket := bucket + bucketSize - 1
count := 0.0
bucketWeights := make([]float64, 0)
idealWeights := make([]float64, 0)
outsideWeights := make([]float64, 0)
for _, w := range weights {
if w >= minBucket && w < minBucket+bucketSize {
count++
bucketWeights = append(bucketWeights, w)
if w >= idealMin && w <= idealMax {
idealWeights = append(idealWeights, w)
} else {
outsideWeights = append(outsideWeights, w)
}
}
}
idealRangeLabel := rangeFromValues(idealWeights)
outsideRangeLabel := rangeFromValues(outsideWeights)
isIdealRange := idealRangeLabel != ""
distribution = append(distribution, utypes.UniformityChartRange{
Range: fmt.Sprintf("%d-%d", int(minBucket), int(maxBucket)),
MinWeight: minBucket,
MaxWeight: maxBucket,
BirdCount: count,
IsIdealRange: minBucket >= idealMin && maxBucket <= idealMax,
IsIdealRange: isIdealRange,
IdealRange: idealRangeLabel,
OutsideRange: outsideRangeLabel,
})
}
@@ -391,3 +405,29 @@ func roundToPrecision(value float64, precision int) float64 {
}
return math.Floor(scaled) / scale
}
func rangeFromValues(values []float64) string {
if len(values) == 0 {
return ""
}
minValue := values[0]
maxValue := values[0]
for _, v := range values[1:] {
if v < minValue {
minValue = v
}
if v > maxValue {
maxValue = v
}
}
return formatRange(minValue, maxValue)
}
func formatRange(minValue, maxValue float64) string {
minInt := int(math.Round(minValue))
maxInt := int(math.Round(maxValue))
if minInt == maxInt {
return fmt.Sprintf("%d", minInt)
}
return fmt.Sprintf("%d-%d", minInt, maxInt)
}