github.com/vmware/govmomi@v0.51.0/vim25/progress/scale.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package progress
     6  
     7  type scaledReport struct {
     8  	Report
     9  	n int
    10  	i int
    11  }
    12  
    13  func (r scaledReport) Percentage() float32 {
    14  	b := 100 * float32(r.i) / float32(r.n)
    15  	return b + (r.Report.Percentage() / float32(r.n))
    16  }
    17  
    18  type scaleOne struct {
    19  	s Sinker
    20  	n int
    21  	i int
    22  }
    23  
    24  func (s scaleOne) Sink() chan<- Report {
    25  	upstream := make(chan Report)
    26  	downstream := s.s.Sink()
    27  	go s.loop(upstream, downstream)
    28  	return upstream
    29  }
    30  
    31  func (s scaleOne) loop(upstream <-chan Report, downstream chan<- Report) {
    32  	defer close(downstream)
    33  
    34  	for r := range upstream {
    35  		downstream <- scaledReport{
    36  			Report: r,
    37  			n:      s.n,
    38  			i:      s.i,
    39  		}
    40  	}
    41  }
    42  
    43  type scaleMany struct {
    44  	s Sinker
    45  	n int
    46  	i int
    47  }
    48  
    49  func Scale(s Sinker, n int) Sinker {
    50  	return &scaleMany{
    51  		s: s,
    52  		n: n,
    53  	}
    54  }
    55  
    56  func (s *scaleMany) Sink() chan<- Report {
    57  	if s.i == s.n {
    58  		s.n++
    59  	}
    60  
    61  	ch := scaleOne{s: s.s, n: s.n, i: s.i}.Sink()
    62  	s.i++
    63  	return ch
    64  }