refactor(hot-rank): extract scored type to package level as hotRankScored
Move the scoped 'scored' struct definition to package level and rename to hotRankScored to enable reuse across Refresh and refreshChannelRanks methods, eliminating duplicate type definitions.
This commit is contained in:
@@ -17,6 +17,11 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type hotRankScored struct {
|
||||||
|
idx int
|
||||||
|
norm float64
|
||||||
|
}
|
||||||
|
|
||||||
// HotRankWorker 定时在候选池上重算热门分,写入 Redis ZSET(仅 TopN)及 top_ids(Redis+本地缓存)
|
// HotRankWorker 定时在候选池上重算热门分,写入 Redis ZSET(仅 TopN)及 top_ids(Redis+本地缓存)
|
||||||
type HotRankWorker struct {
|
type HotRankWorker struct {
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
@@ -145,13 +150,8 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type scored struct {
|
|
||||||
idx int
|
|
||||||
norm float64
|
|
||||||
}
|
|
||||||
|
|
||||||
var snapshots []repository.PostHotSnapshot
|
var snapshots []repository.PostHotSnapshot
|
||||||
var ranked []scored
|
var ranked []hotRankScored
|
||||||
if len(candidateIDs) > 0 {
|
if len(candidateIDs) > 0 {
|
||||||
snapshots, err = w.postRepo.GetPostHotSnapshotsByIDs(candidateIDs)
|
snapshots, err = w.postRepo.GetPostHotSnapshotsByIDs(candidateIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -179,7 +179,7 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
|||||||
minR := slices.Min(raw)
|
minR := slices.Min(raw)
|
||||||
maxR := slices.Max(raw)
|
maxR := slices.Max(raw)
|
||||||
|
|
||||||
ranked = make([]scored, len(snapshots))
|
ranked = make([]hotRankScored, len(snapshots))
|
||||||
for i := range snapshots {
|
for i := range snapshots {
|
||||||
var norm float64
|
var norm float64
|
||||||
if maxR > minR {
|
if maxR > minR {
|
||||||
@@ -187,9 +187,9 @@ func (w *HotRankWorker) Refresh(ctx context.Context) error {
|
|||||||
} else {
|
} else {
|
||||||
norm = 1
|
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 {
|
if a.norm != b.norm {
|
||||||
return cmp.Compare(b.norm, a.norm) // 降序
|
return cmp.Compare(b.norm, a.norm) // 降序
|
||||||
}
|
}
|
||||||
@@ -364,13 +364,11 @@ func (w *HotRankWorker) refreshChannelRanks(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var chSnapshots []repository.PostHotSnapshot
|
var chRanked []hotRankScored
|
||||||
var chRanked []scored
|
chSnapshots, ferr := w.postRepo.GetPostHotSnapshotsByIDs(chCandidateIDs)
|
||||||
filteredSnaps, ferr := w.postRepo.GetPostHotSnapshotsByIDs(chCandidateIDs)
|
if ferr != nil || len(chSnapshots) == 0 {
|
||||||
if ferr != nil || len(filteredSnaps) == 0 {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
chSnapshots = filteredSnaps
|
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
raw := make([]float64, len(chSnapshots))
|
raw := make([]float64, len(chSnapshots))
|
||||||
@@ -391,7 +389,7 @@ func (w *HotRankWorker) refreshChannelRanks(
|
|||||||
minR := slices.Min(raw)
|
minR := slices.Min(raw)
|
||||||
maxR := slices.Max(raw)
|
maxR := slices.Max(raw)
|
||||||
|
|
||||||
chRanked = make([]scored, len(chSnapshots))
|
chRanked = make([]hotRankScored, len(chSnapshots))
|
||||||
for i := range chSnapshots {
|
for i := range chSnapshots {
|
||||||
var norm float64
|
var norm float64
|
||||||
if maxR > minR {
|
if maxR > minR {
|
||||||
@@ -399,9 +397,9 @@ func (w *HotRankWorker) refreshChannelRanks(
|
|||||||
} else {
|
} else {
|
||||||
norm = 1
|
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 {
|
if a.norm != b.norm {
|
||||||
return cmp.Compare(b.norm, a.norm)
|
return cmp.Compare(b.norm, a.norm)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user