fix: computeTrend division by zero, add tests for trend and currency parsing, add username to settings

This commit is contained in:
2026-06-25 01:58:27 +03:30
parent 88ccc6aa68
commit 85587d563d
4 changed files with 104 additions and 18 deletions

View File

@@ -212,11 +212,37 @@ func computeTrend(store *db.Store, userID int64, today string, loc *time.Locatio
recentAvg /= 3.0
olderAvg /= 3.0
if olderAvg < 1 {
if recentAvg > 0 {
return 15, "new"
}
return 5, "stable"
}
changePct := math.Round((recentAvg - olderAvg) / olderAvg * 100)
if recentAvg > olderAvg*1.2 {
return 15, fmt.Sprintf("+%.0f%%", math.Round((recentAvg-olderAvg)/math.Max(olderAvg, 1)*100))
return 15, fmt.Sprintf("+%.0f%%", changePct)
}
if olderAvg > recentAvg*1.2 {
return 0, fmt.Sprintf("-%.0f%%", math.Round((olderAvg-recentAvg)/math.Max(olderAvg, 1)*100))
return 0, fmt.Sprintf("%.0f%%", changePct)
}
return 5, "stable"
}
// computeTrendFromAvgs is a pure version of the trend computation for testing.
func computeTrendFromAvgs(recentAvg, olderAvg float64) (pts int, direction string) {
if olderAvg < 1 {
if recentAvg > 0 {
return 15, "new"
}
return 5, "stable"
}
changePct := math.Round((recentAvg - olderAvg) / olderAvg * 100)
if recentAvg > olderAvg*1.2 {
return 15, fmt.Sprintf("+%.0f%%", changePct)
}
if olderAvg > recentAvg*1.2 {
return 0, fmt.Sprintf("%.0f%%", changePct)
}
return 5, "stable"
}