Popularity Scoring
The Express API runs a cron job every hour (0 * * * *) that recalculates popularityScore for every activity. It powers the "Popular" sort option and featured sections.
Formula
popularityScore = views × 1
+ bookingsCount × 10
+ averageRating × 20
+ (isFeatured ? 100 : 0)
- (daysSinceCreation × 0.5)| Factor | Weight | Rationale |
|---|---|---|
| Views | 1× | Baseline engagement |
| Bookings count | 10× | Conversion signal |
| Average rating | 20× | Quality signal (0–5 → up to 100 points) |
| Featured | +100 | Admin promotion |
| Age penalty | −0.5/day | Keeps old listings from dominating |
Example: 1,000 views, 50 bookings, 4.5★, featured, 30 days old → 1000 + 500 + 90 + 100 − 15 = 1675.
Implementation
File: src/cron/updatePopularity.cron.ts
The job runs a single SQL UPDATE … FROM and only writes rows where the computed score differs from the stored one. That avoids rewriting every row on every run (dead tuples, WAL, index churn).
UPDATE activity a
SET "popularityScore" = s.score
FROM (
SELECT id, ROUND(
"views" * 1 +
"bookingsCount" * 10 +
"averageRating" * 20 +
CASE WHEN "isFeatured" THEN 100 ELSE 0 END
- (EXTRACT(DAY FROM (NOW() - "createdAt")) * 0.5)
)::int AS score
FROM activity
) s
WHERE a.id = s.id AND a."popularityScore" IS DISTINCT FROM s.score;A guard prevents overlapping runs, and the cron is initialized once from server.ts.
Serving order
GET /api/v1/activity/popular reads the stored score. Public listing sorts also use it. Because the score is materialised, changes appear on the next hourly run rather than instantly.
Index
@@index([published, status, popularityScore(sort: Desc)])The composite index matches the public list query, so sorting does not need a separate scan.