/
stats.go
245 lines (222 loc) · 6.26 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package serra
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
func init() {
rootCmd.AddCommand(statsCmd)
}
var statsCmd = &cobra.Command{
Aliases: []string{"stats"},
Use: "stats",
Short: "Shows statistics of the collection",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
Stats()
return nil
},
}
func Stats() {
client := storageConnect()
coll := &Collection{client.Database("serra").Collection("cards")}
totalcoll := &Collection{client.Database("serra").Collection("total")}
defer storageDisconnect(client)
// Show Value Stats
showValueStats(coll, totalcoll)
// Reserved List
showReservedListStats(coll)
// Rarities
showRarityStats(coll)
// Colors
showColorStats(coll)
// Artists
showArtistStats(coll)
// Mana Curve of Collection
showManaCurveStats(coll)
// Show cards added per month
showCardsAddedPerMonth(coll)
}
func showValueStats(coll *Collection, totalcoll *Collection) {
l := Logger()
// Value and Card Numbers
stats, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$group", bson.D{
{"_id", nil},
{"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(false), "$serra_count"}}}}}},
{"value_foil", bson.D{{"$sum", bson.D{{"$multiply", bson.A{getCurrencyField(true), "$serra_count_foil"}}}}}},
{"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}},
{"count_foil", bson.D{{"$sum", "$serra_count_foil"}}},
{"rarity", bson.D{{"$sum", "$rarity"}}},
{"unique", bson.D{{"$sum", 1}}},
}},
},
bson.D{
{"$addFields", bson.D{
{"count_all", bson.D{{"$sum", bson.A{"$count", "$count_foil"}}}},
}},
},
})
fmt.Printf("%sCards %s\n", Green, Reset)
fmt.Printf("Total: %s%.0f%s\n", Yellow, stats[0]["count_all"], Reset)
fmt.Printf("Unique: %s%d%s\n", Purple, stats[0]["unique"], Reset)
fmt.Printf("Normal: %s%.0f%s\n", Purple, stats[0]["count"], Reset)
fmt.Printf("Foil: %s%d%s\n", Purple, stats[0]["count_foil"], Reset)
// Total Value
fmt.Printf("\n%sTotal Value%s\n", Green, Reset)
normalValue, err := getFloat64(stats[0]["value"])
if err != nil {
l.Error(err)
normalValue = 0
}
foilValue, err := getFloat64(stats[0]["value_foil"])
if err != nil {
l.Error(err)
foilValue = 0
}
count_all, err := getFloat64(stats[0]["count_all"])
if err != nil {
l.Error(err)
foilValue = 0
}
totalValue := normalValue + foilValue
fmt.Printf("Total: %s%.2f%s%s\n", Pink, totalValue, getCurrency(), Reset)
fmt.Printf("Normal: %s%.2f%s%s\n", Pink, normalValue, getCurrency(), Reset)
fmt.Printf("Foils: %s%.2f%s%s\n", Pink, foilValue, getCurrency(), Reset)
fmt.Printf("Average Card: %s%.2f%s%s\n", Pink, totalValue/count_all, getCurrency(), Reset)
total, _ := totalcoll.storageFindTotal()
fmt.Printf("History: \n")
showPriceHistory(total.Value, "* ", true)
}
func showReservedListStats(coll *Collection) {
reserved, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$match", bson.D{
{"reserved", true}}}},
bson.D{
{"$group", bson.D{
{"_id", nil},
{"count", bson.D{{"$sum", 1}}},
}}},
})
var count_reserved int32
if len(reserved) > 0 {
count_reserved = reserved[0]["count"].(int32)
}
fmt.Printf("Reserved List: %s%d%s\n", Yellow, count_reserved, Reset)
}
func showRarityStats(coll *Collection) {
rar, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$group", bson.D{
{"_id", "$rarity"},
{"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}},
}}},
bson.D{
{"$sort", bson.D{
{"_id", 1},
}}},
})
ri := convertRarities(rar)
fmt.Printf("\n%sRarity%s\n", Green, Reset)
fmt.Printf("Mythics: %s%.0f%s\n", Pink, ri.Mythics, Reset)
fmt.Printf("Rares: %s%.0f%s\n", Pink, ri.Rares, Reset)
fmt.Printf("Uncommons: %s%.0f%s\n", Yellow, ri.Uncommons, Reset)
fmt.Printf("Commons: %s%.0f%s\n", Purple, ri.Commons, Reset)
}
func showCardsAddedPerMonth(coll *Collection) {
fmt.Printf("\n%sCards added over time%s\n", Green, Reset)
type Caot struct {
Id struct {
Year int32 `mapstructure:"year"`
Month int32 `mapstructure:"month"`
} `mapstructure:"_id"`
Count int32 `mapstructure:"count"`
}
caot, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$project", bson.D{
{"month", bson.D{
{"$month", "$serra_created"}}},
{"year", bson.D{
{"$year", "$serra_created"}},
}},
}},
bson.D{
{"$group", bson.D{
{"_id", bson.D{{"month", "$month"}, {"year", "$year"}}},
{"count", bson.D{{"$sum", 1}}},
}},
},
bson.D{
{"$sort", bson.D{{"_id.year", 1}, {"_id.month", 1}}},
},
})
for _, mo := range caot {
moo := new(Caot)
mapstructure.Decode(mo, moo)
fmt.Printf("%d-%02d: %s%d%s\n", moo.Id.Year, moo.Id.Month, Purple, moo.Count, Reset)
}
}
func showManaCurveStats(coll *Collection) {
cmc, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$group", bson.D{
{"_id", "$cmc"},
{"count", bson.D{{"$sum", 1}}},
}}},
bson.D{
{"$sort", bson.D{
{"_id", 1},
}}},
})
fmt.Printf("\n%sMana Curve%s\n", Green, Reset)
for _, mc := range cmc {
fmt.Printf("%.0f: %s%d%s\n", mc["_id"], Purple, mc["count"], Reset)
}
}
func showArtistStats(coll *Collection) {
artists, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$group", bson.D{
{"_id", "$artist"},
{"count", bson.D{{"$sum", 1}}},
}}},
bson.D{
{"$sort", bson.D{
{"count", -1},
}}},
bson.D{
{"$limit", 10}},
})
fmt.Printf("\n%sTop Artists%s\n", Green, Reset)
for _, artist := range artists {
fmt.Printf("%s: %s%d%s\n", artist["_id"].(string), Purple, artist["count"], Reset)
}
}
func showColorStats(coll *Collection) {
sets, _ := coll.storageAggregate(mongo.Pipeline{
bson.D{
{"$match", bson.D{
{"coloridentity", bson.D{{"$size", 1}}}}}},
bson.D{
{"$group", bson.D{
{"_id", "$coloridentity"},
{"count", bson.D{{"$sum", bson.D{{"$multiply", bson.A{1.0, "$serra_count"}}}}}},
}}},
bson.D{
{"$sort", bson.D{
{"count", -1},
}}},
})
fmt.Printf("\n%sColors%s\n", Green, Reset)
for _, set := range sets {
x, _ := set["_id"].(primitive.A)
s := []interface{}(x)
fmt.Printf("%s: %s%.0f%s\n", convertManaSymbols(s), Purple, set["count"], Reset)
}
}