Calculate Set Prices with currency works

This commit is contained in:
Florian Baumann 2023-02-06 13:23:39 +01:00
parent 292a9da383
commit 5e1291bc74
6 changed files with 28 additions and 41 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/noqqe/serra
go 1.14 go 1.14
require ( require (
github.com/mitchellh/mapstructure v1.4.3
github.com/schollz/progressbar/v3 v3.8.5 github.com/schollz/progressbar/v3 v3.8.5
github.com/spf13/cobra v1.3.0 github.com/spf13/cobra v1.3.0
go.mongodb.org/mongo-driver v1.8.1 go.mongodb.org/mongo-driver v1.8.1

1
go.sum
View File

@ -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/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 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.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/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-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

View File

@ -178,13 +178,3 @@ func print_price_history(prices []PriceEntry, prefix string) {
before = value 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)
}

View File

@ -121,12 +121,12 @@ type Card struct {
type PriceEntry struct { type PriceEntry struct {
Date primitive.DateTime `bson:"date"` Date primitive.DateTime `bson:"date"`
Eur float64 `json:"eur,string"` Eur float64 `json:"eur,string" bson:"eur,float64"`
EurFoil float64 `json:"eur_foil,string"` EurFoil float64 `json:"eur_foil,string" bson:"eur_foil,float64"`
Tix float64 `json:"tix,string"` Tix float64 `json:"tix,string" bson:"tix,float64"`
Usd float64 `json:"usd,string"` Usd float64 `json:"usd,string" bson:"usd,float64"`
UsdEtched float64 `json:"usd_etched,string"` UsdEtched float64 `json:"usd_etched,string" bson:"usd_etched,float64"`
UsdFoil float64 `json:"usd_foil,string"` UsdFoil float64 `json:"usd_foil,string" bson:"usd_foil,float64"`
} }
// Sets // Sets

View File

@ -150,13 +150,13 @@ func (coll Collection) storage_remove(filter bson.M) error {
func (coll Collection) storage_aggregate(pipeline mongo.Pipeline) ([]primitive.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()
opts := options.Aggregate().SetMaxTime(2 * time.Second)
cursor, err := coll.Aggregate( cursor, err := coll.Aggregate(
context.TODO(), context.TODO(),
pipeline, pipeline,
opts) opts)
if err != nil { if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
LogMessage("Could not aggregate data due to connection errors to database", "red") LogMessage("Could not aggregate data due to connection errors to database", "red")
os.Exit(1) os.Exit(1)
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"time" "time"
"github.com/mitchellh/mapstructure"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -83,38 +84,32 @@ var updateCmd = &cobra.Command{
// calculate value summary // calculate value summary
matchStage := bson.D{ matchStage := bson.D{
{"$match", bson.D{ {"$match", bson.D{
{"set", set.Code}, {"set", set.Code}}}}
}}, projectStage := bson.D{{"$project",
} bson.D{
// Eur float64 `json:"eur,string"` {"serra_count", true},
// EurFoil float64 `json:"eur_foil,string"` {"serra_count_foil", true},
// Tix float64 `json:"tix,string"` {"serra_count_etched", true},
// Usd float64 `json:"usd,string"` {"set", true},
// UsdEtched float64 `json:"usd_etched,string"` {"last_price", bson.D{{"$arrayElemAt", bson.A{"$serra_prices", -1}}}}}}}
// UsdFoil float64 `json:"usd_foil,string"`
groupStage := bson.D{ groupStage := bson.D{
{"$group", bson.D{ {"$group", bson.D{
{"_id", "$set"}, {"_id", "$set"},
{"eur", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}}}}, {"eur", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.eur", "$serra_count"}}}}}},
{"eurfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eurfoil", "$serra_count_foil"}}}}}}}}, {"eurfoil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.eur_foil", "$serra_count_foil"}}}}}},
{"usd", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}}, {"usd", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.usd", "$serra_count_foil"}}}}}},
{"usdetched", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usdetched", "$serra_count_etched"}}}}}}}}, {"usdetched", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$last_price.usd_etched", "$serra_count_etched"}}}}}},
{"usdfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}}, {"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}) setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, projectStage, groupStage})
if err != nil {
LogMessage(fmt.Sprintf("%s", err), "red")
}
p := PriceEntry{} p := PriceEntry{}
s := setvalue[0] s := setvalue[0]
p.Date = primitive.NewDateTimeFromTime(time.Now()) p.Date = primitive.NewDateTimeFromTime(time.Now())
p.Eur = toFloat(s["eur"])
p.EurFoil = toFloat(s["eurfoil"]) // fill struct PriceEntry with map from mongoresult
p.Usd = toFloat(s["usd"]) mapstructure.Decode(s, &p)
p.UsdEtched = toFloat(s["usdetched"])
p.UsdFoil = toFloat(s["usdfoil"])
// do the update // do the update
set_update := bson.M{ set_update := bson.M{