From 0b27acc67b3b7dcc298b27f39d2f0cdcf764f14e Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Thu, 15 Oct 2020 15:30:35 -0700 Subject: [PATCH 1/8] Initial commit --- trader/trader.go | 38 +++++++++++++++++++++------ trader/trader_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/trader/trader.go b/trader/trader.go index 35221064f..a6944d716 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -115,21 +115,28 @@ func MakeTrader( // Start starts the bot with the injected strategy func (t *Trader) Start() { log.Println("----------------------------------------------------------------------------------------------------") - var lastUpdateTime time.Time + var ( + lastUpdateTime time.Time + lastMetricUpdateTime time.Time + ) + startTime := time.Now() for { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - millisForUpdate := time.Since(currentUpdateTime).Milliseconds() - e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { - e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) + if shouldSendUpdateMetric(startTime, currentUpdateTime, lastMetricUpdateTime) { + millisForUpdate := time.Since(currentUpdateTime).Milliseconds() + e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { + e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) + if e != nil { + log.Printf("failed to send update event metric: %s", e) + } + }, nil) if e != nil { - log.Printf("failed to send update event metric: %s", e) + log.Printf("failed to trigger goroutine for send update event: %s", e) } - }, nil) - if e != nil { - log.Printf("failed to trigger goroutine for send update event: %s", e) + lastMetricUpdateTime = currentUpdateTime } if t.fixedIterations != nil && success { @@ -154,6 +161,21 @@ func (t *Trader) Start() { } } +func shouldSendUpdateMetric(start, currentUpdate, lastMetricUpdate time.Time) bool { + timeFromStart := currentUpdate.Sub(start) + var refreshMetricInterval time.Duration + switch { + case timeFromStart < 5*time.Minute: + refreshMetricInterval = 5 * time.Second + case timeFromStart < 1*time.Hour: + refreshMetricInterval = 10 * time.Minute + default: + refreshMetricInterval = 1 * time.Hour + } + + return currentUpdate.Sub(lastMetricUpdate) >= refreshMetricInterval +} + // deletes all offers for the bot (not all offers on the account) func (t *Trader) deleteAllOffers(isAsync bool) { logPrefix := "" diff --git a/trader/trader_test.go b/trader/trader_test.go index fcb682ac1..89ffcb820 100644 --- a/trader/trader_test.go +++ b/trader/trader_test.go @@ -2,6 +2,7 @@ package trader import ( "testing" + "time" "github.com/stretchr/testify/assert" @@ -159,3 +160,63 @@ func TestIsStateSynchronized(t *testing.T) { }) } } + +func TestShouldSendUpdateMetric(t *testing.T) { + now := time.Now() + testCases := []struct { + name string + start time.Time + currentUpdate time.Time + lastMetricUpdate time.Time + wantShouldSendMetric bool + }{ + { + name: "first 5 mins - refresh", + start: now.Add(-4 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-10 * time.Second), + wantShouldSendMetric: true, + }, + { + name: "first 5 mins - no refresh", + start: now.Add(-4 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-3 * time.Second), + wantShouldSendMetric: false, + }, + { + name: "first hour - refresh", + start: now.Add(-50 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-15 * time.Minute), + wantShouldSendMetric: true, + }, + { + name: "first hour - no refresh", + start: now.Add(-50 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-5 * time.Minute), + wantShouldSendMetric: false, + }, + { + name: "past first hour - refresh", + start: now.Add(-2 * time.Hour), + currentUpdate: now, + lastMetricUpdate: now.Add(-65 * time.Minute), + wantShouldSendMetric: true, + }, + { + name: "past first hour - no refresh", + start: now.Add(-2 * time.Hour), + currentUpdate: now, + lastMetricUpdate: now.Add(-15 * time.Minute), + wantShouldSendMetric: false, + }, + } + for _, k := range testCases { + t.Run(k.name, func(t *testing.T) { + actual := shouldSendUpdateMetric(k.start, k.currentUpdate, k.lastMetricUpdate) + assert.Equal(t, k.wantShouldSendMetric, actual) + }) + } +} From 959e55ed4e6990390aede654a6d9ba451b7d9eeb Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 13:36:48 -0700 Subject: [PATCH 2/8] Address review - inject start time, add border tests --- cmd/trade.go | 4 +++- trader/trader.go | 11 +++++++---- trader/trader_test.go | 45 +++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/cmd/trade.go b/cmd/trade.go index 470f067e2..48ecdced1 100644 --- a/cmd/trade.go +++ b/cmd/trade.go @@ -384,6 +384,7 @@ func makeBot( threadTracker *multithreading.ThreadTracker, options inputs, metricsTracker *metrics.MetricsTracker, + botStart time.Time, ) *trader.Trader { timeController := plugins.MakeIntervalTimeController( time.Duration(botConfig.TickIntervalSeconds)*time.Second, @@ -485,6 +486,7 @@ func makeBot( dataKey, alert, metricsTracker, + botStart, ) } @@ -518,7 +520,6 @@ func runTradeCmd(options inputs) { guiVersionFlag = guiVersion } - deviceID, e := machineid.ID() if e != nil { logger.Fatal(l, fmt.Errorf("could not generate machine id: %s", e)) @@ -699,6 +700,7 @@ func runTradeCmd(options inputs) { threadTracker, options, metricsTracker, + botStart, ) // --- end initialization of objects --- // --- start initialization of services --- diff --git a/trader/trader.go b/trader/trader.go index a6944d716..0eb67838c 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -46,6 +46,7 @@ type Trader struct { dataKey *model.BotKey alert api.Alert metricsTracker *metrics.MetricsTracker + start time.Time // initialized runtime vars deleteCycles int64 @@ -83,6 +84,7 @@ func MakeTrader( dataKey *model.BotKey, alert api.Alert, metricsTracker *metrics.MetricsTracker, + start time.Time, ) *Trader { return &Trader{ api: api, @@ -107,6 +109,7 @@ func MakeTrader( dataKey: dataKey, alert: alert, metricsTracker: metricsTracker, + start: start, // initialized runtime vars deleteCycles: 0, } @@ -119,24 +122,23 @@ func (t *Trader) Start() { lastUpdateTime time.Time lastMetricUpdateTime time.Time ) - startTime := time.Now() for { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - if shouldSendUpdateMetric(startTime, currentUpdateTime, lastMetricUpdateTime) { + if shouldSendUpdateMetric(t.start, currentUpdateTime, lastMetricUpdateTime) { millisForUpdate := time.Since(currentUpdateTime).Milliseconds() e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) if e != nil { log.Printf("failed to send update event metric: %s", e) } + lastMetricUpdateTime = currentUpdateTime }, nil) if e != nil { log.Printf("failed to trigger goroutine for send update event: %s", e) } - lastMetricUpdateTime = currentUpdateTime } if t.fixedIterations != nil && success { @@ -173,7 +175,8 @@ func shouldSendUpdateMetric(start, currentUpdate, lastMetricUpdate time.Time) bo refreshMetricInterval = 1 * time.Hour } - return currentUpdate.Sub(lastMetricUpdate) >= refreshMetricInterval + timeSinceLastUpdate := currentUpdate.Sub(lastMetricUpdate) + return timeSinceLastUpdate >= refreshMetricInterval } // deletes all offers for the bot (not all offers on the account) diff --git a/trader/trader_test.go b/trader/trader_test.go index 89ffcb820..7a531285f 100644 --- a/trader/trader_test.go +++ b/trader/trader_test.go @@ -171,45 +171,66 @@ func TestShouldSendUpdateMetric(t *testing.T) { wantShouldSendMetric bool }{ { - name: "first 5 mins - refresh", + name: "first 5 mins - border - refresh", start: now.Add(-4 * time.Minute), currentUpdate: now, - lastMetricUpdate: now.Add(-10 * time.Second), + lastMetricUpdate: now.Add(-5 * time.Second), wantShouldSendMetric: true, }, { - name: "first 5 mins - no refresh", + name: "first 5 mins - greater than - refresh", start: now.Add(-4 * time.Minute), currentUpdate: now, - lastMetricUpdate: now.Add(-3 * time.Second), + lastMetricUpdate: now.Add(-5*time.Second - time.Nanosecond), + wantShouldSendMetric: true, + }, + { + name: "first 5 mins - less than - no refresh", + start: now.Add(-4 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-5*time.Second + time.Nanosecond), wantShouldSendMetric: false, }, { - name: "first hour - refresh", + name: "first hour - border - refresh", start: now.Add(-50 * time.Minute), currentUpdate: now, - lastMetricUpdate: now.Add(-15 * time.Minute), + lastMetricUpdate: now.Add(-10 * time.Minute), wantShouldSendMetric: true, }, { - name: "first hour - no refresh", + name: "first hour - greater than - refresh", start: now.Add(-50 * time.Minute), currentUpdate: now, - lastMetricUpdate: now.Add(-5 * time.Minute), + lastMetricUpdate: now.Add(-10*time.Minute - time.Nanosecond), + wantShouldSendMetric: true, + }, + { + name: "first hour - less than - no refresh", + start: now.Add(-50 * time.Minute), + currentUpdate: now, + lastMetricUpdate: now.Add(-10*time.Minute + time.Nanosecond), wantShouldSendMetric: false, }, { - name: "past first hour - refresh", + name: "past first hour - border - refresh", + start: now.Add(-2 * time.Hour), + currentUpdate: now, + lastMetricUpdate: now.Add(-1 * time.Hour), + wantShouldSendMetric: true, + }, + { + name: "past first hour - greater than - refresh", start: now.Add(-2 * time.Hour), currentUpdate: now, - lastMetricUpdate: now.Add(-65 * time.Minute), + lastMetricUpdate: now.Add(-1*time.Hour - time.Nanosecond), wantShouldSendMetric: true, }, { - name: "past first hour - no refresh", + name: "past first hour - less than - no refresh", start: now.Add(-2 * time.Hour), currentUpdate: now, - lastMetricUpdate: now.Add(-15 * time.Minute), + lastMetricUpdate: now.Add(-1*time.Hour + time.Nanosecond), wantShouldSendMetric: false, }, } From 972df91d35994b873d7648dea175e6287156b9d6 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 14:58:16 -0700 Subject: [PATCH 3/8] Rename startTime, fix boundary condition. --- trader/trader.go | 12 ++++++------ trader/trader_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/trader/trader.go b/trader/trader.go index 0eb67838c..0d0d2c530 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -46,7 +46,7 @@ type Trader struct { dataKey *model.BotKey alert api.Alert metricsTracker *metrics.MetricsTracker - start time.Time + startTime time.Time // initialized runtime vars deleteCycles int64 @@ -84,7 +84,7 @@ func MakeTrader( dataKey *model.BotKey, alert api.Alert, metricsTracker *metrics.MetricsTracker, - start time.Time, + startTime time.Time, ) *Trader { return &Trader{ api: api, @@ -109,7 +109,7 @@ func MakeTrader( dataKey: dataKey, alert: alert, metricsTracker: metricsTracker, - start: start, + startTime: startTime, // initialized runtime vars deleteCycles: 0, } @@ -127,7 +127,7 @@ func (t *Trader) Start() { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - if shouldSendUpdateMetric(t.start, currentUpdateTime, lastMetricUpdateTime) { + if shouldSendUpdateMetric(t.startTime, currentUpdateTime, lastMetricUpdateTime) { millisForUpdate := time.Since(currentUpdateTime).Milliseconds() e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) @@ -167,9 +167,9 @@ func shouldSendUpdateMetric(start, currentUpdate, lastMetricUpdate time.Time) bo timeFromStart := currentUpdate.Sub(start) var refreshMetricInterval time.Duration switch { - case timeFromStart < 5*time.Minute: + case timeFromStart <= 5*time.Minute: refreshMetricInterval = 5 * time.Second - case timeFromStart < 1*time.Hour: + case timeFromStart <= 1*time.Hour: refreshMetricInterval = 10 * time.Minute default: refreshMetricInterval = 1 * time.Hour diff --git a/trader/trader_test.go b/trader/trader_test.go index 7a531285f..4db24afbe 100644 --- a/trader/trader_test.go +++ b/trader/trader_test.go @@ -172,42 +172,42 @@ func TestShouldSendUpdateMetric(t *testing.T) { }{ { name: "first 5 mins - border - refresh", - start: now.Add(-4 * time.Minute), + start: now.Add(-5 * time.Minute), currentUpdate: now, lastMetricUpdate: now.Add(-5 * time.Second), wantShouldSendMetric: true, }, { name: "first 5 mins - greater than - refresh", - start: now.Add(-4 * time.Minute), + start: now.Add(-5 * time.Minute), currentUpdate: now, lastMetricUpdate: now.Add(-5*time.Second - time.Nanosecond), wantShouldSendMetric: true, }, { name: "first 5 mins - less than - no refresh", - start: now.Add(-4 * time.Minute), + start: now.Add(-5 * time.Minute), currentUpdate: now, lastMetricUpdate: now.Add(-5*time.Second + time.Nanosecond), wantShouldSendMetric: false, }, { name: "first hour - border - refresh", - start: now.Add(-50 * time.Minute), + start: now.Add(-1 * time.Hour), currentUpdate: now, lastMetricUpdate: now.Add(-10 * time.Minute), wantShouldSendMetric: true, }, { name: "first hour - greater than - refresh", - start: now.Add(-50 * time.Minute), + start: now.Add(-1 * time.Hour), currentUpdate: now, lastMetricUpdate: now.Add(-10*time.Minute - time.Nanosecond), wantShouldSendMetric: true, }, { name: "first hour - less than - no refresh", - start: now.Add(-50 * time.Minute), + start: now.Add(-1 * time.Hour), currentUpdate: now, lastMetricUpdate: now.Add(-10*time.Minute + time.Nanosecond), wantShouldSendMetric: false, From cbf7d4369bf2531ea243cbfd6e042310930d1422 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 15:00:58 -0700 Subject: [PATCH 4/8] Add lastMetricUpdateTime to struct. --- support/metrics/metricsTracker.go | 35 ++++++++++++++++--------------- trader/trader.go | 9 +++----- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/support/metrics/metricsTracker.go b/support/metrics/metricsTracker.go index 04f5738c8..26f05d890 100644 --- a/support/metrics/metricsTracker.go +++ b/support/metrics/metricsTracker.go @@ -24,13 +24,14 @@ const ( // and can be used to directly send events to the // Amplitude HTTP API. type MetricsTracker struct { - client *http.Client - apiKey string - userID string - deviceID string - props commonProps - start time.Time - isDisabled bool + client *http.Client + apiKey string + userID string + deviceID string + props commonProps + botStartTime time.Time + isDisabled bool + lastMetricUpdateTime time.Time } // TODO DS Investigate other fields to add to this top-level event. @@ -109,7 +110,7 @@ func MakeMetricsTracker( deviceID string, apiKey string, client *http.Client, - start time.Time, + botStartTime time.Time, version string, goos string, goarch string, @@ -136,13 +137,13 @@ func MakeMetricsTracker( } return &MetricsTracker{ - client: client, - apiKey: apiKey, - userID: userID, - deviceID: deviceID, - props: props, - start: start, - isDisabled: isDisabled, + client: client, + apiKey: apiKey, + userID: userID, + deviceID: deviceID, + props: props, + botStartTime: botStartTime, + isDisabled: isDisabled, }, nil } @@ -154,7 +155,7 @@ func (mt *MetricsTracker) SendStartupEvent() error { // SendUpdateEvent sends the update Amplitude event. func (mt *MetricsTracker) SendUpdateEvent(now time.Time, success bool, millisForUpdate int64) error { commonProps := mt.props - commonProps.SecondsSinceStart = now.Sub(mt.start).Seconds() + commonProps.SecondsSinceStart = now.Sub(mt.botStartTime).Seconds() updateProps := updateProps{ commonProps: commonProps, Success: success, @@ -188,7 +189,7 @@ func (mt *MetricsTracker) sendEvent(eventType string, eventProps interface{}) er ApiKey: mt.apiKey, Events: []event{{ UserID: mt.userID, - SessionID: mt.start.Unix() * 1000, // convert to millis based on docs + SessionID: mt.botStartTime.Unix() * 1000, // convert to millis based on docs DeviceID: mt.deviceID, EventType: eventType, Props: eventProps, diff --git a/trader/trader.go b/trader/trader.go index 0d0d2c530..c5a82712f 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -118,23 +118,20 @@ func MakeTrader( // Start starts the bot with the injected strategy func (t *Trader) Start() { log.Println("----------------------------------------------------------------------------------------------------") - var ( - lastUpdateTime time.Time - lastMetricUpdateTime time.Time - ) + var lastUpdateTime time.Time for { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - if shouldSendUpdateMetric(t.startTime, currentUpdateTime, lastMetricUpdateTime) { + if shouldSendUpdateMetric(t.startTime, currentUpdateTime, t.lastMetricUpdateTime) { millisForUpdate := time.Since(currentUpdateTime).Milliseconds() e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) if e != nil { log.Printf("failed to send update event metric: %s", e) } - lastMetricUpdateTime = currentUpdateTime + t.lastMetricUpdateTime = currentUpdateTime }, nil) if e != nil { log.Printf("failed to trigger goroutine for send update event: %s", e) From d226f08ab872266835635afe115188fc4cd2a1f9 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 15:01:38 -0700 Subject: [PATCH 5/8] Fix delete start time. --- support/metrics/metricsTracker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support/metrics/metricsTracker.go b/support/metrics/metricsTracker.go index 26f05d890..fea4a0314 100644 --- a/support/metrics/metricsTracker.go +++ b/support/metrics/metricsTracker.go @@ -167,7 +167,7 @@ func (mt *MetricsTracker) SendUpdateEvent(now time.Time, success bool, millisFor // SendDeleteEvent sends the delete Amplitude event. func (mt *MetricsTracker) SendDeleteEvent(exit bool) error { commonProps := mt.props - commonProps.SecondsSinceStart = time.Now().Sub(mt.start).Seconds() + commonProps.SecondsSinceStart = time.Now().Sub(mt.botStartTime).Seconds() deleteProps := deleteProps{ commonProps: commonProps, Exit: exit, From bc43406451c7ac13abc2b6cf42b6be0e51c09a55 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 15:05:10 -0700 Subject: [PATCH 6/8] Fix exporting. --- support/metrics/metricsTracker.go | 10 +++++----- trader/trader.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/support/metrics/metricsTracker.go b/support/metrics/metricsTracker.go index fea4a0314..260a73fb6 100644 --- a/support/metrics/metricsTracker.go +++ b/support/metrics/metricsTracker.go @@ -31,7 +31,7 @@ type MetricsTracker struct { props commonProps botStartTime time.Time isDisabled bool - lastMetricUpdateTime time.Time + LastMetricUpdateTime time.Time } // TODO DS Investigate other fields to add to this top-level event. @@ -178,10 +178,10 @@ func (mt *MetricsTracker) SendDeleteEvent(exit bool) error { } func (mt *MetricsTracker) sendEvent(eventType string, eventProps interface{}) error { - if mt.apiKey == "" || mt.userID == "-1" || mt.isDisabled { - log.Printf("metric - not sending event metric of type '%s' because metrics are disabled", eventType) - return nil - } + // if mt.apiKey == "" || mt.userID == "-1" || mt.isDisabled { + // log.Printf("metric - not sending event metric of type '%s' because metrics are disabled", eventType) + // return nil + // } // session_id is the start time of the session in milliseconds since epoch (Unix Timestamp), // necessary to associate events with a particular system (taken from amplitude docs) diff --git a/trader/trader.go b/trader/trader.go index c5a82712f..8528a7fe5 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -124,14 +124,14 @@ func (t *Trader) Start() { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - if shouldSendUpdateMetric(t.startTime, currentUpdateTime, t.lastMetricUpdateTime) { + if shouldSendUpdateMetric(t.startTime, currentUpdateTime, t.metricsTracker.LastMetricUpdateTime) { millisForUpdate := time.Since(currentUpdateTime).Milliseconds() e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) if e != nil { log.Printf("failed to send update event metric: %s", e) } - t.lastMetricUpdateTime = currentUpdateTime + t.metricsTracker.LastMetricUpdateTime = currentUpdateTime }, nil) if e != nil { log.Printf("failed to trigger goroutine for send update event: %s", e) From e662b7bc3b62e4cb356c1984b011353f1c793133 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 15:06:08 -0700 Subject: [PATCH 7/8] Remove debug. --- support/metrics/metricsTracker.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/support/metrics/metricsTracker.go b/support/metrics/metricsTracker.go index 260a73fb6..df50e95a3 100644 --- a/support/metrics/metricsTracker.go +++ b/support/metrics/metricsTracker.go @@ -178,10 +178,10 @@ func (mt *MetricsTracker) SendDeleteEvent(exit bool) error { } func (mt *MetricsTracker) sendEvent(eventType string, eventProps interface{}) error { - // if mt.apiKey == "" || mt.userID == "-1" || mt.isDisabled { - // log.Printf("metric - not sending event metric of type '%s' because metrics are disabled", eventType) - // return nil - // } + if mt.apiKey == "" || mt.userID == "-1" || mt.isDisabled { + log.Printf("metric - not sending event metric of type '%s' because metrics are disabled", eventType) + return nil + } // session_id is the start time of the session in milliseconds since epoch (Unix Timestamp), // necessary to associate events with a particular system (taken from amplitude docs) From b4ff414dd4aa16a0d8496c32a65f6f0972c37639 Mon Sep 17 00:00:00 2001 From: Debnil Sur Date: Fri, 16 Oct 2020 16:30:35 -0700 Subject: [PATCH 8/8] Change update time pattern. --- support/metrics/metricsTracker.go | 29 ++++++++++++++++++++--------- trader/trader.go | 3 +-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/support/metrics/metricsTracker.go b/support/metrics/metricsTracker.go index df50e95a3..5ebd4d48d 100644 --- a/support/metrics/metricsTracker.go +++ b/support/metrics/metricsTracker.go @@ -24,14 +24,14 @@ const ( // and can be used to directly send events to the // Amplitude HTTP API. type MetricsTracker struct { - client *http.Client - apiKey string - userID string - deviceID string - props commonProps - botStartTime time.Time - isDisabled bool - LastMetricUpdateTime time.Time + client *http.Client + apiKey string + userID string + deviceID string + props commonProps + botStartTime time.Time + isDisabled bool + updateEventSentTime time.Time } // TODO DS Investigate other fields to add to this top-level event. @@ -147,6 +147,11 @@ func MakeMetricsTracker( }, nil } +// GetUpdateEventSentTime gets the last sent time of the update event. +func (mt *MetricsTracker) GetUpdateEventSentTime() time.Time { + return mt.updateEventSentTime +} + // SendStartupEvent sends the startup Amplitude event. func (mt *MetricsTracker) SendStartupEvent() error { return mt.sendEvent(startupEventName, mt.props) @@ -161,7 +166,13 @@ func (mt *MetricsTracker) SendUpdateEvent(now time.Time, success bool, millisFor Success: success, MillisForUpdate: millisForUpdate, } - return mt.sendEvent(updateEventName, updateProps) + e := mt.sendEvent(updateEventName, updateProps) + if e != nil { + return fmt.Errorf("could not send update event: %s", e) + } + + mt.updateEventSentTime = now + return nil } // SendDeleteEvent sends the delete Amplitude event. diff --git a/trader/trader.go b/trader/trader.go index 8528a7fe5..41192fe7d 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -124,14 +124,13 @@ func (t *Trader) Start() { currentUpdateTime := time.Now() if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) { success := t.update() - if shouldSendUpdateMetric(t.startTime, currentUpdateTime, t.metricsTracker.LastMetricUpdateTime) { + if shouldSendUpdateMetric(t.startTime, currentUpdateTime, t.metricsTracker.GetUpdateEventSentTime()) { millisForUpdate := time.Since(currentUpdateTime).Milliseconds() e := t.threadTracker.TriggerGoroutine(func(inputs []interface{}) { e := t.metricsTracker.SendUpdateEvent(currentUpdateTime, success, millisForUpdate) if e != nil { log.Printf("failed to send update event metric: %s", e) } - t.metricsTracker.LastMetricUpdateTime = currentUpdateTime }, nil) if e != nil { log.Printf("failed to trigger goroutine for send update event: %s", e)