import { readEvents } from "@/lib/analytics.server"; /** * อันดับ "ยอดนิยม" จากสถิติผู้เข้าชมจริง (ไม่ใช่ป้ายที่คนตั้งเอง) * * กติกา: * - ต้องมีประวัติข้อมูลอย่างน้อย 6 เดือน (180 วัน) ก่อน ถึงจะเริ่มจัดอันดับ * เพื่อไม่ให้โรงที่เพิ่งถูกเปิดดู 2-3 ครั้งครองอันดับจากฐานผู้ชมที่ยังเล็ก * - คะแนน: cinema_view = 1, cinema_select = 1.5 (เจตนาชัดกว่า), คูณ decay * ครึ่งชีวิต 30 วัน และนับเฉพาะ event ภายใน 90 วันล่าสุด */ export const POPULARITY_MIN_HISTORY_DAYS = 180; // 6 เดือน export const POPULARITY_WINDOW_DAYS = 90; export const POPULARITY_TOP_N = 8; const DAY_MS = 24 * 60 * 60 * 1000; const SELECT_WEIGHT = 1.5; const HALF_LIFE_DAYS = 30; export type PopularityScore = { slug: string; score: number; views: number; selects: number; }; export type PopularityState = { /** true = มีข้อมูลครบ 6 เดือนแล้ว ใช้จัดอันดับได้ */ ready: boolean; /** จำนวนวันของข้อมูลที่มีอยู่จริง */ history_days: number; /** วันที่ยังต้องเก็บเพิ่ม (0 = ครบแล้ว) */ days_remaining: number; window_days: number; total_events: number; /** เรียงจากมากไปน้อย (ว่างเมื่อ ready=false) */ ranking: PopularityScore[]; /** slug ที่ติดอันดับ (ว่างเมื่อ ready=false) */ slugs: string[]; computed_at: string; }; let cache: { state: PopularityState; expires: number } | null = null; const CACHE_MS = 5 * 60 * 1000; /** คำนวณสถานะ/อันดับยอดนิยม (memo 5 นาที) */ export async function computePopularity(now = Date.now()): Promise { if (cache && cache.expires > now) return cache.state; const events = await readEvents(); let earliest = Number.POSITIVE_INFINITY; const buckets = new Map(); const windowStart = now - POPULARITY_WINDOW_DAYS * DAY_MS; for (const event of events) { const at = Date.parse(event.at); if (!Number.isFinite(at)) continue; if (at < earliest) earliest = at; if (at < windowStart) continue; if (!event.slug) continue; if (event.type !== "cinema_view" && event.type !== "cinema_select") continue; const ageDays = Math.max(0, (now - at) / DAY_MS); const decay = 0.5 ** (ageDays / HALF_LIFE_DAYS); const weight = (event.type === "cinema_view" ? 1 : SELECT_WEIGHT) * decay; const bucket = buckets.get(event.slug) ?? { views: 0, selects: 0, score: 0 }; if (event.type === "cinema_view") bucket.views += 1; else bucket.selects += 1; bucket.score += weight; buckets.set(event.slug, bucket); } const historyDays = Number.isFinite(earliest) ? Math.floor((now - earliest) / DAY_MS) : 0; const ready = historyDays >= POPULARITY_MIN_HISTORY_DAYS; const ranking: PopularityScore[] = [...buckets.entries()] .map(([slug, bucket]) => ({ slug, score: Math.round(bucket.score * 100) / 100, views: bucket.views, selects: bucket.selects, })) .sort((a, b) => b.score - a.score || b.views - a.views || a.slug.localeCompare(b.slug)); const state: PopularityState = { ready, history_days: historyDays, days_remaining: Math.max(0, POPULARITY_MIN_HISTORY_DAYS - historyDays), window_days: POPULARITY_WINDOW_DAYS, total_events: events.length, ranking: ready ? ranking.slice(0, POPULARITY_TOP_N) : [], slugs: ready ? ranking.slice(0, POPULARITY_TOP_N).map((item) => item.slug) : [], computed_at: new Date(now).toISOString(), }; cache = { state, expires: now + CACHE_MS }; return state; }