From 5eddcf6f3defd76f91df5a6bbaadf7d9136a9245 Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Mon, 6 Feb 2023 14:47:45 +0100 Subject: [PATCH] Add total calculation to new priceentry --- src/serra/stats.go | 4 +++ src/serra/storage.go | 10 ++------ src/serra/update.go | 58 ++++++++++++++++++++------------------------ 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/serra/stats.go b/src/serra/stats.go index bd844f4..51cba3e 100644 --- a/src/serra/stats.go +++ b/src/serra/stats.go @@ -52,12 +52,16 @@ var statsCmd = &cobra.Command{ {"_id", nil}, {"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}}, {"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}}, + {"count_foil", bson.D{{"$sum", "$serra_count_foil"}}}, + {"count_etched", bson.D{{"$sum", "$serra_count_etched"}}}, {"rarity", bson.D{{"$sum", "$rarity"}}}, {"unique", bson.D{{"$sum", 1}}}, }}}, }) fmt.Printf("\n%sCards %s\n", Green, Reset) fmt.Printf("Total Cards: %s%.0f%s\n", Yellow, stats[0]["count"], Reset) + fmt.Printf("Total Foil Cards: %s%.0f%s\n", Purple, stats[0]["count_foil"], Reset) + fmt.Printf("Total Etched Cards: %s%.0f%s\n", Purple, stats[0]["count_foil"], Reset) fmt.Printf("Unique Cards: %s%d%s\n", Purple, stats[0]["unique"], Reset) rar, _ := coll.storage_aggregate(mongo.Pipeline{ diff --git a/src/serra/storage.go b/src/serra/storage.go index aa9d1da..e5f745e 100644 --- a/src/serra/storage.go +++ b/src/serra/storage.go @@ -61,20 +61,14 @@ func (coll Collection) storage_add_set(set *Set) (*mongo.InsertOneResult, error) } -func (coll Collection) storage_add_total(v float64) error { +func (coll Collection) storage_add_total(p PriceEntry) error { // create total object if not exists... coll.InsertOne(context.TODO(), Total{ID: "1", Value: []PriceEntry{}}) // update object as intended... filter := bson.D{{"_id", "1"}} - update := bson.M{ - "$push": bson.M{"value": bson.M{ - "date": primitive.NewDateTimeFromTime(time.Now()), - "value": v, - }, - }, - } + update := bson.M{"$push": bson.M{"value": p}} _, err := coll.UpdateOne( context.Background(), diff --git a/src/serra/update.go b/src/serra/update.go index cd13802..d50aa9d 100644 --- a/src/serra/update.go +++ b/src/serra/update.go @@ -2,7 +2,6 @@ package serra import ( "fmt" - "os" "time" "github.com/mitchellh/mapstructure" @@ -33,6 +32,23 @@ var updateCmd = &cobra.Command{ coll := &Collection{client.Database("serra").Collection("cards")} totalcoll := &Collection{client.Database("serra").Collection("total")} + 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", ""}, + {"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"}}}}}}, + {"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"}}}}}}, + }}} + sets, _ := fetch_sets() for _, set := range sets.Data { @@ -82,25 +98,7 @@ var updateCmd = &cobra.Command{ // update set value sum // calculate value summary - matchStage := bson.D{ - {"$match", bson.D{ - {"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{{"$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"}}}}}}, - }}} + matchStage := bson.D{{"$match", bson.D{{"set", set.Code}}}} setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, projectStage, groupStage}) p := PriceEntry{} @@ -120,19 +118,15 @@ var updateCmd = &cobra.Command{ setscoll.storage_update(bson.M{"code": bson.M{"$eq": set.Code}}, set_update) } - os.Exit(1) + totalvalue, _ := coll.storage_aggregate(mongo.Pipeline{projectStage, groupStage}) - // calculate total summary over all sets - overall_value := mongo.Pipeline{ - bson.D{{"$match", - bson.D{{"serra_prices", bson.D{{"$type", "array"}}}}}}, - bson.D{{"$project", - bson.D{{"name", true}, {"totalValue", bson.D{{"$arrayElemAt", bson.A{"$serra_prices", -1}}}}}}}, - bson.D{{"$group", bson.D{{"_id", nil}, {"total", bson.D{{"$sum", "$totalValue.value"}}}}}}, - } - ostats, _ := setscoll.storage_aggregate(overall_value) - fmt.Printf("\n%sUpdating total value of collection to: %s%.02f EUR%s\n", Green, Yellow, ostats[0]["total"].(float64), Reset) - totalcoll.storage_add_total(ostats[0]["total"].(float64)) + t := PriceEntry{} + t.Date = primitive.NewDateTimeFromTime(time.Now()) + mapstructure.Decode(totalvalue[0], &t) + + // TODO: eur value chooser + fmt.Printf("\n%sUpdating total value of collection to: %s%.02f EUR%s\n", Green, Yellow, totalvalue[0]["eur"], Reset) + totalcoll.storage_add_total(t) return nil },