github.com/koko1123/flow-go-1@v0.29.6/ledger/complete/ledger_stats.go (about)

     1  package complete
     2  
     3  import (
     4  	"github.com/schollz/progressbar/v3"
     5  
     6  	"github.com/koko1123/flow-go-1/ledger"
     7  	"github.com/koko1123/flow-go-1/ledger/complete/mtrie/flattener"
     8  	"github.com/koko1123/flow-go-1/ledger/complete/mtrie/node"
     9  )
    10  
    11  type LedgerStats struct {
    12  	TrieCount        uint64 `json:"tries_count"`
    13  	NodeCount        uint64 `json:"node_count"`
    14  	InterimNodeCount uint64 `json:"interim_node_count"`
    15  	LeafNodeCount    uint64 `json:"leaf_node_count"`
    16  }
    17  
    18  func (l *Ledger) CollectStats(payloadCallBack func(payload *ledger.Payload)) (*LedgerStats, error) {
    19  	visitedNodes := make(map[*node.Node]uint64)
    20  	var interimNodeCounter, leafNodeCounter, totalNodeCounter uint64
    21  
    22  	tries, err := l.Tries()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	bar := progressbar.Default(int64(len(tries)), "collecting ledger stats")
    28  	for _, trie := range tries {
    29  		for itr := flattener.NewUniqueNodeIterator(trie.RootNode(), visitedNodes); itr.Next(); {
    30  			n := itr.Value()
    31  			if n.IsLeaf() {
    32  				payload := n.Payload()
    33  				leafNodeCounter++
    34  				payloadCallBack(payload)
    35  			} else {
    36  				interimNodeCounter++
    37  			}
    38  			visitedNodes[n] = totalNodeCounter
    39  			totalNodeCounter++
    40  		}
    41  		if err = bar.Add(1); err != nil {
    42  			return nil, err
    43  		}
    44  	}
    45  
    46  	return &LedgerStats{
    47  		TrieCount:        uint64(len(tries)),
    48  		NodeCount:        totalNodeCounter,
    49  		InterimNodeCount: interimNodeCounter,
    50  		LeafNodeCount:    leafNodeCounter,
    51  	}, nil
    52  }