diff --git a/internal/modules/production/uniformities/services/uniformity.calculate.go b/internal/modules/production/uniformities/services/uniformity.calculate.go index 1b99afa7..24eb107a 100644 --- a/internal/modules/production/uniformities/services/uniformity.calculate.go +++ b/internal/modules/production/uniformities/services/uniformity.calculate.go @@ -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) +} \ No newline at end of file diff --git a/internal/modules/production/uniformities/types/uniformity.types.go b/internal/modules/production/uniformities/types/uniformity.types.go index 17fcf305..877795f3 100644 --- a/internal/modules/production/uniformities/types/uniformity.types.go +++ b/internal/modules/production/uniformities/types/uniformity.types.go @@ -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"` -} +} \ No newline at end of file