mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-21 13:55:43 +00:00
feat(BE):change standart egg in fcr master data
This commit is contained in:
@@ -507,37 +507,180 @@ func (s dashboardService) buildComparisonChartsAll(ctx context.Context, startDat
|
||||
stdFcr[row.Week] = row.StdFcr
|
||||
}
|
||||
|
||||
layingWeeks, layingActual := mapComparisonWeeklyMetricRows(layingRows)
|
||||
eggWeightWeeks, eggWeightActual := mapComparisonWeeklyMetricRows(eggWeightRows)
|
||||
feedWeeks, feedActual := mapComparisonWeeklyMetricRows(feedIntakeRows)
|
||||
fcrWeeks, fcrActual := mapComparisonWeeklyMetricRows(fcrRows)
|
||||
deplesiWeeks, deplesiActual := mapComparisonWeeklyMetricRows(deplesiRows)
|
||||
bodyWeightWeeks, bodyWeightActual, uniformityWeeks, uniformityActual := mapComparisonUniformityRows(uniformityRows)
|
||||
_, layingActual := mapComparisonWeeklyMetricRows(layingRows)
|
||||
_, eggWeightActual := mapComparisonWeeklyMetricRows(eggWeightRows)
|
||||
_, feedActual := mapComparisonWeeklyMetricRows(feedIntakeRows)
|
||||
_, fcrActual := mapComparisonWeeklyMetricRows(fcrRows)
|
||||
_, deplesiActual := mapComparisonWeeklyMetricRows(deplesiRows)
|
||||
_, bodyWeightActual, _, uniformityActual := mapComparisonUniformityRows(uniformityRows)
|
||||
|
||||
charts := map[string]dto.DashboardChartDTO{}
|
||||
if len(bodyWeightWeeks) > 0 {
|
||||
charts["body_weight"] = buildComparisonPercentChart(seriesRows, bodyWeightWeeks, bodyWeightActual, stdBodyWeight)
|
||||
}
|
||||
if len(layingWeeks) > 0 {
|
||||
charts["laying"] = buildComparisonPercentChart(seriesRows, layingWeeks, layingActual, stdLaying)
|
||||
}
|
||||
if len(eggWeightWeeks) > 0 {
|
||||
charts["egg_weight"] = buildComparisonPercentChart(seriesRows, eggWeightWeeks, eggWeightActual, stdEggWeight)
|
||||
}
|
||||
if len(feedWeeks) > 0 {
|
||||
charts["feed_intake"] = buildComparisonPercentChart(seriesRows, feedWeeks, feedActual, stdFeedIntake)
|
||||
}
|
||||
if len(uniformityWeeks) > 0 {
|
||||
charts["uniformity"] = buildComparisonPercentChart(seriesRows, uniformityWeeks, uniformityActual, stdUniformity)
|
||||
}
|
||||
if len(fcrWeeks) > 0 {
|
||||
charts["fcr"] = buildComparisonPercentChart(seriesRows, fcrWeeks, fcrActual, stdFcr)
|
||||
}
|
||||
if len(deplesiWeeks) > 0 {
|
||||
charts["deplesi"] = buildComparisonPercentChart(seriesRows, deplesiWeeks, deplesiActual, stdDeplesi)
|
||||
aggregateActual := buildAggregateComparisonPercent(weeks, seriesRows, aggregateComparisonInput{
|
||||
BodyWeightActual: bodyWeightActual,
|
||||
LayingActual: layingActual,
|
||||
EggWeightActual: eggWeightActual,
|
||||
FeedIntakeActual: feedActual,
|
||||
UniformityActual: uniformityActual,
|
||||
FcrActual: fcrActual,
|
||||
DeplesiActual: deplesiActual,
|
||||
StdBodyWeight: stdBodyWeight,
|
||||
StdLaying: stdLaying,
|
||||
StdEggWeight: stdEggWeight,
|
||||
StdFeedIntake: stdFeedIntake,
|
||||
StdUniformity: stdUniformity,
|
||||
StdFcr: stdFcr,
|
||||
StdDeplesi: stdDeplesi,
|
||||
})
|
||||
|
||||
if len(aggregateActual) == 0 {
|
||||
return map[string]dto.DashboardChartDTO{}, nil
|
||||
}
|
||||
|
||||
return charts, nil
|
||||
chartKey := strings.ToLower(params.ComparisonType)
|
||||
return map[string]dto.DashboardChartDTO{
|
||||
chartKey: buildComparisonAggregateChart(seriesRows, weeks, aggregateActual),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type aggregateComparisonInput struct {
|
||||
BodyWeightActual map[int]map[uint]float64
|
||||
LayingActual map[int]map[uint]float64
|
||||
EggWeightActual map[int]map[uint]float64
|
||||
FeedIntakeActual map[int]map[uint]float64
|
||||
UniformityActual map[int]map[uint]float64
|
||||
FcrActual map[int]map[uint]float64
|
||||
DeplesiActual map[int]map[uint]float64
|
||||
StdBodyWeight map[int]float64
|
||||
StdLaying map[int]float64
|
||||
StdEggWeight map[int]float64
|
||||
StdFeedIntake map[int]float64
|
||||
StdUniformity map[int]float64
|
||||
StdFcr map[int]float64
|
||||
StdDeplesi map[int]float64
|
||||
}
|
||||
|
||||
func buildAggregateComparisonPercent(weeks []int, seriesRows []repository.ComparisonSeries, input aggregateComparisonInput) map[int]map[uint]float64 {
|
||||
result := map[int]map[uint]float64{}
|
||||
|
||||
for _, week := range weeks {
|
||||
stdBodyWeight := input.StdBodyWeight[week]
|
||||
stdLaying := input.StdLaying[week]
|
||||
stdEggWeight := input.StdEggWeight[week]
|
||||
stdFeedIntake := input.StdFeedIntake[week]
|
||||
stdUniformity := input.StdUniformity[week]
|
||||
stdFcr := input.StdFcr[week]
|
||||
stdDeplesi := input.StdDeplesi[week]
|
||||
|
||||
for _, series := range seriesRows {
|
||||
sum := 0.0
|
||||
count := 0.0
|
||||
|
||||
if percent, ok := higherIsBetterPercent(input.LayingActual, week, series.Id, stdLaying); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := higherIsBetterPercent(input.EggWeightActual, week, series.Id, stdEggWeight); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := higherIsBetterPercent(input.UniformityActual, week, series.Id, stdUniformity); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := lowerIsBetterPercent(input.FcrActual, week, series.Id, stdFcr); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := lowerIsBetterPercent(input.DeplesiActual, week, series.Id, stdDeplesi); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := higherIsBetterPercent(input.BodyWeightActual, week, series.Id, stdBodyWeight); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
if percent, ok := lowerIsBetterPercent(input.FeedIntakeActual, week, series.Id, stdFeedIntake); ok {
|
||||
sum += percent
|
||||
count++
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if result[week] == nil {
|
||||
result[week] = map[uint]float64{}
|
||||
}
|
||||
result[week][series.Id] = sum / count
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func higherIsBetterPercent(actual map[int]map[uint]float64, week int, seriesId uint, standard float64) (float64, bool) {
|
||||
if standard <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
val, ok := metricValue(actual, week, seriesId)
|
||||
if !ok || val <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return (val / standard) * 100, true
|
||||
}
|
||||
|
||||
func lowerIsBetterPercent(actual map[int]map[uint]float64, week int, seriesId uint, standard float64) (float64, bool) {
|
||||
if standard <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
val, ok := metricValue(actual, week, seriesId)
|
||||
if !ok || val <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return (standard / val) * 100, true
|
||||
}
|
||||
|
||||
func metricValue(actual map[int]map[uint]float64, week int, seriesId uint) (float64, bool) {
|
||||
weekRows, ok := actual[week]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
val, ok := weekRows[seriesId]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
func buildComparisonAggregateChart(seriesRows []repository.ComparisonSeries, weeks []int, actual map[int]map[uint]float64) dto.DashboardChartDTO {
|
||||
series := make([]dto.DashboardChartSeriesDTO, 0, len(seriesRows))
|
||||
for _, sRow := range seriesRows {
|
||||
series = append(series, dto.DashboardChartSeriesDTO{
|
||||
Id: strconv.FormatUint(uint64(sRow.Id), 10),
|
||||
Label: sRow.Label,
|
||||
Unit: "%",
|
||||
})
|
||||
}
|
||||
|
||||
dataset := make([]map[string]interface{}, 0, len(weeks))
|
||||
for _, week := range weeks {
|
||||
row := map[string]interface{}{
|
||||
"week": week,
|
||||
}
|
||||
values, ok := actual[week]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, sRow := range seriesRows {
|
||||
if val, exists := values[sRow.Id]; exists {
|
||||
row[strconv.FormatUint(uint64(sRow.Id), 10)] = roundTo(val, 2)
|
||||
}
|
||||
}
|
||||
if len(row) > 1 {
|
||||
dataset = append(dataset, row)
|
||||
}
|
||||
}
|
||||
|
||||
return dto.DashboardChartDTO{
|
||||
Series: series,
|
||||
Dataset: dataset,
|
||||
}
|
||||
}
|
||||
|
||||
func (s dashboardService) standardComparisonMap(ctx context.Context, weeks []int, metric string, filter *validation.DashboardFilter) (map[int]float64, error) {
|
||||
|
||||
Reference in New Issue
Block a user