[FIX/BE] add percent rasio in statistic dashboard

This commit is contained in:
ragilap
2026-01-12 01:15:31 +07:00
parent d1d94357cf
commit c15ff8a211
@@ -124,48 +124,35 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params
} }
} }
hppPercent := 0.0 hppPercent := percentDelta(hppCurrent, hppLast)
if hppLast > 0 { sellingPercent := percentDelta(sellingCurrent, sellingLast)
hppPercent = (hppCurrent - hppLast) / hppLast * 100
}
sellingPercent := 0.0
if sellingLast > 0 {
sellingPercent = sellingCurrent / sellingLast * 100
}
stats := []dto.DashboardStatisticsDTO{ stats := []dto.DashboardStatisticsDTO{
{ {
Label: "HPP Global", Label: "HPP Global",
Value: roundTo(hppCurrent, 0), Value: roundTo(hppCurrent, 0),
PercentLastMonth: hppPercent, PercentLastMonth: roundTo(hppPercent*100, 2),
}, },
{ {
Label: "Avg. Selling Price", Label: "Avg. Selling Price",
Value: roundTo(sellingCurrent, 0), Value: roundTo(sellingCurrent, 0),
PercentLastMonth: sellingPercent, PercentLastMonth: roundTo(sellingPercent*100, 2),
}, },
} }
if hasFilter { if hasFilter {
fcrPercent := 0.0 fcrPercent := percentDelta(fcrCurrent, fcrLast)
if fcrLast > 0 { mortalityPercent := percentDelta(mortalityCurrent, mortalityLast)
fcrPercent = (fcrCurrent - fcrLast) / fcrLast * 100
}
mortalityPercent := 0.0
if mortalityLast > 0 {
mortalityPercent = (mortalityCurrent - mortalityLast) / mortalityLast * 100
}
stats = append(stats, stats = append(stats,
dto.DashboardStatisticsDTO{ dto.DashboardStatisticsDTO{
Label: "FCR", Label: "FCR",
Value: roundTo(fcrCurrent, 2), Value: roundTo(fcrCurrent, 2),
PercentLastMonth: fcrPercent, PercentLastMonth: roundTo(fcrPercent*100, 2),
}, },
dto.DashboardStatisticsDTO{ dto.DashboardStatisticsDTO{
Label: "Mortality", Label: "Mortality",
Value: roundTo(mortalityCurrent, 2), 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) { 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) totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, filter)
if err != nil { if err != nil {