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)
}
@@ -29,6 +29,8 @@ type UniformityChartRange struct {
MaxWeight float64 `json:"max_weight"`
BirdCount float64 `json:"bird_count"`
IsIdealRange bool `json:"is_ideal_range"`
IdealRange string `json:"ideal_range,omitempty"`
OutsideRange string `json:"outside_range,omitempty"`
}
type UniformityChartIdealRange struct {
@@ -82,4 +84,4 @@ type UniformityChartGauge struct {
type UniformityChartData struct {
BarChart UniformityChartBar `json:"bar_chart"`
GaugeChart UniformityChartGauge `json:"gauge_chart"`
}
}