Compare commits

...

3 Commits

Author SHA1 Message Date
1a252a12be feat: count. 2025-10-22 15:25:42 +08:00
8d8dd26a2c feat: calculate when upload. 2025-08-05 10:55:19 +08:00
a14c553736 refactor: port. 2025-08-05 10:50:39 +08:00
3 changed files with 29 additions and 1 deletions

View File

@ -79,6 +79,33 @@ func (tc *StepTrainingController) CreateTrainingRecord(c *gin.Context) {
return nil
})
// ====== 新增部分:启动异步回归计算 ======
go func() {
// 查询完整数据(需要关联的心率和步频数据)
var fullRecord models.StepTrainRecord
if err := tc.DB.
Where("train_id = ?", record.TrainId).
Preload("HeartRates", "heart_rate_type = ?", 1). // 只要有效心率
Preload("StrideFreqs", "predict_value = ?", 1). // 只要有效步频
First(&fullRecord).Error; err != nil {
log.Printf("训练记录%d查询失败无法计算回归: %v", record.TrainId, err)
return
}
// 检查数据是否满足计算条件
if len(fullRecord.HeartRates) == 0 || len(fullRecord.StrideFreqs) == 0 {
log.Printf("训练记录%d缺少心率或步频数据跳过回归计算", record.TrainId)
return
}
// 计算并保存回归结果
if _, err := tc.GetOrCalculateRegression(fullRecord.TrainId); err != nil {
log.Printf("训练记录%d回归计算失败: %v", fullRecord.TrainId, err)
} else {
log.Printf("训练记录%d回归结果已保存", fullRecord.TrainId)
}
}()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return

View File

@ -28,5 +28,5 @@ func main() {
// 启动服务
r := routes.SetupRouter()
r.Run(":8000")
r.Run(":8080")
}

View File

@ -7,6 +7,7 @@ type StepStrideFreq struct {
TrainId uint `gorm:"column:train_id; index" json:"trainId"` // 外键关联训练记录[4](@ref)
Time int64 `gorm:"type:bigint" json:"time"` // 保持与前端一致的毫秒时间戳[3](@ref)
Value int `gorm:"type:int" json:"value"`
Count int `gorm:"type:int" json:"count"`
PredictValue int `gorm:"type:int" json:"predictValue"`
Identifier string `gorm:"uniqueIndex;type:varchar(255)" json:"identifier"`
}