Skip to content

Commit

Permalink
Merge pull request #617 from mysteriumnetwork/bug/session-stats-reset
Browse files Browse the repository at this point in the history
session stats are now reset
  • Loading branch information
zolia authored Dec 14, 2018
2 parents 7749a27 + 53ba245 commit 2571549
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
25 changes: 14 additions & 11 deletions consumer/statistics/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ var ErrSessionNotStarted = errors.New("session not started")
// LocationDetector detects the country for session stats
type LocationDetector func() location.Location

// Retriever allows for retrieval of statistics
type Retriever interface {
// StatsTracker allows for retrieval and resetting of statistics
type StatsTracker interface {
Retrieve() consumer.SessionStatistics
Reset()
}

// Reporter defines method for sending stats outside
Expand All @@ -55,9 +56,9 @@ type Reporter interface {
type SessionStatisticsReporter struct {
locationDetector LocationDetector

signerFactory identity.SignerFactory
statisticsRetriever Retriever
remoteReporter Reporter
signerFactory identity.SignerFactory
statisticsTracker StatsTracker
remoteReporter Reporter

sendInterval time.Duration
done chan struct{}
Expand All @@ -67,12 +68,12 @@ type SessionStatisticsReporter struct {
}

// NewSessionStatisticsReporter function creates new session stats sender by given options
func NewSessionStatisticsReporter(statisticsRetriever Retriever, remoteReporter Reporter, signerFactory identity.SignerFactory, locationDetector LocationDetector, interval time.Duration) *SessionStatisticsReporter {
func NewSessionStatisticsReporter(statisticsTracker StatsTracker, remoteReporter Reporter, signerFactory identity.SignerFactory, locationDetector LocationDetector, interval time.Duration) *SessionStatisticsReporter {
return &SessionStatisticsReporter{
locationDetector: locationDetector,
signerFactory: signerFactory,
statisticsRetriever: statisticsRetriever,
remoteReporter: remoteReporter,
locationDetector: locationDetector,
signerFactory: signerFactory,
statisticsTracker: statisticsTracker,
remoteReporter: remoteReporter,

sendInterval: interval,
done: make(chan struct{}),
Expand Down Expand Up @@ -101,6 +102,8 @@ func (sr *SessionStatisticsReporter) start(consumerID identity.Identity, service
} else {
log.Debug(statsSenderLogPrefix, "Final stats sent")
}
// reset the stats in preparation for a new session
sr.statisticsTracker.Reset()
return
case <-time.After(sr.sendInterval):
if err := sr.send(serviceType, providerID, country, sessionID, signer); err != nil {
Expand Down Expand Up @@ -131,7 +134,7 @@ func (sr *SessionStatisticsReporter) stop() {
}

func (sr *SessionStatisticsReporter) send(serviceType, providerID, country string, sessionID session.ID, signer identity.Signer) error {
sessionStats := sr.statisticsRetriever.Retrieve()
sessionStats := sr.statisticsTracker.Retrieve()
return sr.remoteReporter.SendSessionStats(
sessionID,
mysterium.SessionStats{
Expand Down
5 changes: 5 additions & 0 deletions consumer/statistics/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (sst *SessionStatisticsTracker) Retrieve() consumer.SessionStatistics {
return sst.sessionStats
}

// Reset resets session stats to 0
func (sst *SessionStatisticsTracker) Reset() {
sst.sessionStats = consumer.SessionStatistics{}
}

// MarkSessionStart marks current time as session start time for statistics
func (sst *SessionStatisticsTracker) markSessionStart() {
time := sst.timeGetter()
Expand Down

0 comments on commit 2571549

Please sign in to comment.