refactor: upgrade to Go 1.26 and modernize code idioms
- Replace `interface{}` with `any` type alias across all packages
- Use built-in `min()`/`max()` for parameter clamping
- Use `slices.SortFunc`, `slices.Min`, `slices.Max` for cleaner code
- Use `strings.Cut()` for simpler string parsing in auth middleware
- Use `errors.Is()` for proper error comparison in handlers
- Update dependencies: golang.org/x/image 0.37.0 -> 0.38.0
- Add Wire code generation guidelines to ARCHITECTURE.md
- Disable Go cache in CI build workflow
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -103,9 +104,7 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
||||
if topN <= 0 {
|
||||
topN = 100
|
||||
}
|
||||
if topN > 500 {
|
||||
topN = 500
|
||||
}
|
||||
topN = min(topN, 500)
|
||||
recentHours := w.cfg.HotRank.RecentWindowHours
|
||||
if recentHours <= 0 {
|
||||
recentHours = 168
|
||||
@@ -176,15 +175,8 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
||||
raw[i] = base / denom
|
||||
}
|
||||
|
||||
minR, maxR := raw[0], raw[0]
|
||||
for _, v := range raw[1:] {
|
||||
if v < minR {
|
||||
minR = v
|
||||
}
|
||||
if v > maxR {
|
||||
maxR = v
|
||||
}
|
||||
}
|
||||
minR := slices.Min(raw)
|
||||
maxR := slices.Max(raw)
|
||||
|
||||
ranked = make([]scored, len(snapshots))
|
||||
for i := range snapshots {
|
||||
@@ -196,11 +188,16 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
||||
}
|
||||
ranked[i] = scored{idx: i, norm: norm}
|
||||
}
|
||||
sort.Slice(ranked, func(a, b int) bool {
|
||||
if ranked[a].norm != ranked[b].norm {
|
||||
return ranked[a].norm > ranked[b].norm
|
||||
slices.SortFunc(ranked, func(a, b scored) int {
|
||||
if a.norm != b.norm {
|
||||
return cmp.Compare(b.norm, a.norm) // 降序
|
||||
}
|
||||
return snapshots[ranked[a].idx].CreatedAt.After(snapshots[ranked[b].idx].CreatedAt)
|
||||
if snapshots[a.idx].CreatedAt.After(snapshots[b.idx].CreatedAt) {
|
||||
return -1
|
||||
} else if snapshots[a.idx].CreatedAt.Before(snapshots[b.idx].CreatedAt) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user