fix: sort.

This commit is contained in:
2025-08-05 09:55:37 +08:00
parent 7a2a44e327
commit 3078c13e14

View File

@ -751,12 +751,12 @@ func (tc *StepTrainingController) GetTrainingRank(c *gin.Context) {
query := tc.DB.Model(&models.RegressionResult{})
switch regType {
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:
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": "查询排名数据失败"})
return
}
@ -767,12 +767,22 @@ func (tc *StepTrainingController) GetTrainingRank(c *gin.Context) {
return
}
// 排序逻辑
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
})
// 计算排名(处理并列)