Sample least-recently-fetched friends instead of random

Replaces random.sample with a staleness-ordered pick: friends never
fetched sort first (epoch timestamp), then oldest fetched_at. Ensures
full following list coverage over successive nightly runs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-18 07:15:35 -04:00
parent 4d29bb3ca4
commit ccc5126ae0
+18 -1
View File
@@ -15,6 +15,8 @@ from utils.beli_service import (
refresh_access_token,
)
from tortoise.functions import Max
from .models import BeliProfile, BeliRestaurant, BeliStatus
logger = logging.getLogger(__name__)
@@ -55,7 +57,22 @@ async def _refresh_friend_sample(profile: BeliProfile, access_token: str) -> int
if not following:
return 0
sample = random.sample(following, min(FRIEND_SAMPLE_SIZE, len(following)))
# Pick the FRIEND_SAMPLE_SIZE stalest friends — never-fetched first, then oldest
fetched_rows = (
await BeliRestaurant.filter(profile=profile)
.exclude(friend_beli_user_id="")
.group_by("friend_beli_user_id")
.annotate(last_fetched=Max("fetched_at"))
.values("friend_beli_user_id", "last_fetched")
)
last_fetched: dict[str, datetime] = {
r["friend_beli_user_id"]: r["last_fetched"] for r in fetched_rows
}
_epoch = datetime.fromtimestamp(0, tz=timezone.utc)
sample = sorted(
following,
key=lambda f: last_fetched.get(str(f.get("id") or ""), _epoch),
)[:FRIEND_SAMPLE_SIZE]
upserted = 0
for friend in sample: