From 677404f317323bc3b26d975c77e6069c24d99bb4 Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:49:43 -0800 Subject: [PATCH] Revert "remove mongodb" This reverts commit 840316dc2cf5b544ab62a5285b58c9f90878d874. --- api.go | 8 +++++++- go.mod | 1 + go.sum | 4 ++++ portfolio.go | 3 ++- returns/list.go | 4 ++-- returns/table.go | 37 +++++++++++++++++++++++++++++++++---- 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 5cf7966..4fb341d 100644 --- a/api.go +++ b/api.go @@ -11,6 +11,8 @@ import ( "os" "strings" + "go.mongodb.org/mongo-driver/bson/primitive" + "github.com/portfoliotree/portfolio/returns" ) @@ -64,7 +66,11 @@ func ParseComponentsFromURL(values url.Values, prefix string) ([]Component, erro } components := make([]Component, 0, len(assetValues)) for _, v := range assetValues { - components = append(components, Component{ID: v}) + if _, err := primitive.ObjectIDFromHex(v); err == nil { + components = append(components, Component{Type: "Portfolio", ID: v}) + continue + } + components = append(components, Component{Type: "Security", ID: v}) } return components, nil } diff --git a/go.mod b/go.mod index 13f3b6f..686818d 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ toolchain go1.23.3 require ( github.com/portfoliotree/round v0.1.0 github.com/stretchr/testify v1.10.0 + go.mongodb.org/mongo-driver v1.17.1 gonum.org/v1/gonum v0.15.1 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index fdf0baf..2c78435 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -13,6 +15,8 @@ github.com/portfoliotree/round v0.1.0 h1:cgwCj64CUk262Cga+kQLFJEEnplebSeCwnlDtA3 github.com/portfoliotree/round v0.1.0/go.mod h1:sm64uU9te4vt/uRZi5Ag3LFVQtc7isrrYr2ZKWi147M= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM= +go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4= golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo= golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c= golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8= diff --git a/portfolio.go b/portfolio.go index f587708..87af0bd 100644 --- a/portfolio.go +++ b/portfolio.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "go.mongodb.org/mongo-driver/bson/primitive" "gopkg.in/yaml.v3" "github.com/portfoliotree/portfolio/allocation" @@ -17,7 +18,7 @@ import ( "github.com/portfoliotree/portfolio/returns" ) -type Identifier = string +type Identifier = primitive.ObjectID type Document struct { ID Identifier `json:"_id" yaml:"_id" bson:"_id"` diff --git a/returns/list.go b/returns/list.go index b2eeaac..bd14e18 100644 --- a/returns/list.go +++ b/returns/list.go @@ -166,9 +166,9 @@ func indexOfClosest[T any](list []T, time func(T) time.Time, t time.Time) int { if len(list) == 1 { return 1 } - return index + indexOfClosest[T](list[index:], time, t) + return index + indexOfClosest(list[index:], time, t) } - return indexOfClosest[T](list[:index], time, t) + return indexOfClosest(list[:index], time, t) } func inBounds[T any](list []T, index int) bool { diff --git a/returns/table.go b/returns/table.go index b34b487..ace1aba 100644 --- a/returns/table.go +++ b/returns/table.go @@ -11,6 +11,8 @@ import ( "strconv" "time" + "go.mongodb.org/mongo-driver/bson" + "github.com/portfoliotree/round" "github.com/portfoliotree/portfolio/calculate" @@ -36,9 +38,21 @@ func NewTable(list []List) Table { return table } +func (table *Table) UnmarshalBSON(buf []byte) error { + var enc encodedTable + err := bson.Unmarshal(buf, &enc) + table.times = enc.Times + table.values = enc.Values + return err +} + +func (table Table) MarshalBSON() ([]byte, error) { + return bson.Marshal(newEncodedTable(table.times, table.values)) +} + type encodedTable struct { - Times []time.Time `json:"times"` - Values [][]float64 `json:"values"` + Times []time.Time `json:"times" bson:"times"` + Values [][]float64 `json:"values" bson:"values"` } func newEncodedTable(times []time.Time, values [][]float64) encodedTable { @@ -464,8 +478,23 @@ func (table Table) RangeIndexes(last, first time.Time) (end int, start int) { } type encodedColumnGroup struct { - Index int `json:"index"` - Length int `json:"length"` + Index int `json:"index" bson:"index"` + Length int `json:"length" bson:"length"` +} + +func (group *ColumnGroup) UnmarshalBSON(buf []byte) error { + var ecg encodedColumnGroup + err := bson.Unmarshal(buf, &ecg) + group.index = ecg.Index + group.length = ecg.Length + return err +} + +func (group ColumnGroup) MarshalBSON() ([]byte, error) { + return bson.Marshal(encodedColumnGroup{ + Index: group.index, + Length: group.length, + }) } func (group *ColumnGroup) UnmarshalJSON(buf []byte) error {