GetBatchProgress 获取批次进度
(batchID string, params *ProgressQueryParams)
| 295 | |
| 296 | // GetBatchProgress 获取批次进度 |
| 297 | func (s *ProgressStorage) GetBatchProgress(batchID string, params *ProgressQueryParams) (*BatchProgressResponse, error) { |
| 298 | batch, err := s.LoadBatch(batchID) |
| 299 | if err != nil { |
| 300 | return nil, err |
| 301 | } |
| 302 | |
| 303 | // 计算批次进度信息 |
| 304 | batchProgress := s.calculateBatchProgress(batch) |
| 305 | |
| 306 | response := &BatchProgressResponse{ |
| 307 | BatchID: batchID, |
| 308 | Status: batch.Status, |
| 309 | BatchProgress: batchProgress, |
| 310 | LastUpdate: batch.UpdatedAt.UnixMilli(), |
| 311 | } |
| 312 | |
| 313 | // 如果需要详细信息 |
| 314 | if params.Detail { |
| 315 | files := make(map[string]*FileProgress) |
| 316 | errors := make([]FileError, 0) |
| 317 | |
| 318 | for FileUploadID, fileUpload := range batch.Files { |
| 319 | // 如果指定了特定文件ID,只返回该文件 |
| 320 | if params.FileUploadID != "" && FileUploadID != params.FileUploadID { |
| 321 | continue |
| 322 | } |
| 323 | |
| 324 | // 如果指定了时间戳,只返回更新时间晚于该时间戳的文件 |
| 325 | if params.Since > 0 && fileUpload.UpdateTime.UnixMilli() <= params.Since { |
| 326 | continue |
| 327 | } |
| 328 | |
| 329 | fileProgress := &FileProgress{ |
| 330 | FileUploadID: FileUploadID, |
| 331 | RelativePath: fileUpload.RelativePath, |
| 332 | Status: fileUpload.Status, |
| 333 | Progress: fileUpload.Progress, |
| 334 | UploadedSize: fileUpload.UploadedSize, |
| 335 | TotalSize: fileUpload.TotalSize, |
| 336 | Speed: fileUpload.Speed, |
| 337 | ETA: fileUpload.ETA, |
| 338 | Error: fileUpload.Error, |
| 339 | } |
| 340 | |
| 341 | // 如果有数据库ID,从数据库获取文件信息 |
| 342 | if fileUpload.DatabaseID != 0 { |
| 343 | // 从数据库获取文件详细信息 |
| 344 | dbFile, err := model.GetUploadFileByID(fileUpload.DatabaseID) |
| 345 | if err == nil && dbFile != nil { |
| 346 | // 添加数据库ID到响应中 |
| 347 | fileProgress.DatabaseID = dbFile.ID |
| 348 | // 如果有错误信息,确保包含在响应中 |
| 349 | if fileProgress.Status == "failed" && (dbFile.Error != "" && fileProgress.Error == "") { |
| 350 | fileProgress.Error = dbFile.Error |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 |
no test coverage detected