fix: sort.
This commit is contained in:
@ -751,12 +751,12 @@ func (tc *StepTrainingController) GetTrainingRank(c *gin.Context) {
|
|||||||
query := tc.DB.Model(&models.RegressionResult{})
|
query := tc.DB.Model(&models.RegressionResult{})
|
||||||
switch regType {
|
switch regType {
|
||||||
case models.LinearRegression:
|
case models.LinearRegression:
|
||||||
query = query.Where("slope IS NOT NULL").Select("train_id, slope AS metric")
|
query = query.Where("slope IS NOT NULL").Select("train_id, slope,regression_type")
|
||||||
case models.QuadraticRegression:
|
case models.QuadraticRegression:
|
||||||
query = query.Where("quadratic_a IS NOT NULL").
|
query = query.Where("quadratic_a IS NOT NULL").
|
||||||
Select("train_id, ABS(quadratic_a) AS metric")
|
Select("train_id, ABS(quadratic_a),regression_type")
|
||||||
}
|
}
|
||||||
if err := query.Find(&records).Error; err != nil {
|
if err := query.Debug().Find(&records).Error; err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询排名数据失败"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "查询排名数据失败"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -767,12 +767,22 @@ func (tc *StepTrainingController) GetTrainingRank(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排序逻辑
|
|
||||||
sort.Slice(records, func(i, j int) bool {
|
sort.Slice(records, func(i, j int) bool {
|
||||||
if regType == models.LinearRegression {
|
// 处理空指针情况
|
||||||
return *records[i].Slope < *records[j].Slope
|
slopeI := records[i].Slope
|
||||||
|
slopeJ := records[j].Slope
|
||||||
|
|
||||||
|
if slopeI == nil && slopeJ == nil {
|
||||||
|
return false // 两者均为空时视为相等
|
||||||
}
|
}
|
||||||
return math.Abs(*records[i].QuadraticA) > math.Abs(*records[j].QuadraticA) // 二次回归按绝对值降序
|
if slopeI == nil {
|
||||||
|
return false // 空值视为极大值(排在最后)
|
||||||
|
}
|
||||||
|
if slopeJ == nil {
|
||||||
|
return true // 非空值始终排在前
|
||||||
|
}
|
||||||
|
|
||||||
|
return *slopeI > *slopeJ
|
||||||
})
|
})
|
||||||
|
|
||||||
// 计算排名(处理并列)
|
// 计算排名(处理并列)
|
||||||
|
|||||||
Reference in New Issue
Block a user