fix: 假单2分钟窗口内固定顺序,避免刷新即变序
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -50,10 +50,10 @@ def _random_display_time_str():
|
||||
|
||||
def _resolve_shuffle_epoch():
|
||||
"""
|
||||
全用户共用一条打乱状态(id=1):
|
||||
- 距上次打乱 >= IntervalSeconds:更新 LastShuffleTime,进入新打乱窗口
|
||||
- 否则:沿用上次打乱时刻,本窗口内顺序不变
|
||||
返回用于固定顺序的 epoch 时间戳;若应使用库表顺序则返回 None。
|
||||
全用户共用 id=1 一条记录:
|
||||
- 已满 IntervalSeconds(默认120秒):更新 LastShuffleTime
|
||||
- 未满:不更新,沿用上次时刻
|
||||
同一窗口内所有用户用相同 seed,刷新不会变顺序。
|
||||
"""
|
||||
now = timezone.now()
|
||||
with transaction.atomic():
|
||||
@@ -69,23 +69,12 @@ def _resolve_shuffle_epoch():
|
||||
if created or elapsed >= interval:
|
||||
state.LastShuffleTime = now
|
||||
state.save(update_fields=['LastShuffleTime', 'UpdateTime'])
|
||||
return int(state.LastShuffleTime.timestamp())
|
||||
return None
|
||||
return int(state.LastShuffleTime.timestamp())
|
||||
|
||||
|
||||
def _order_fake_rows(rows, shuffle_epoch):
|
||||
"""shuffle_epoch 有值:按全局 epoch 固定打乱;None:按库表 SortOrder/CreateTime。"""
|
||||
items = list(rows)
|
||||
if shuffle_epoch is None:
|
||||
items.sort(
|
||||
key=lambda r: (
|
||||
-(r.SortOrder or 0),
|
||||
r.CreateTime or timezone.datetime.min.replace(tzinfo=timezone.utc),
|
||||
r.id or 0,
|
||||
),
|
||||
)
|
||||
return items
|
||||
base = sorted(items, key=lambda r: (r.id or 0, r.OrderID or ''))
|
||||
"""按全局 epoch 固定打乱;2 分钟内所有人顺序一致。"""
|
||||
base = sorted(list(rows), key=lambda r: (r.id or 0, r.OrderID or ''))
|
||||
rng = random.Random(shuffle_epoch)
|
||||
rng.shuffle(base)
|
||||
return base
|
||||
|
||||
Reference in New Issue
Block a user