File: percentage_task.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (98 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tasklog

import (
	"fmt"
	"math"
	"sync/atomic"
	"time"

	"github.com/git-lfs/git-lfs/v3/tr"
)

// PercentageTask is a task that is performed against a known number of
// elements.
type PercentageTask struct {
	// members managed via sync/atomic must be aligned at the top of this
	// structure (see: https://github.com/git-lfs/git-lfs/pull/2880).

	// n is the number of elements whose work has been completed. It is
	// managed sync/atomic.
	n uint64
	// total is the total number of elements to execute work upon.
	total uint64
	// msg is the task message.
	msg string
	// ch is a channel which is written to when the task state changes and
	// is closed when the task is completed.
	ch chan *Update
}

func NewPercentageTask(msg string, total uint64) *PercentageTask {
	p := &PercentageTask{
		msg:   msg,
		total: total,
		ch:    make(chan *Update, 1),
	}
	p.Count(0)

	return p
}

// Count indicates that work has been completed against "n" number of elements,
// marking the task as complete if the total "n" given to all invocations of
// this method is equal to total.
//
// Count returns the new total number of (atomically managed) elements that have
// been completed.
func (c *PercentageTask) Count(n uint64) (new uint64) {
	if new = atomic.AddUint64(&c.n, n); new > c.total {
		panic(fmt.Sprintf("tasklog: %s", tr.Tr.Get("counted too many items")))
	}

	var percentage float64
	if c.total == 0 {
		percentage = 100
	} else {
		percentage = 100 * float64(new) / float64(c.total)
	}

	c.ch <- &Update{
		S: fmt.Sprintf("%s: %3.f%% (%d/%d)",
			c.msg, math.Floor(percentage), new, c.total),
		At: time.Now(),
	}

	if new >= c.total {
		close(c.ch)
	}

	return new
}

// Entry logs a line-delimited task entry.
func (c *PercentageTask) Entry(update string) {
	c.ch <- &Update{
		S:     fmt.Sprintf("%s\n", update),
		At:    time.Now(),
		Force: true,
	}
}

// Complete notes that the task is completed by setting the number of
// completed elements to the total number of elements, and if necessary
// closing the Updates channel, which yields the logger to the next Task.
func (c *PercentageTask) Complete() {
	if count := atomic.SwapUint64(&c.n, c.total); count < c.total {
		close(c.ch)
	}
}

// Updates implements Task.Updates and returns a channel which is written to
// when the state of this task changes, and closed when the task is completed.
func (c *PercentageTask) Updates() <-chan *Update {
	return c.ch
}

// Throttled implements Task.Throttled and returns true, indicating that this
// task is throttled.
func (c *PercentageTask) Throttled() bool { return true }