github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/consumer/session/stats.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package session
    19  
    20  import (
    21  	"math/big"
    22  	"time"
    23  
    24  	"github.com/mysteriumnetwork/node/identity"
    25  )
    26  
    27  // NewStats initiates zero Stats instance.
    28  func NewStats() Stats {
    29  	return Stats{
    30  		ConsumerCounts: make(map[identity.Identity]int),
    31  		SumTokens:      new(big.Int),
    32  	}
    33  }
    34  
    35  // Stats holds structure of aggregate session statistics.
    36  type Stats struct {
    37  	Count           int
    38  	ConsumerCounts  map[identity.Identity]int
    39  	SumDataSent     uint64
    40  	SumDataReceived uint64
    41  	SumDuration     time.Duration
    42  	SumTokens       *big.Int
    43  }
    44  
    45  // Add accumulates given session to statistics.
    46  func (s *Stats) Add(session History) {
    47  	s.Count++
    48  
    49  	if _, found := s.ConsumerCounts[session.ConsumerID]; !found {
    50  		s.ConsumerCounts[session.ConsumerID] = 1
    51  	} else {
    52  		s.ConsumerCounts[session.ConsumerID]++
    53  	}
    54  
    55  	s.SumDataReceived += session.DataReceived
    56  	s.SumDataSent += session.DataSent
    57  	s.SumDuration += session.GetDuration()
    58  	s.SumTokens = new(big.Int).Add(s.SumTokens, session.Tokens)
    59  }