Compare commits
4 Commits
0b15a339e0
...
791306acf1
| Author | SHA1 | Date | |
|---|---|---|---|
| 791306acf1 | |||
| f3d0f5d71a | |||
| 54fced39aa | |||
| c477023041 |
@@ -14,6 +14,11 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: 1.25
|
||||
|
||||
- name: Save pre-generate git state
|
||||
run: git status --porcelain > pre-generate.txt
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"git.naterciomoniz.net/applications/broker2anexoj/internal"
|
||||
"git.naterciomoniz.net/applications/broker2anexoj/internal/trading212"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := run()
|
||||
err := run(context.Background())
|
||||
if err != nil {
|
||||
slog.Error("found a fatal issue", slog.Any("err", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
func run(ctx context.Context) error {
|
||||
ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
f, err := os.Open("test.csv")
|
||||
if err != nil {
|
||||
return fmt.Errorf("open statement: %w", err)
|
||||
@@ -27,7 +35,11 @@ func run() error {
|
||||
|
||||
writer := internal.NewStdOutLogger()
|
||||
|
||||
err = internal.BuildReport(reader, writer)
|
||||
eg.Go(func() error {
|
||||
return internal.BuildReport(ctx, reader, writer)
|
||||
})
|
||||
|
||||
err = eg.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
10
go.sum
10
go.sum
@@ -1,3 +1,11 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
|
||||
@@ -6,3 +14,5 @@ golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
||||
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
|
||||
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
context "context"
|
||||
big "math/big"
|
||||
reflect "reflect"
|
||||
time "time"
|
||||
@@ -43,18 +44,18 @@ func (m *MockRecordReader) EXPECT() *MockRecordReaderMockRecorder {
|
||||
}
|
||||
|
||||
// ReadRecord mocks base method.
|
||||
func (m *MockRecordReader) ReadRecord() (internal.Record, error) {
|
||||
func (m *MockRecordReader) ReadRecord(arg0 context.Context) (internal.Record, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ReadRecord")
|
||||
ret := m.ctrl.Call(m, "ReadRecord", arg0)
|
||||
ret0, _ := ret[0].(internal.Record)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ReadRecord indicates an expected call of ReadRecord.
|
||||
func (mr *MockRecordReaderMockRecorder) ReadRecord() *MockRecordReaderReadRecordCall {
|
||||
func (mr *MockRecordReaderMockRecorder) ReadRecord(arg0 any) *MockRecordReaderReadRecordCall {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadRecord", reflect.TypeOf((*MockRecordReader)(nil).ReadRecord))
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadRecord", reflect.TypeOf((*MockRecordReader)(nil).ReadRecord), arg0)
|
||||
return &MockRecordReaderReadRecordCall{Call: call}
|
||||
}
|
||||
|
||||
@@ -70,13 +71,13 @@ func (c *MockRecordReaderReadRecordCall) Return(arg0 internal.Record, arg1 error
|
||||
}
|
||||
|
||||
// Do rewrite *gomock.Call.Do
|
||||
func (c *MockRecordReaderReadRecordCall) Do(f func() (internal.Record, error)) *MockRecordReaderReadRecordCall {
|
||||
func (c *MockRecordReaderReadRecordCall) Do(f func(context.Context) (internal.Record, error)) *MockRecordReaderReadRecordCall {
|
||||
c.Call = c.Call.Do(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||
func (c *MockRecordReaderReadRecordCall) DoAndReturn(f func() (internal.Record, error)) *MockRecordReaderReadRecordCall {
|
||||
func (c *MockRecordReaderReadRecordCall) DoAndReturn(f func(context.Context) (internal.Record, error)) *MockRecordReaderReadRecordCall {
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
@@ -396,17 +397,17 @@ func (m *MockReportWriter) EXPECT() *MockReportWriterMockRecorder {
|
||||
}
|
||||
|
||||
// Write mocks base method.
|
||||
func (m *MockReportWriter) Write(arg0 internal.ReportItem) error {
|
||||
func (m *MockReportWriter) Write(arg0 context.Context, arg1 internal.ReportItem) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Write", arg0)
|
||||
ret := m.ctrl.Call(m, "Write", arg0, arg1)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Write indicates an expected call of Write.
|
||||
func (mr *MockReportWriterMockRecorder) Write(arg0 any) *MockReportWriterWriteCall {
|
||||
func (mr *MockReportWriterMockRecorder) Write(arg0, arg1 any) *MockReportWriterWriteCall {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockReportWriter)(nil).Write), arg0)
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockReportWriter)(nil).Write), arg0, arg1)
|
||||
return &MockReportWriterWriteCall{Call: call}
|
||||
}
|
||||
|
||||
@@ -422,13 +423,13 @@ func (c *MockReportWriterWriteCall) Return(arg0 error) *MockReportWriterWriteCal
|
||||
}
|
||||
|
||||
// Do rewrite *gomock.Call.Do
|
||||
func (c *MockReportWriterWriteCall) Do(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
func (c *MockReportWriterWriteCall) Do(f func(context.Context, internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
c.Call = c.Call.Do(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||
func (c *MockReportWriterWriteCall) DoAndReturn(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
func (c *MockReportWriterWriteCall) DoAndReturn(f func(context.Context, internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -10,40 +11,45 @@ import (
|
||||
|
||||
type RecordReader interface {
|
||||
// ReadRecord should return Records until an error is found.
|
||||
ReadRecord() (Record, error)
|
||||
ReadRecord(context.Context) (Record, error)
|
||||
}
|
||||
|
||||
type ReportWriter interface {
|
||||
// ReportWriter writes report items
|
||||
Write(ReportItem) error
|
||||
Write(context.Context, ReportItem) error
|
||||
}
|
||||
|
||||
func BuildReport(reader RecordReader, writer ReportWriter) error {
|
||||
func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter) error {
|
||||
buys := make(map[string]*RecordQueue)
|
||||
|
||||
for {
|
||||
rec, err := reader.ReadRecord()
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
rec, err := reader.ReadRecord(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
buyQueue, ok := buys[rec.Symbol()]
|
||||
if !ok {
|
||||
buyQueue = new(RecordQueue)
|
||||
buys[rec.Symbol()] = buyQueue
|
||||
}
|
||||
buyQueue, ok := buys[rec.Symbol()]
|
||||
if !ok {
|
||||
buyQueue = new(RecordQueue)
|
||||
buys[rec.Symbol()] = buyQueue
|
||||
}
|
||||
|
||||
err = processRecord(buyQueue, rec, writer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("processing record: %w", err)
|
||||
err = processRecord(ctx, buyQueue, rec, writer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("processing record: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func processRecord(q *RecordQueue, rec Record, writer ReportWriter) error {
|
||||
func processRecord(ctx context.Context, q *RecordQueue, rec Record, writer ReportWriter) error {
|
||||
switch rec.Side() {
|
||||
case SideBuy:
|
||||
q.Push(rec)
|
||||
@@ -72,7 +78,7 @@ func processRecord(q *RecordQueue, rec Record, writer ReportWriter) error {
|
||||
sellValue := new(big.Float).Mul(matchedQty, rec.Price())
|
||||
buyValue := new(big.Float).Mul(matchedQty, buy.Price())
|
||||
|
||||
err := writer.Write(ReportItem{
|
||||
err := writer.Write(ctx, ReportItem{
|
||||
BuyValue: buyValue,
|
||||
BuyTimestamp: buy.Timestamp(),
|
||||
SellValue: sellValue,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"math/big"
|
||||
"testing"
|
||||
@@ -20,7 +21,7 @@ func TestReporter_Run(t *testing.T) {
|
||||
mockRecord(ctrl, 20.0, 10.0, internal.SideBuy, now),
|
||||
mockRecord(ctrl, 25.0, 10.0, internal.SideSell, now.Add(1)),
|
||||
}
|
||||
reader.EXPECT().ReadRecord().DoAndReturn(func() (internal.Record, error) {
|
||||
reader.EXPECT().ReadRecord(gomock.Any()).DoAndReturn(func(ctx context.Context) (internal.Record, error) {
|
||||
if len(records) > 0 {
|
||||
r := records[0]
|
||||
records = records[1:]
|
||||
@@ -31,7 +32,7 @@ func TestReporter_Run(t *testing.T) {
|
||||
}).Times(3)
|
||||
|
||||
writer := mocks.NewMockReportWriter(ctrl)
|
||||
writer.EXPECT().Write(gomock.Eq(internal.ReportItem{
|
||||
writer.EXPECT().Write(gomock.Any(), gomock.Eq(internal.ReportItem{
|
||||
BuyValue: new(big.Float).SetFloat64(200.0),
|
||||
BuyTimestamp: now,
|
||||
SellValue: new(big.Float).SetFloat64(250.0),
|
||||
@@ -40,7 +41,7 @@ func TestReporter_Run(t *testing.T) {
|
||||
Taxes: new(big.Float),
|
||||
})).Times(1)
|
||||
|
||||
gotErr := internal.BuildReport(reader, writer)
|
||||
gotErr := internal.BuildReport(t.Context(), reader, writer)
|
||||
if gotErr != nil {
|
||||
t.Fatalf("got unexpected err: %v", gotErr)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -24,7 +25,7 @@ func NewReportLogger(w io.Writer) *ReportLogger {
|
||||
}
|
||||
}
|
||||
|
||||
func (rl *ReportLogger) Write(ri ReportItem) error {
|
||||
func (rl *ReportLogger) Write(_ context.Context, ri ReportItem) error {
|
||||
rl.counter++
|
||||
_, err := fmt.Fprintf(rl.writer, "%6d - realised %+f on %s\n", rl.counter, ri.RealisedPnL(), ri.SellTimestamp.Format(time.RFC3339))
|
||||
return err
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestReportLogger_Write(t *testing.T) {
|
||||
rw := internal.NewReportLogger(buf)
|
||||
|
||||
for _, item := range tt.items {
|
||||
err := rw.Write(item)
|
||||
err := rw.Write(t.Context(), item)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error on write: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package trading212
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -66,7 +67,7 @@ const (
|
||||
LimitSell = "limit sell"
|
||||
)
|
||||
|
||||
func (rr RecordReader) ReadRecord() (internal.Record, error) {
|
||||
func (rr RecordReader) ReadRecord(_ context.Context) (internal.Record, error) {
|
||||
for {
|
||||
raw, err := rr.reader.Read()
|
||||
if err != nil {
|
||||
|
||||
@@ -93,7 +93,7 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rr := NewRecordReader(tt.r)
|
||||
got, gotErr := rr.ReadRecord()
|
||||
got, gotErr := rr.ReadRecord(t.Context())
|
||||
if gotErr != nil {
|
||||
if !tt.wantErr {
|
||||
t.Fatalf("ReadRecord() failed: %v", gotErr)
|
||||
|
||||
Reference in New Issue
Block a user