Skip to content

Commit bcc4c46

Browse files
committed
Only show siginificant price changes >5% in all price history outputs
1 parent 5e34f32 commit bcc4c46

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

pkg/serra/helpers.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ func convertRarities(rar []primitive.M) Rarities {
193193
func showPriceHistory(prices []PriceEntry, prefix string, total bool) {
194194

195195
var before float64
196-
for _, e := range prices {
196+
last := len(prices)
197+
for i, e := range prices {
197198

198199
var value float64
200+
201+
// check if a total sum is going to be printed
199202
if total {
200203
if getCurrency() == EUR {
201204
value = e.Eur + e.EurFoil
@@ -210,14 +213,28 @@ func showPriceHistory(prices []PriceEntry, prefix string, total bool) {
210213
}
211214
}
212215

213-
if value > before && before != 0 {
214-
fmt.Printf("%s%s%s %.2f%s%s (%+.2f%%, %+.2f%s)\n", prefix, stringToTime(e.Date), Green, value, getCurrency(), Reset, (value/before*100)-100, value-before, getCurrency())
215-
} else if value < before {
216-
fmt.Printf("%s%s%s %.2f%s%s (%+.2f%%, %+.2f%s)\n", prefix, stringToTime(e.Date), Red, value, getCurrency(), Reset, (value/before*100)-100, value-before, getCurrency())
217-
} else {
216+
// calculate percent difference
217+
diffPercent := (value / before * 100) - 100
218+
219+
// always display first history entry
220+
if i == 0 {
218221
fmt.Printf("%s%s %.2f%s%s\n", prefix, stringToTime(e.Date), value, getCurrency(), Reset)
222+
before = value
223+
continue
224+
}
225+
226+
// price increased or first or last element in history
227+
if (value >= before && before != 0) && (diffPercent > 5 || i+1 == last) {
228+
fmt.Printf("%s%s%s %.2f%s%s (%+.2f%%, %+.2f%s)\n", prefix, stringToTime(e.Date), Green, value, getCurrency(), Reset, diffPercent, value-before, getCurrency())
219229
}
230+
231+
// price decreased or first or last element in history
232+
if (value < before) && (diffPercent < -5 || i == last) {
233+
fmt.Printf("%s%s%s %.2f%s%s (%+.2f%%, %+.2f%s)\n", prefix, stringToTime(e.Date), Red, value, getCurrency(), Reset, diffPercent, value-before, getCurrency())
234+
}
235+
220236
before = value
237+
221238
}
222239
}
223240

0 commit comments

Comments
 (0)