add sets
This commit is contained in:
parent
dcbe0706a7
commit
2267469da8
6
serra.go
6
serra.go
@ -14,8 +14,8 @@ func main() {
|
||||
Usage:
|
||||
serra add <card>...
|
||||
serra list
|
||||
serra sets
|
||||
serra update <path>...
|
||||
serra value <path>...
|
||||
|
||||
Options:
|
||||
-h --help Show this screen.
|
||||
@ -32,4 +32,8 @@ Options:
|
||||
serra.List()
|
||||
}
|
||||
|
||||
if args["sets"].(bool) {
|
||||
serra.Sets()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -73,12 +72,8 @@ type Card struct {
|
||||
OracleText string `json:"oracle_text"`
|
||||
Oversized bool `json:"oversized"`
|
||||
Prices struct {
|
||||
Eur interface{} `json:"eur"`
|
||||
EurFoil interface{} `json:"eur_foil"`
|
||||
Tix interface{} `json:"tix"`
|
||||
Usd interface{} `json:"usd"`
|
||||
UsdEtched interface{} `json:"usd_etched"`
|
||||
UsdFoil interface{} `json:"usd_foil"`
|
||||
Eur float64 `json:"eur,string"`
|
||||
EurFoil float64 `json:"eur_foil,string"`
|
||||
} `json:"prices"`
|
||||
PrintedName string `json:"printed_name"`
|
||||
PrintedText string `json:"printed_text"`
|
||||
@ -136,8 +131,7 @@ func fetch(path string) (*Card, error) {
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
LogMessage(fmt.Sprintf("Card %s not found", path), "yellow")
|
||||
err := errors.New("card: not found")
|
||||
err := errors.New(fmt.Sprintf("card: %s not found", path))
|
||||
return &Card{}, err
|
||||
}
|
||||
|
||||
@ -157,8 +151,7 @@ func fetch(path string) (*Card, error) {
|
||||
val._count = val._count + 1
|
||||
|
||||
// Increase Price
|
||||
v, _ := strconv.ParseFloat(val.Prices.Eur.(string), 32)
|
||||
val._prices = append(val._prices, PriceEntry{time.Now().Format("2006-01-02 15:04:05"), v})
|
||||
val._prices = append(val._prices, PriceEntry{time.Now().Format("2006-01-02 15:04:05"), val.Prices.Eur})
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@ package serra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -22,6 +24,7 @@ func Add(cards []string) {
|
||||
c, err := fetch(card)
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||
continue
|
||||
}
|
||||
|
||||
// Write card to mongodb
|
||||
@ -31,7 +34,7 @@ func Add(cards []string) {
|
||||
continue
|
||||
}
|
||||
|
||||
LogMessage(fmt.Sprintf("\"%s\" (%s Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
|
||||
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
|
||||
}
|
||||
|
||||
}
|
||||
@ -43,7 +46,28 @@ func List() {
|
||||
coll := client.Database("serra").Collection("cards")
|
||||
cards, _ := storage_find(coll)
|
||||
for _, card := range cards {
|
||||
fmt.Printf("%s (%s) %s\n", card.Name, card.Set, card.Prices.Eur)
|
||||
fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur)
|
||||
}
|
||||
}
|
||||
|
||||
func Sets() {
|
||||
LogMessage(fmt.Sprintf("Archivar %v\n", version), "green")
|
||||
|
||||
client := storage_connect()
|
||||
coll := client.Database("serra").Collection("cards")
|
||||
|
||||
groupStage := bson.D{
|
||||
{"$group", bson.D{
|
||||
{"_id", "$setname"},
|
||||
{"sum", bson.D{
|
||||
{"$sum", "$prices.eur"},
|
||||
}},
|
||||
}},
|
||||
}
|
||||
|
||||
sets, _ := storage_aggregate(coll, groupStage)
|
||||
for _, set := range sets {
|
||||
fmt.Printf("* %s (%.2f Eur)\n", set["_id"], set["sum"])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
@ -59,3 +61,25 @@ func storage_find(coll *mongo.Collection) ([]Card, error) {
|
||||
return results, nil
|
||||
|
||||
}
|
||||
|
||||
func storage_aggregate(coll *mongo.Collection, groupstage bson.D) ([]primitive.M, error) {
|
||||
|
||||
// db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}])
|
||||
opts := options.Aggregate().SetMaxTime(2 * time.Second)
|
||||
cursor, err := coll.Aggregate(
|
||||
context.TODO(),
|
||||
mongo.Pipeline{groupstage},
|
||||
opts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Get a list of all returned documents and print them out.
|
||||
// See the mongo.Cursor documentation for more examples of using cursors.
|
||||
var results []bson.M
|
||||
if err = cursor.All(context.TODO(), &results); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return results, nil
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user