github.com/0chain/gosdk@v1.17.11/winsdk/status.go (about)

     1  package main
     2  
     3  import (
     4  	"sync"
     5  
     6  	lru "github.com/hashicorp/golang-lru/v2"
     7  )
     8  
     9  var (
    10  	statusUpload, _   = lru.New[string, *Status](1000)
    11  	statusDownload, _ = lru.New[string, *Status](1000)
    12  	transcodeFiles, _ = lru.New[string, string](1000)
    13  )
    14  
    15  type Status struct {
    16  	Started        bool
    17  	TotalBytes     int
    18  	CompletedBytes int
    19  	Error          string
    20  	Completed      bool
    21  	LookupHash     string
    22  	wg             *sync.WaitGroup
    23  }
    24  
    25  type StatusCallback struct {
    26  	key   string
    27  	items *lru.Cache[string, *Status]
    28  }
    29  
    30  func NewStatusBar(items *lru.Cache[string, *Status], key string) *StatusCallback {
    31  	sc := &StatusCallback{
    32  		key:   key,
    33  		items: items,
    34  	}
    35  
    36  	return sc
    37  }
    38  
    39  func (c *StatusCallback) getStatus(lookupHash string) *Status {
    40  
    41  	if len(c.key) == 0 {
    42  		s, ok := c.items.Get(lookupHash)
    43  
    44  		if !ok {
    45  			s = &Status{}
    46  			c.items.Add(lookupHash, s)
    47  		}
    48  		return s
    49  	}
    50  
    51  	s, ok := c.items.Get(c.key)
    52  
    53  	if !ok {
    54  		s = &Status{}
    55  		c.items.Add(c.key, s)
    56  	}
    57  	return s
    58  }
    59  
    60  func (c *StatusCallback) Started(allocationID, remotePath string, op int, totalBytes int) {
    61  	lookupHash := getLookupHash(allocationID, remotePath)
    62  	log.Info("status: Started ", remotePath, " ", totalBytes, " ", lookupHash)
    63  	s := c.getStatus(lookupHash)
    64  	s.Started = true
    65  	s.TotalBytes = totalBytes
    66  	s.LookupHash = lookupHash
    67  }
    68  
    69  func (c *StatusCallback) InProgress(allocationID, remotePath string, op int, completedBytes int, data []byte) {
    70  	lookupHash := getLookupHash(allocationID, remotePath)
    71  	log.Info("status: InProgress ", remotePath, " ", completedBytes, " ", lookupHash)
    72  	s := c.getStatus(lookupHash)
    73  	s.CompletedBytes = completedBytes
    74  	s.LookupHash = lookupHash
    75  	if completedBytes >= s.TotalBytes {
    76  		s.Completed = true
    77  		if s.wg != nil {
    78  			s.wg.Done()
    79  			s.wg = nil
    80  		}
    81  
    82  	}
    83  }
    84  
    85  func (c *StatusCallback) Error(allocationID string, remotePath string, op int, err error) {
    86  	lookupHash := getLookupHash(allocationID, remotePath)
    87  	log.Info("status: Error ", remotePath, " ", err, " ", lookupHash)
    88  	s := c.getStatus(lookupHash)
    89  	s.Error = err.Error()
    90  	s.LookupHash = lookupHash
    91  	if s.wg != nil {
    92  		s.wg.Done()
    93  		s.wg = nil
    94  	}
    95  }
    96  
    97  func (c *StatusCallback) Completed(allocationID, remotePath string, filename string, mimetype string, size int, op int) {
    98  	lookupHash := getLookupHash(allocationID, remotePath)
    99  	log.Info("status: Completed ", remotePath, " ", lookupHash)
   100  	s := c.getStatus(lookupHash)
   101  	s.Completed = true
   102  	s.LookupHash = lookupHash
   103  	s.CompletedBytes = s.TotalBytes
   104  	if s.wg != nil {
   105  		s.wg.Done()
   106  		s.wg = nil
   107  	}
   108  }
   109  
   110  func (c *StatusCallback) CommitMetaCompleted(request, response string, err error) {
   111  	//c.Callback.CommitMetaCompleted(request, response, err)
   112  }
   113  
   114  func (c *StatusCallback) RepairCompleted(filesRepaired int) {
   115  	//c.Callback.RepairCompleted(filesRepaired)
   116  }