github.com/status-im/status-go@v1.1.0/services/rpcstats/api.go (about)

     1  package rpcstats
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  )
     7  
     8  // PublicAPI represents a set of APIs from the namespace.
     9  type PublicAPI struct {
    10  	s *Service
    11  }
    12  
    13  // NewAPI creates an instance of the API.
    14  func NewAPI(s *Service) *PublicAPI {
    15  	return &PublicAPI{s: s}
    16  }
    17  
    18  // Reset resets RPC usage stats
    19  func (api *PublicAPI) Reset(context context.Context) {
    20  	resetStats()
    21  }
    22  
    23  type RPCStats struct {
    24  	Total            uint            `json:"total"`
    25  	CounterPerMethod map[string]uint `json:"methods"`
    26  }
    27  
    28  // GetStats returns RPC usage stats
    29  func (api *PublicAPI) GetStats(context context.Context) (RPCStats, error) {
    30  	total, perMethod, perMethodPerTag := getStats()
    31  
    32  	counterPerMethod := make(map[string]uint)
    33  	perMethod.Range(func(key, value interface{}) bool {
    34  		counterPerMethod[key.(string)] = value.(uint)
    35  		return true
    36  	})
    37  	perMethodPerTag.Range(func(key, value interface{}) bool {
    38  		tag := key.(string)
    39  		methods := value.(*sync.Map)
    40  		methods.Range(func(key, value interface{}) bool {
    41  			method := key.(string)
    42  			count := value.(uint)
    43  			counterPerMethod[method+"_"+tag] = count
    44  			return true
    45  		})
    46  		return true
    47  	})
    48  
    49  	return RPCStats{
    50  		Total:            total,
    51  		CounterPerMethod: counterPerMethod,
    52  	}, nil
    53  }