store total value of sets in db
This commit is contained in:
parent
b815bc73c6
commit
29d79b5728
@ -8,9 +8,7 @@
|
|||||||
# Todo
|
# Todo
|
||||||
|
|
||||||
* termui overview
|
* termui overview
|
||||||
* add - do search for cards by name
|
|
||||||
* prices since the beginning
|
* prices since the beginning
|
||||||
* sort of cards in cards query
|
|
||||||
* total card value column with history in mongodb
|
* total card value column with history in mongodb
|
||||||
|
|
||||||
# What its not
|
# What its not
|
||||||
@ -39,6 +37,9 @@ Color distribution
|
|||||||
|
|
||||||
db.cards.aggregate([{ $group: { _id: { color: "$colors" }, count: { $sum: 1 } } }])
|
db.cards.aggregate([{ $group: { _id: { color: "$colors" }, count: { $sum: 1 } } }])
|
||||||
|
|
||||||
|
Calculate value of all sets
|
||||||
|
|
||||||
|
db.sets.aggregate({$match: {serra_prices: {$exists: true}}}, {$project: {name: 1, "totalValue": {$arrayElemAt: ["$serra_prices", -1]} }}, {$group: {_id: null, total: {$sum: "$totalValue.value" }}})
|
||||||
|
|
||||||
# MongoDB Operations
|
# MongoDB Operations
|
||||||
|
|
||||||
|
|||||||
@ -325,6 +325,9 @@ func Update() error {
|
|||||||
// update sets
|
// update sets
|
||||||
setscoll := &Collection{client.Database("serra").Collection("sets")}
|
setscoll := &Collection{client.Database("serra").Collection("sets")}
|
||||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||||
|
totalcoll := &Collection{client.Database("serra").Collection("total")}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
sets, _ := fetch_sets()
|
sets, _ := fetch_sets()
|
||||||
for i, set := range sets.Data {
|
for i, set := range sets.Data {
|
||||||
@ -371,7 +374,6 @@ func Update() error {
|
|||||||
setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
|
setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
|
||||||
|
|
||||||
// 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": primitive.NewDateTimeFromTime(time.Now())},
|
||||||
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
|
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
|
||||||
@ -380,6 +382,19 @@ func Update() error {
|
|||||||
fmt.Printf("Updating Set value: %s (%s) to %.02f EUR\n", set.Name, set.Code, setvalue[0]["value"])
|
fmt.Printf("Updating Set value: %s (%s) to %.02f EUR\n", set.Name, set.Code, setvalue[0]["value"])
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculate total summary over all sets
|
||||||
|
overall_value := mongo.Pipeline{
|
||||||
|
bson.D{{"$match",
|
||||||
|
bson.D{{"serra_prices", bson.D{{"$exists", true}}}}}},
|
||||||
|
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("Updating total value of collection to: %s%.02f EUR%s\n", Yellow, ostats[0]["total"].(float64), Reset)
|
||||||
|
totalcoll.storage_add_total(ostats[0]["total"].(float64))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package serra
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -12,6 +13,11 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Total struct {
|
||||||
|
ID string `json:"id" bson:"_id"`
|
||||||
|
Value []PriceEntry `bson:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
// https://siongui.github.io/2017/02/11/go-add-method-function-to-type-in-external-package/
|
// https://siongui.github.io/2017/02/11/go-add-method-function-to-type-in-external-package/
|
||||||
type Collection struct {
|
type Collection struct {
|
||||||
*mongo.Collection
|
*mongo.Collection
|
||||||
@ -54,6 +60,33 @@ func (coll Collection) storage_add_set(set *Set) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (coll Collection) storage_add_total(v float64) 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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := coll.UpdateOne(
|
||||||
|
context.Background(),
|
||||||
|
filter,
|
||||||
|
update,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (coll Collection) storage_find(filter, sort bson.D) ([]Card, error) {
|
func (coll Collection) storage_find(filter, sort bson.D) ([]Card, error) {
|
||||||
|
|
||||||
opts := options.Find().SetSort(sort)
|
opts := options.Find().SetSort(sort)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user