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
Show file tree
Hide file tree
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
38 changes: 30 additions & 8 deletions trader/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved

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) {
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
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
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
}

if t.fixedIterations != nil && success {
Expand All @@ -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
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
}

// deletes all offers for the bot (not all offers on the account)
func (t *Trader) deleteAllOffers(isAsync bool) {
logPrefix := ""
Expand Down
61 changes: 61 additions & 0 deletions trader/trader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trader

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -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",
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
start: now.Add(-4 * time.Minute),
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
currentUpdate: now,
lastMetricUpdate: now.Add(-10 * time.Second),
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
wantShouldSendMetric: true,
},
{
name: "first 5 mins - no refresh",
start: now.Add(-4 * time.Minute),
currentUpdate: now,
lastMetricUpdate: now.Add(-3 * time.Second),
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
wantShouldSendMetric: false,
},
{
name: "first hour - refresh",
start: now.Add(-50 * time.Minute),
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
}