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

     1  /*
     2   * Copyright (C) 2021 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 entertainment
    19  
    20  import "math"
    21  
    22  const (
    23  	video720pMBPerMin   = 15
    24  	audioNormalMBPerMin = 0.75
    25  	browsingMBPerMin    = 0.5
    26  )
    27  
    28  // Estimates represent estimated entertainment
    29  type Estimates struct {
    30  	VideoMinutes    uint64
    31  	MusicMinutes    uint64
    32  	BrowsingMinutes uint64
    33  	TrafficMB       uint64
    34  	PricePerGiB     float64
    35  	PricePerMin     float64
    36  }
    37  
    38  // Estimator stores average provider prices to estimate entertainment estimates
    39  type Estimator struct {
    40  	pricePerGiB float64
    41  	pricePerMin float64
    42  }
    43  
    44  // NewEstimator constructor
    45  func NewEstimator(pricePerGiB, pricePerMin float64) *Estimator {
    46  	return &Estimator{
    47  		pricePerGiB: pricePerGiB,
    48  		pricePerMin: pricePerMin,
    49  	}
    50  }
    51  
    52  // EstimatedEntertainment calculates average service times
    53  func (e *Estimator) EstimatedEntertainment(myst float64) Estimates {
    54  	return Estimates{
    55  		VideoMinutes:    e.minutes(myst, video720pMBPerMin),
    56  		MusicMinutes:    e.minutes(myst, audioNormalMBPerMin),
    57  		BrowsingMinutes: e.minutes(myst, browsingMBPerMin),
    58  		TrafficMB:       uint64(mib2MB(e.totalTrafficMiB(myst))),
    59  		PricePerGiB:     e.pricePerGiB,
    60  		PricePerMin:     e.pricePerMin,
    61  	}
    62  }
    63  
    64  func mib2MB(mibs float64) float64 {
    65  	return mibs * math.Pow(2, 20) / math.Pow(10, 6)
    66  }
    67  
    68  func mb2MiB(mb float64) float64 {
    69  	return mb * math.Pow(10, 6) / math.Pow(2, 20)
    70  }
    71  
    72  func (e *Estimator) totalTrafficMiB(amount float64) float64 {
    73  	return amount / e.pricePerGiB * 1024
    74  }
    75  
    76  func (e *Estimator) minutes(amount, serviceMBPerMin float64) uint64 {
    77  	pricePerMiB := e.pricePerGiB / 1024
    78  	totalPricePerMin := mb2MiB(serviceMBPerMin)*pricePerMiB + e.pricePerMin
    79  	return uint64(amount / totalPricePerMin)
    80  }