| 1 | package tasklog |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "sync/atomic" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/tr" |
| 10 | ) |
| 11 | |
| 12 | // PercentageTask is a task that is performed against a known number of |
| 13 | // elements. |
| 14 | type PercentageTask struct { |
| 15 | // members managed via sync/atomic must be aligned at the top of this |
| 16 | // structure (see: https://github.com/git-lfs/git-lfs/pull/2880). |
| 17 | |
| 18 | // n is the number of elements whose work has been completed. It is |
| 19 | // managed sync/atomic. |
| 20 | n uint64 |
| 21 | // total is the total number of elements to execute work upon. |
| 22 | total uint64 |
| 23 | // msg is the task message. |
| 24 | msg string |
| 25 | // ch is a channel which is written to when the task state changes and |
| 26 | // is closed when the task is completed. |
| 27 | ch chan *Update |
| 28 | } |
| 29 | |
| 30 | func NewPercentageTask(msg string, total uint64) *PercentageTask { |
| 31 | p := &PercentageTask{ |
| 32 | msg: msg, |
| 33 | total: total, |
| 34 | ch: make(chan *Update, 1), |
| 35 | } |
| 36 | p.Count(0) |
| 37 | |
| 38 | return p |
| 39 | } |
| 40 | |
| 41 | // Count indicates that work has been completed against "n" number of elements, |
| 42 | // marking the task as complete if the total "n" given to all invocations of |
| 43 | // this method is equal to total. |
| 44 | // |
| 45 | // Count returns the new total number of (atomically managed) elements that have |
| 46 | // been completed. |
| 47 | func (c *PercentageTask) Count(n uint64) (new uint64) { |
| 48 | if new = atomic.AddUint64(&c.n, n); new > c.total { |
| 49 | panic(fmt.Sprintf("tasklog: %s", tr.Tr.Get("counted too many items"))) |
| 50 | } |
| 51 | |
| 52 | var percentage float64 |
| 53 | if c.total == 0 { |
| 54 | percentage = 100 |
| 55 | } else { |
| 56 | percentage = 100 * float64(new) / float64(c.total) |
| 57 | } |
| 58 | |
| 59 | c.ch <- &Update{ |
| 60 | S: fmt.Sprintf("%s: %3.f%% (%d/%d)", |
| 61 | c.msg, math.Floor(percentage), new, c.total), |
| 62 | At: time.Now(), |
| 63 | } |
| 64 | |
| 65 | if new >= c.total { |
| 66 | close(c.ch) |
| 67 | } |
| 68 | |
| 69 | return new |
| 70 | } |
| 71 | |
| 72 | // Entry logs a line-delimited task entry. |
| 73 | func (c *PercentageTask) Entry(update string) { |
| 74 | c.ch <- &Update{ |
| 75 | S: fmt.Sprintf("%s\n", update), |
| 76 | At: time.Now(), |
| 77 | Force: true, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Complete notes that the task is completed by setting the number of |
| 82 | // completed elements to the total number of elements, and if necessary |
| 83 | // closing the Updates channel, which yields the logger to the next Task. |
| 84 | func (c *PercentageTask) Complete() { |
| 85 | if count := atomic.SwapUint64(&c.n, c.total); count < c.total { |
| 86 | close(c.ch) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Updates implements Task.Updates and returns a channel which is written to |
| 91 | // when the state of this task changes, and closed when the task is completed. |
| 92 | func (c *PercentageTask) Updates() <-chan *Update { |
| 93 | return c.ch |
| 94 | } |
| 95 | |
| 96 | // Throttled implements Task.Throttled and returns true, indicating that this |
| 97 | // task is throttled. |
| 98 | func (c *PercentageTask) Throttled() bool { return true } |