Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Send update metric at certain intervals (closes #516) #536

Merged
merged 8 commits into from
Oct 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add lastMetricUpdateTime to struct.
debnil committed Oct 16, 2020

Verified

This commit was signed with the committer’s verified signature. The key has expired.
debnil Debnil Sur
commit cbf7d4369bf2531ea243cbfd6e042310930d1422
35 changes: 18 additions & 17 deletions support/metrics/metricsTracker.go
Original file line number Diff line number Diff line change
@@ -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,
9 changes: 3 additions & 6 deletions trader/trader.go
Original file line number Diff line number Diff line change
@@ -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)