adjustment meta

This commit is contained in:
ragilap
2026-01-11 18:54:05 +07:00
parent a54129866e
commit dc7dc0ba47
4 changed files with 72 additions and 44 deletions
@@ -61,9 +61,9 @@ func (u *DashboardController) GetAll(c *fiber.Ctx) error {
return ids, nil return ids, nil
} }
lokasiIds, err := parseUintListParam(c.Query("lokasi_ids", "")) lokasiIds, err := parseUintListParam(c.Query("location_ids", ""))
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid lokasi_ids") return fiber.NewError(fiber.StatusBadRequest, "Invalid location_ids")
} }
flockIds, err := parseUintListParam(c.Query("flock_ids", "")) flockIds, err := parseUintListParam(c.Query("flock_ids", ""))
@@ -128,7 +128,19 @@ func (u *DashboardController) GetAll(c *fiber.Ctx) error {
return err return err
} }
filters := dto.DashboardFiltersDTO{ hasFilter := query.StartDate != "" ||
query.EndDate != "" ||
len(query.LokasiIds) > 0 ||
len(query.FlockIds) > 0 ||
len(query.KandangIds) > 0 ||
len(query.Include) > 0 ||
query.ComparisonType != "" ||
query.Metric != "" ||
query.AnalysisMode != validation.AnalysisModeOverview
var filters interface{}
if hasFilter {
filters = dto.DashboardFiltersDTO{
StartDate: query.StartDate, StartDate: query.StartDate,
EndDate: query.EndDate, EndDate: query.EndDate,
AnalysisMode: query.AnalysisMode, AnalysisMode: query.AnalysisMode,
@@ -139,6 +151,7 @@ func (u *DashboardController) GetAll(c *fiber.Ctx) error {
KandangIds: defaultUintSlice(query.KandangIds), KandangIds: defaultUintSlice(query.KandangIds),
Include: query.Include, Include: query.Include,
} }
}
return c.Status(fiber.StatusOK). return c.Status(fiber.StatusOK).
JSON(response.SuccessWithMeta{ JSON(response.SuccessWithMeta{
@@ -27,7 +27,7 @@ type DashboardFiltersDTO struct {
AnalysisMode string `json:"analysis_mode"` AnalysisMode string `json:"analysis_mode"`
ComparisonType string `json:"comparison_type,omitempty"` ComparisonType string `json:"comparison_type,omitempty"`
Metric string `json:"metric,omitempty"` Metric string `json:"metric,omitempty"`
LokasiIds []uint `json:"lokasi_ids"` LokasiIds []uint `json:"location_ids"`
FlockIds []uint `json:"flock_ids"` FlockIds []uint `json:"flock_ids"`
KandangIds []uint `json:"kandang_ids"` KandangIds []uint `json:"kandang_ids"`
Include []string `json:"include,omitempty"` Include []string `json:"include,omitempty"`
@@ -108,15 +108,21 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params
return nil, err return nil, err
} }
fcrCurrent, fcrLast, err := s.calculateFcr(ctx, filter, startDate, endExclusive, endDate, location) hasFilter := filter != nil && (len(filter.LokasiIds) > 0 || len(filter.FlockIds) > 0 || len(filter.KandangIds) > 0)
fcrCurrent := 0.0
fcrLast := 0.0
mortalityCurrent := 0.0
mortalityLast := 0.0
if hasFilter {
fcrCurrent, fcrLast, err = s.calculateFcr(ctx, filter, startDate, endExclusive, endDate, location)
if err != nil { if err != nil {
return nil, err return nil, err
} }
mortalityCurrent, mortalityLast, err = s.calculateMortality(ctx, filter, startDate, endExclusive, endDate, location)
mortalityCurrent, mortalityLast, err := s.calculateMortality(ctx, filter, startDate, endExclusive, endDate, location)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
hppPercent := 0.0 hppPercent := 0.0
if hppLast > 0 { if hppLast > 0 {
@@ -128,17 +134,7 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params
sellingPercent = sellingCurrent / sellingLast * 100 sellingPercent = sellingCurrent / sellingLast * 100
} }
fcrPercent := 0.0 stats := []dto.DashboardStatisticsDTO{
if fcrLast > 0 {
fcrPercent = (fcrCurrent - fcrLast) / fcrLast * 100
}
mortalityPercent := 0.0
if mortalityLast > 0 {
mortalityPercent = (mortalityCurrent - mortalityLast) / mortalityLast * 100
}
return []dto.DashboardStatisticsDTO{
{ {
Label: "HPP Global", Label: "HPP Global",
Value: roundTo(hppCurrent, 0), Value: roundTo(hppCurrent, 0),
@@ -149,17 +145,32 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params
Value: roundTo(sellingCurrent, 0), Value: roundTo(sellingCurrent, 0),
PercentLastMonth: sellingPercent, PercentLastMonth: sellingPercent,
}, },
{ }
if hasFilter {
fcrPercent := 0.0
if fcrLast > 0 {
fcrPercent = (fcrCurrent - fcrLast) / fcrLast * 100
}
mortalityPercent := 0.0
if mortalityLast > 0 {
mortalityPercent = (mortalityCurrent - mortalityLast) / mortalityLast * 100
}
stats = append(stats,
dto.DashboardStatisticsDTO{
Label: "FCR", Label: "FCR",
Value: roundTo(fcrCurrent, 2), Value: roundTo(fcrCurrent, 2),
PercentLastMonth: fcrPercent, PercentLastMonth: fcrPercent,
}, },
{ dto.DashboardStatisticsDTO{
Label: "Mortality", Label: "Mortality",
Value: roundTo(mortalityCurrent, 2), Value: roundTo(mortalityCurrent, 2),
PercentLastMonth: mortalityPercent, PercentLastMonth: mortalityPercent,
}, },
}, nil )
}
return stats, nil
} }
func (s dashboardService) buildPerformanceCharts(ctx context.Context, params *validation.Query, filter *validation.DashboardFilter) (map[string]dto.DashboardChartDTO, error) { func (s dashboardService) buildPerformanceCharts(ctx context.Context, params *validation.Query, filter *validation.DashboardFilter) (map[string]dto.DashboardChartDTO, error) {
@@ -171,6 +182,10 @@ func (s dashboardService) buildPerformanceCharts(ctx context.Context, params *va
return nil, errors.New("period dates are not initialized") return nil, errors.New("period dates are not initialized")
} }
if filter == nil || (len(filter.LokasiIds) == 0 && len(filter.FlockIds) == 0 && len(filter.KandangIds) == 0) {
return map[string]dto.DashboardChartDTO{}, nil
}
startDate := params.PeriodStart startDate := params.PeriodStart
endExclusive := params.PeriodEndExclusive endExclusive := params.PeriodEndExclusive
@@ -41,7 +41,7 @@ type PerformanceOverviewFilter struct {
AnalysisMode string `query:"analysis_mode" validate:"omitempty,oneof=OVERVIEW COMPARASION"` AnalysisMode string `query:"analysis_mode" validate:"omitempty,oneof=OVERVIEW COMPARASION"`
ComparisonType string `query:"comparison_type" validate:"omitempty,oneof=FARM FLOCK KANDANG"` ComparisonType string `query:"comparison_type" validate:"omitempty,oneof=FARM FLOCK KANDANG"`
Metric string `query:"metric" validate:"omitempty,oneof=fcr mortality laying egg_weight feed_intake"` Metric string `query:"metric" validate:"omitempty,oneof=fcr mortality laying egg_weight feed_intake"`
LokasiIds []uint `query:"lokasi_ids" validate:"omitempty,dive,gt=0"` LokasiIds []uint `query:"location_ids" validate:"omitempty,dive,gt=0"`
FlockIds []uint `query:"flock_ids" validate:"omitempty,dive,gt=0"` FlockIds []uint `query:"flock_ids" validate:"omitempty,dive,gt=0"`
KandangIds []uint `query:"kandang_ids" validate:"omitempty,dive,gt=0"` KandangIds []uint `query:"kandang_ids" validate:"omitempty,dive,gt=0"`
Include []string `query:"include" validate:"omitempty,dive,oneof=statistics charts"` Include []string `query:"include" validate:"omitempty,dive,oneof=statistics charts"`