diff --git a/go.mod b/go.mod index 5f34745..c355402 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/noqqe/serra go 1.14 require ( + github.com/mitchellh/mapstructure v1.4.3 github.com/schollz/progressbar/v3 v3.8.5 github.com/spf13/cobra v1.3.0 go.mongodb.org/mongo-driver v1.8.1 diff --git a/go.sum b/go.sum index bdfd9d6..1ac8c9a 100644 --- a/go.sum +++ b/go.sum @@ -276,6 +276,7 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/src/serra/helpers.go b/src/serra/helpers.go index d4462f6..b781b86 100644 --- a/src/serra/helpers.go +++ b/src/serra/helpers.go @@ -178,13 +178,3 @@ func print_price_history(prices []PriceEntry, prefix string) { before = value } } - -// Sometimes its exhausting dealing with those types... -// Usecase: price can be 3.14 or nil -func toFloat(p interface{}) float64 { - fmt.Printf("%v\n", p) - if p != nil { - return float64(p.(float64)) - } - return float64(0) -} diff --git a/src/serra/scryfall.go b/src/serra/scryfall.go index ef180dc..79f0839 100644 --- a/src/serra/scryfall.go +++ b/src/serra/scryfall.go @@ -121,12 +121,12 @@ type Card struct { type PriceEntry struct { Date primitive.DateTime `bson:"date"` - Eur float64 `json:"eur,string"` - EurFoil float64 `json:"eur_foil,string"` - Tix float64 `json:"tix,string"` - Usd float64 `json:"usd,string"` - UsdEtched float64 `json:"usd_etched,string"` - UsdFoil float64 `json:"usd_foil,string"` + Eur float64 `json:"eur,string" bson:"eur,float64"` + EurFoil float64 `json:"eur_foil,string" bson:"eur_foil,float64"` + Tix float64 `json:"tix,string" bson:"tix,float64"` + Usd float64 `json:"usd,string" bson:"usd,float64"` + UsdEtched float64 `json:"usd_etched,string" bson:"usd_etched,float64"` + UsdFoil float64 `json:"usd_foil,string" bson:"usd_foil,float64"` } // Sets diff --git a/src/serra/storage.go b/src/serra/storage.go index 1aeac85..aa9d1da 100644 --- a/src/serra/storage.go +++ b/src/serra/storage.go @@ -150,13 +150,13 @@ func (coll Collection) storage_remove(filter bson.M) error { func (coll Collection) storage_aggregate(pipeline mongo.Pipeline) ([]primitive.M, error) { - // db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}]) - opts := options.Aggregate().SetMaxTime(2 * time.Second) + opts := options.Aggregate() cursor, err := coll.Aggregate( context.TODO(), pipeline, opts) if err != nil { + LogMessage(fmt.Sprintf("%v", err), "red") LogMessage("Could not aggregate data due to connection errors to database", "red") os.Exit(1) } diff --git a/src/serra/update.go b/src/serra/update.go index 042da53..cd13802 100644 --- a/src/serra/update.go +++ b/src/serra/update.go @@ -5,6 +5,7 @@ import ( "os" "time" + "github.com/mitchellh/mapstructure" "github.com/schollz/progressbar/v3" "github.com/spf13/cobra" "go.mongodb.org/mongo-driver/bson" @@ -83,38 +84,32 @@ var updateCmd = &cobra.Command{ // calculate value summary matchStage := bson.D{ {"$match", bson.D{ - {"set", set.Code}, - }}, - } - // Eur float64 `json:"eur,string"` - // EurFoil float64 `json:"eur_foil,string"` - // Tix float64 `json:"tix,string"` - // Usd float64 `json:"usd,string"` - // UsdEtched float64 `json:"usd_etched,string"` - // UsdFoil float64 `json:"usd_foil,string"` + {"set", set.Code}}}} + projectStage := bson.D{{"$project", + bson.D{ + {"serra_count", true}, + {"serra_count_foil", true}, + {"serra_count_etched", true}, + {"set", true}, + {"last_price", bson.D{{"$arrayElemAt", bson.A{"$serra_prices", -1}}}}}}} groupStage := bson.D{ {"$group", bson.D{ {"_id", "$set"}, - {"eur", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}}}}, - {"eurfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eurfoil", "$serra_count_foil"}}}}}}}}, - {"usd", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}}, - {"usdetched", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usdetched", "$serra_count_etched"}}}}}}}}, - {"usdfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}}, + {"eur", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.eur", "$serra_count"}}}}}}, + {"eurfoil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.eur_foil", "$serra_count_foil"}}}}}}, + {"usd", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.usd", "$serra_count_foil"}}}}}}, + {"usdetched", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.usd_etched", "$serra_count_etched"}}}}}}, + {"usdfoil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.usd_foil", "$serra_count_foil"}}}}}}, }}} - setvalue, err := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage}) - if err != nil { - LogMessage(fmt.Sprintf("%s", err), "red") - } + setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, projectStage, groupStage}) p := PriceEntry{} s := setvalue[0] p.Date = primitive.NewDateTimeFromTime(time.Now()) - p.Eur = toFloat(s["eur"]) - p.EurFoil = toFloat(s["eurfoil"]) - p.Usd = toFloat(s["usd"]) - p.UsdEtched = toFloat(s["usdetched"]) - p.UsdFoil = toFloat(s["usdfoil"]) + + // fill struct PriceEntry with map from mongoresult + mapstructure.Decode(s, &p) // do the update set_update := bson.M{