| 410 | ) |
| 411 | |
| 412 | func toPullProgressEvent(parent string, jm jsonstream.Message, events api.EventProcessor) { |
| 413 | if jm.ID == "" || jm.Progress == nil { |
| 414 | return |
| 415 | } |
| 416 | |
| 417 | var ( |
| 418 | details string |
| 419 | total int64 |
| 420 | percent int |
| 421 | current int64 |
| 422 | status = api.Working |
| 423 | ) |
| 424 | |
| 425 | switch jm.Status { |
| 426 | case PreparingPhase, WaitingPhase, PullingFsPhase: |
| 427 | percent = 0 |
| 428 | case DownloadingPhase, ExtractingPhase, VerifyingChecksumPhase: |
| 429 | if jm.Progress != nil { |
| 430 | current = jm.Progress.Current |
| 431 | total = jm.Progress.Total |
| 432 | if jm.Progress.Total > 0 { |
| 433 | percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100) |
| 434 | } |
| 435 | } |
| 436 | case DownloadCompletePhase, AlreadyExistsPhase, PullCompletePhase: |
| 437 | status = api.Done |
| 438 | percent = 100 |
| 439 | } |
| 440 | |
| 441 | if strings.Contains(jm.Status, "Image is up to date") || |
| 442 | strings.Contains(jm.Status, "Downloaded newer image") { |
| 443 | status = api.Done |
| 444 | percent = 100 |
| 445 | } |
| 446 | |
| 447 | if jm.Error != nil { |
| 448 | status = api.Error |
| 449 | details = jm.Error.Message |
| 450 | } else { |
| 451 | details = units.HumanSize(float64(jm.Progress.Current)) |
| 452 | } |
| 453 | |
| 454 | events.On(api.Resource{ |
| 455 | ID: jm.ID, |
| 456 | ParentID: parent, |
| 457 | Current: current, |
| 458 | Total: total, |
| 459 | Percent: percent, |
| 460 | Status: status, |
| 461 | Text: jm.Status, |
| 462 | Details: details, |
| 463 | }) |
| 464 | } |