diff --git a/internal/service/hot_rank_worker.go b/internal/service/hot_rank_worker.go index df2a52d..e8645e9 100644 --- a/internal/service/hot_rank_worker.go +++ b/internal/service/hot_rank_worker.go @@ -17,6 +17,11 @@ import ( "go.uber.org/zap" ) +type hotRankScored struct { + idx int + norm float64 +} + // HotRankWorker 定时在候选池上重算热门分,写入 Redis ZSET(仅 TopN)及 top_ids(Redis+本地缓存) type HotRankWorker struct { cfg *config.Config @@ -145,13 +150,8 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error { return nil } - type scored struct { - idx int - norm float64 - } - var snapshots []repository.PostHotSnapshot - var ranked []scored + var ranked []hotRankScored if len(candidateIDs) > 0 { snapshots, err = w.postRepo.GetPostHotSnapshotsByIDs(candidateIDs) if err != nil { @@ -179,7 +179,7 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error { minR := slices.Min(raw) maxR := slices.Max(raw) - ranked = make([]scored, len(snapshots)) + ranked = make([]hotRankScored, len(snapshots)) for i := range snapshots { var norm float64 if maxR > minR { @@ -187,9 +187,9 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error { } else { norm = 1 } - ranked[i] = scored{idx: i, norm: norm} + ranked[i] = hotRankScored{idx: i, norm: norm} } - slices.SortFunc(ranked, func(a, b scored) int { + slices.SortFunc(ranked, func(a, b hotRankScored) int { if a.norm != b.norm { return cmp.Compare(b.norm, a.norm) // 降序 } @@ -364,13 +364,11 @@ func (w *HotRankWorker) refreshChannelRanks( continue } - var chSnapshots []repository.PostHotSnapshot - var chRanked []scored - filteredSnaps, ferr := w.postRepo.GetPostHotSnapshotsByIDs(chCandidateIDs) - if ferr != nil || len(filteredSnaps) == 0 { + var chRanked []hotRankScored + chSnapshots, ferr := w.postRepo.GetPostHotSnapshotsByIDs(chCandidateIDs) + if ferr != nil || len(chSnapshots) == 0 { continue } - chSnapshots = filteredSnaps now := time.Now() raw := make([]float64, len(chSnapshots)) @@ -391,7 +389,7 @@ func (w *HotRankWorker) refreshChannelRanks( minR := slices.Min(raw) maxR := slices.Max(raw) - chRanked = make([]scored, len(chSnapshots)) + chRanked = make([]hotRankScored, len(chSnapshots)) for i := range chSnapshots { var norm float64 if maxR > minR { @@ -399,9 +397,9 @@ func (w *HotRankWorker) refreshChannelRanks( } else { norm = 1 } - chRanked[i] = scored{idx: i, norm: norm} + chRanked[i] = hotRankScored{idx: i, norm: norm} } - slices.SortFunc(chRanked, func(a, b scored) int { + slices.SortFunc(chRanked, func(a, b hotRankScored) int { if a.norm != b.norm { return cmp.Compare(b.norm, a.norm) }