init work on multi currency
This commit is contained in:
parent
cb726856ae
commit
292a9da383
@ -165,13 +165,26 @@ func print_price_history(prices []PriceEntry, prefix string) {
|
|||||||
|
|
||||||
var before float64
|
var before float64
|
||||||
for _, e := range prices {
|
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)
|
// TODO: Make currency configurable
|
||||||
} else if e.Value < before {
|
value := e.Eur
|
||||||
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Red, e.Value, Reset, (e.Value/before*100)-100)
|
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 {
|
} 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)
|
||||||
|
}
|
||||||
|
|||||||
@ -17,6 +17,8 @@ import (
|
|||||||
type Card struct {
|
type Card struct {
|
||||||
// Added by Serra
|
// Added by Serra
|
||||||
SerraCount int64 `bson:"serra_count"`
|
SerraCount int64 `bson:"serra_count"`
|
||||||
|
SerraCountFoil int64 `bson:"serra_count_foil"`
|
||||||
|
SerraCountEtched int64 `bson:"serra_count_etched"`
|
||||||
SerraPrices []PriceEntry `bson:"serra_prices"`
|
SerraPrices []PriceEntry `bson:"serra_prices"`
|
||||||
SerraCreated primitive.DateTime `bson:"serra_created"`
|
SerraCreated primitive.DateTime `bson:"serra_created"`
|
||||||
SerraUpdated primitive.DateTime `bson:"serra_updated"`
|
SerraUpdated primitive.DateTime `bson:"serra_updated"`
|
||||||
@ -80,14 +82,7 @@ type Card struct {
|
|||||||
OracleID string `json:"oracle_id"`
|
OracleID string `json:"oracle_id"`
|
||||||
OracleText string `json:"oracle_text"`
|
OracleText string `json:"oracle_text"`
|
||||||
Oversized bool `json:"oversized"`
|
Oversized bool `json:"oversized"`
|
||||||
Prices struct {
|
Prices PriceEntry `json:"prices"`
|
||||||
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"`
|
|
||||||
PrintedName string `json:"printed_name"`
|
PrintedName string `json:"printed_name"`
|
||||||
PrintedText string `json:"printed_text"`
|
PrintedText string `json:"printed_text"`
|
||||||
PrintedTypeLine string `json:"printed_type_line"`
|
PrintedTypeLine string `json:"printed_type_line"`
|
||||||
@ -126,7 +121,12 @@ type Card struct {
|
|||||||
|
|
||||||
type PriceEntry struct {
|
type PriceEntry struct {
|
||||||
Date primitive.DateTime `bson:"date"`
|
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
|
// Sets
|
||||||
@ -191,7 +191,8 @@ func fetch_card(path string) (*Card, error) {
|
|||||||
val.SerraCreated = primitive.NewDateTimeFromTime(time.Now())
|
val.SerraCreated = primitive.NewDateTimeFromTime(time.Now())
|
||||||
|
|
||||||
// Increase Price
|
// 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
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package serra
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/schollz/progressbar/v3"
|
"github.com/schollz/progressbar/v3"
|
||||||
@ -67,10 +68,11 @@ var updateCmd = &cobra.Command{
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updated_card.Prices.Date = primitive.NewDateTimeFromTime(time.Now())
|
||||||
|
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now()), "prices": updated_card.Prices, "collectornumber": updated_card.CollectorNumber},
|
"$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()),
|
"$push": bson.M{"serra_prices": updated_card.Prices},
|
||||||
"value": updated_card.Prices.Eur}},
|
|
||||||
}
|
}
|
||||||
coll.storage_update(bson.M{"_id": bson.M{"$eq": card.ID}}, update)
|
coll.storage_update(bson.M{"_id": bson.M{"$eq": card.ID}}, update)
|
||||||
}
|
}
|
||||||
@ -84,23 +86,47 @@ var updateCmd = &cobra.Command{
|
|||||||
{"set", set.Code},
|
{"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{
|
groupStage := bson.D{
|
||||||
{"$group", bson.D{
|
{"$group", bson.D{
|
||||||
{"_id", "$set"},
|
{"_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
|
// do the update
|
||||||
set_update := bson.M{
|
set_update := bson.M{
|
||||||
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now())},
|
"$set": bson.M{"serra_updated": p.Date},
|
||||||
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
|
"$push": bson.M{"serra_prices": p},
|
||||||
"value": setvalue[0]["value"]}},
|
|
||||||
}
|
}
|
||||||
// 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)
|
// 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)
|
setscoll.storage_update(bson.M{"code": bson.M{"$eq": set.Code}}, set_update)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
|
||||||
// calculate total summary over all sets
|
// calculate total summary over all sets
|
||||||
overall_value := mongo.Pipeline{
|
overall_value := mongo.Pipeline{
|
||||||
bson.D{{"$match",
|
bson.D{{"$match",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user