summaryrefslogtreecommitdiff
path: root/plugin/progress.go
blob: ffe6c70aaeac33ad51b0baf75fad7e96161128e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package plugin

import (
	"sync"
	"time"

	"github.com/containerd/containerd/remotes/docker"
)

func newPushJobs(tracker docker.StatusTracker) *pushJobs {
	return &pushJobs{
		names: make(map[string]string),
		t:     tracker,
	}
}

type pushJobs struct {
	t docker.StatusTracker

	mu   sync.Mutex
	jobs []string
	// maps job ref to a name
	names map[string]string
}

func (p *pushJobs) add(id, name string) {
	p.mu.Lock()
	defer p.mu.Unlock()

	if _, ok := p.names[id]; ok {
		return
	}
	p.jobs = append(p.jobs, id)
	p.names[id] = name
}

func (p *pushJobs) status() []contentStatus {
	statuses := make([]contentStatus, 0, len(p.jobs))

	p.mu.Lock()
	defer p.mu.Unlock()

	for _, j := range p.jobs {
		var s contentStatus
		s.Ref = p.names[j]

		status, err := p.t.GetStatus(j)
		if err != nil {
			s.Status = "Waiting"
		} else {
			s.Total = status.Total
			s.Offset = status.Offset
			s.StartedAt = status.StartedAt
			s.UpdatedAt = status.UpdatedAt
			if status.UploadUUID == "" {
				s.Status = "Upload complete"
			} else {
				s.Status = "Uploading"
			}
		}
		statuses = append(statuses, s)
	}

	return statuses
}

type contentStatus struct {
	Status    string
	Total     int64
	Offset    int64
	StartedAt time.Time
	UpdatedAt time.Time
	Ref       string
}