Add total calculation to new priceentry

This commit is contained in:
Florian Baumann 2023-02-06 14:47:45 +01:00
parent 5e1291bc74
commit 5eddcf6f3d
3 changed files with 32 additions and 40 deletions

View File

@ -52,12 +52,16 @@ var statsCmd = &cobra.Command{
{"_id", nil}, {"_id", nil},
{"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}}, {"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", 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"}}}, {"rarity", bson.D{{"$sum", "$rarity"}}},
{"unique", bson.D{{"$sum", 1}}}, {"unique", bson.D{{"$sum", 1}}},
}}}, }}},
}) })
fmt.Printf("\n%sCards %s\n", Green, Reset) fmt.Printf("\n%sCards %s\n", Green, Reset)
fmt.Printf("Total Cards: %s%.0f%s\n", Yellow, stats[0]["count"], 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) fmt.Printf("Unique Cards: %s%d%s\n", Purple, stats[0]["unique"], Reset)
rar, _ := coll.storage_aggregate(mongo.Pipeline{ rar, _ := coll.storage_aggregate(mongo.Pipeline{

View File

@ -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... // create total object if not exists...
coll.InsertOne(context.TODO(), Total{ID: "1", Value: []PriceEntry{}}) coll.InsertOne(context.TODO(), Total{ID: "1", Value: []PriceEntry{}})
// update object as intended... // update object as intended...
filter := bson.D{{"_id", "1"}} filter := bson.D{{"_id", "1"}}
update := bson.M{ update := bson.M{"$push": bson.M{"value": p}}
"$push": bson.M{"value": bson.M{
"date": primitive.NewDateTimeFromTime(time.Now()),
"value": v,
},
},
}
_, err := coll.UpdateOne( _, err := coll.UpdateOne(
context.Background(), context.Background(),

View File

@ -2,7 +2,6 @@ package serra
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -33,6 +32,23 @@ var updateCmd = &cobra.Command{
coll := &Collection{client.Database("serra").Collection("cards")} coll := &Collection{client.Database("serra").Collection("cards")}
totalcoll := &Collection{client.Database("serra").Collection("total")} 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() sets, _ := fetch_sets()
for _, set := range sets.Data { for _, set := range sets.Data {
@ -82,25 +98,7 @@ var updateCmd = &cobra.Command{
// update set value sum // update set value sum
// calculate value summary // calculate value summary
matchStage := bson.D{ matchStage := bson.D{{"$match", bson.D{{"set", set.Code}}}}
{"$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"}}}}}},
}}}
setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, projectStage, groupStage}) setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, projectStage, groupStage})
p := PriceEntry{} p := PriceEntry{}
@ -120,19 +118,15 @@ var updateCmd = &cobra.Command{
setscoll.storage_update(bson.M{"code": bson.M{"$eq": set.Code}}, set_update) 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 t := PriceEntry{}
overall_value := mongo.Pipeline{ t.Date = primitive.NewDateTimeFromTime(time.Now())
bson.D{{"$match", mapstructure.Decode(totalvalue[0], &t)
bson.D{{"serra_prices", bson.D{{"$type", "array"}}}}}},
bson.D{{"$project", // TODO: eur value chooser
bson.D{{"name", true}, {"totalValue", bson.D{{"$arrayElemAt", bson.A{"$serra_prices", -1}}}}}}}, fmt.Printf("\n%sUpdating total value of collection to: %s%.02f EUR%s\n", Green, Yellow, totalvalue[0]["eur"], Reset)
bson.D{{"$group", bson.D{{"_id", nil}, {"total", bson.D{{"$sum", "$totalValue.value"}}}}}}, totalcoll.storage_add_total(t)
}
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))
return nil return nil
}, },