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
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

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/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=

View File

@ -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)
}

View File

@ -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

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) {
// 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)
}

View File

@ -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{