init work on multi currency

This commit is contained in:
Florian Baumann 2023-01-27 14:29:42 +01:00
parent cb726856ae
commit 292a9da383
3 changed files with 82 additions and 42 deletions

View File

@ -165,13 +165,26 @@ func print_price_history(prices []PriceEntry, prefix string) {
var before float64
for _, e := range prices {
if e.Value > before && before != 0 {
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Green, e.Value, Reset, (e.Value/before*100)-100)
} else if e.Value < before {
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Red, e.Value, Reset, (e.Value/before*100)-100)
// TODO: Make currency configurable
value := e.Eur
if value > before && before != 0 {
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Green, value, Reset, (value/before*100)-100)
} else if value < before {
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Red, value, Reset, (value/before*100)-100)
} else {
fmt.Printf("%s%s %.2f EUR%s\n", prefix, stringToTime(e.Date), e.Value, Reset)
fmt.Printf("%s%s %.2f EUR%s\n", prefix, stringToTime(e.Date), value, Reset)
}
before = e.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

@ -17,6 +17,8 @@ import (
type Card struct {
// Added by Serra
SerraCount int64 `bson:"serra_count"`
SerraCountFoil int64 `bson:"serra_count_foil"`
SerraCountEtched int64 `bson:"serra_count_etched"`
SerraPrices []PriceEntry `bson:"serra_prices"`
SerraCreated primitive.DateTime `bson:"serra_created"`
SerraUpdated primitive.DateTime `bson:"serra_updated"`
@ -80,14 +82,7 @@ type Card struct {
OracleID string `json:"oracle_id"`
OracleText string `json:"oracle_text"`
Oversized bool `json:"oversized"`
Prices struct {
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"`
} `json:"prices"`
Prices PriceEntry `json:"prices"`
PrintedName string `json:"printed_name"`
PrintedText string `json:"printed_text"`
PrintedTypeLine string `json:"printed_type_line"`
@ -126,7 +121,12 @@ type Card struct {
type PriceEntry struct {
Date primitive.DateTime `bson:"date"`
Value float64 `bson:"value"`
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"`
}
// Sets
@ -191,7 +191,8 @@ func fetch_card(path string) (*Card, error) {
val.SerraCreated = primitive.NewDateTimeFromTime(time.Now())
// Increase Price
val.SerraPrices = append(val.SerraPrices, PriceEntry{primitive.NewDateTimeFromTime(time.Now()), val.Prices.Eur})
val.Prices.Date = primitive.NewDateTimeFromTime(time.Now())
val.SerraPrices = append(val.SerraPrices, val.Prices)
return val, nil
}

View File

@ -2,6 +2,7 @@ package serra
import (
"fmt"
"os"
"time"
"github.com/schollz/progressbar/v3"
@ -67,10 +68,11 @@ var updateCmd = &cobra.Command{
continue
}
updated_card.Prices.Date = primitive.NewDateTimeFromTime(time.Now())
update := bson.M{
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now()), "prices": updated_card.Prices, "collectornumber": updated_card.CollectorNumber},
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
"value": updated_card.Prices.Eur}},
"$push": bson.M{"serra_prices": updated_card.Prices},
}
coll.storage_update(bson.M{"_id": bson.M{"$eq": card.ID}}, update)
}
@ -84,23 +86,47 @@ var updateCmd = &cobra.Command{
{"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"`
groupStage := bson.D{
{"$group", bson.D{
{"_id", "$set"},
{"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}},
{"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"}}}}}}}},
}}}
setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
setvalue, err := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
if err != nil {
LogMessage(fmt.Sprintf("%s", err), "red")
}
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"])
// do the update
set_update := bson.M{
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now())},
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
"value": setvalue[0]["value"]}},
"$set": bson.M{"serra_updated": p.Date},
"$push": bson.M{"serra_prices": p},
}
// fmt.Printf("Set %s%s%s (%s) is now worth %s%.02f EUR%s\n", Pink, set.Name, Reset, set.Code, Yellow, setvalue[0]["value"], Reset)
setscoll.storage_update(bson.M{"code": bson.M{"$eq": set.Code}}, set_update)
}
os.Exit(1)
// calculate total summary over all sets
overall_value := mongo.Pipeline{
bson.D{{"$match",