Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session stats are now reset #617

Merged
merged 1 commit into from Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions consumer/statistics/reporter.go
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
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