Include latest and earliest record times in state #34

Merged
natercio merged 2 commits from state-time-boundaries into main 2026-10-12 23:09:40 +01:00
2 changed files with 22 additions and 6 deletions
Showing only changes of commit 79151422b0 - Show all commits

No files matched your search

+19 -6
View File
@@ -96,11 +96,9 @@ func (fs *JSONStore) Load(ctx context.Context) (map[string]*FillerQueue, error)
// the same directory followed by an atomic rename so a crash mid-write cannot // the same directory followed by an atomic rename so a crash mid-write cannot
// leave a half-written state file. // leave a half-written state file.
func (fs *JSONStore) Save(ctx context.Context, queue map[string]*FillerQueue) error { func (fs *JSONStore) Save(ctx context.Context, queue map[string]*FillerQueue) error {
state := State{ queues := make(map[string][]persistedFiller)
Version: StateVersion, latest := time.Time{}
Platform: fs.platform, earliest := time.Time{}
Queues: make(map[string][]persistedFiller),
}
for symbol, q := range queue { for symbol, q := range queue {
if q == nil || q.Len() == 0 { if q == nil || q.Len() == 0 {
@@ -119,8 +117,23 @@ func (fs *JSONStore) Save(ctx context.Context, queue map[string]*FillerQueue) er
Price: f.Price(), Price: f.Price(),
Filled: f.Filled(), Filled: f.Filled(),
}) })
if f.Timestamp().After(latest) {
latest = f.Timestamp()
}
if f.Timestamp().Before(earliest) || earliest.IsZero() {
earliest = f.Timestamp()
}
} }
state.Queues[symbol] = persisted queues[symbol] = persisted
}
state := State{
Version: StateVersion,
Platform: fs.platform,
Queues: queues,
Latest: latest,
Earliest: earliest,
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
+3
View File
@@ -3,6 +3,7 @@ package internal
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"time"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
@@ -15,6 +16,8 @@ const StateVersion = "2"
type State struct { type State struct {
Version string `json:"version"` Version string `json:"version"`
Platform string `json:"platform"` Platform string `json:"platform"`
Latest time.Time `json:"latest"`
Earliest time.Time `json:"earliest"`
Queues map[string][]persistedFiller `json:"queues"` Queues map[string][]persistedFiller `json:"queues"`
} }