diff --git a/internal/modules/dashboards/services/dashboard.service.go b/internal/modules/dashboards/services/dashboard.service.go index a60f1555..8fa0a2c9 100644 --- a/internal/modules/dashboards/services/dashboard.service.go +++ b/internal/modules/dashboards/services/dashboard.service.go @@ -124,48 +124,35 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params } } - hppPercent := 0.0 - if hppLast > 0 { - hppPercent = (hppCurrent - hppLast) / hppLast * 100 - } - - sellingPercent := 0.0 - if sellingLast > 0 { - sellingPercent = sellingCurrent / sellingLast * 100 - } + hppPercent := percentDelta(hppCurrent, hppLast) + sellingPercent := percentDelta(sellingCurrent, sellingLast) stats := []dto.DashboardStatisticsDTO{ { Label: "HPP Global", Value: roundTo(hppCurrent, 0), - PercentLastMonth: hppPercent, + PercentLastMonth: roundTo(hppPercent*100, 2), }, { Label: "Avg. Selling Price", Value: roundTo(sellingCurrent, 0), - PercentLastMonth: sellingPercent, + PercentLastMonth: roundTo(sellingPercent*100, 2), }, } if hasFilter { - fcrPercent := 0.0 - if fcrLast > 0 { - fcrPercent = (fcrCurrent - fcrLast) / fcrLast * 100 - } - mortalityPercent := 0.0 - if mortalityLast > 0 { - mortalityPercent = (mortalityCurrent - mortalityLast) / mortalityLast * 100 - } + fcrPercent := percentDelta(fcrCurrent, fcrLast) + mortalityPercent := percentDelta(mortalityCurrent, mortalityLast) stats = append(stats, dto.DashboardStatisticsDTO{ Label: "FCR", Value: roundTo(fcrCurrent, 2), - PercentLastMonth: fcrPercent, + PercentLastMonth: roundTo(fcrPercent*100, 2), }, dto.DashboardStatisticsDTO{ Label: "Mortality", Value: roundTo(mortalityCurrent, 2), - PercentLastMonth: mortalityPercent, + PercentLastMonth: roundTo(mortalityPercent*100, 2), }, ) } @@ -849,6 +836,13 @@ func buildComparisonPercentChart(seriesRows []repository.ComparisonSeries, weeks } } +func percentDelta(current, last float64) float64 { + if last <= 0 { + return 0 + } + return (current - last) / last +} + func (s dashboardService) calculateHppGlobal(ctx context.Context, filter *validation.DashboardFilter, startDate, endExclusive, endDate time.Time, location *time.Location) (float64, float64, error) { totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, filter) if err != nil {