convertTaskProgressToResponse renders a task's current state, computing percent and speed from the snapshot taken under the task's mutex.
(task *TaskProgressInfo)
| 188 | // convertTaskProgressToResponse renders a task's current state, computing |
| 189 | // percent and speed from the snapshot taken under the task's mutex. |
| 190 | func convertTaskProgressToResponse(task *TaskProgressInfo) TaskInfoResponse { |
| 191 | status, total, downloaded, totalFiles, downloadedFiles, startedAt, errMsg, updatedAt := task.snapshot() |
| 192 | |
| 193 | resp := TaskInfoResponse{ |
| 194 | TaskID: task.TaskID, |
| 195 | Type: tasktype.TaskType(task.Type), |
| 196 | Status: status, |
| 197 | Title: task.Title, |
| 198 | Storage: task.Storage, |
| 199 | Path: task.Path, |
| 200 | Error: errMsg, |
| 201 | CreatedAt: task.CreatedAt, |
| 202 | UpdatedAt: updatedAt, |
| 203 | } |
| 204 | |
| 205 | var percent float64 |
| 206 | var speedMBPS float64 |
| 207 | if total > 0 { |
| 208 | percent = float64(downloaded) * 100 / float64(total) |
| 209 | } else if totalFiles > 0 { |
| 210 | percent = float64(downloadedFiles) * 100 / float64(totalFiles) |
| 211 | } |
| 212 | if !startedAt.IsZero() { |
| 213 | elapsed := time.Since(startedAt).Seconds() |
| 214 | if elapsed > 0 { |
| 215 | speedMBPS = float64(downloaded) / elapsed / (1024 * 1024) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if total > 0 || totalFiles > 0 { |
| 220 | resp.Progress = &TaskProgress{ |
| 221 | TotalBytes: total, |
| 222 | DownloadedBytes: downloaded, |
| 223 | TotalFiles: totalFiles, |
| 224 | DownloadedFiles: downloadedFiles, |
| 225 | Percent: percent, |
| 226 | SpeedMBPS: speedMBPS, |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return resp |
| 231 | } |
| 232 | |
| 233 | // NotFoundHandler 404 处理器 |
| 234 | func NotFoundHandler(w http.ResponseWriter, r *http.Request) { |
no test coverage detected