fix: snapshotBefore may returns null even there's a snapshot exists

This commit is contained in:
alikia2x (寒寒) 2025-03-27 05:49:19 +08:00
parent 9e3cc8236c
commit 7337538f0b
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6

View File

@ -173,7 +173,8 @@ export const collectMilestoneSnapshotsWorker = async (_job: Job) => {
const getRegularSnapshotInterval = async (client: Client, aid: number) => {
const now = Date.now();
const date = new Date(now - 24 * HOUR);
const oldSnapshot = await findSnapshotBefore(client, aid, date);
let oldSnapshot = await findSnapshotBefore(client, aid, date);
if (!oldSnapshot) oldSnapshot = await findClosestSnapshot(client, aid, date);
const latestSnapshot = await getLatestSnapshot(client, aid);
if (!oldSnapshot || !latestSnapshot) return 0;
if (oldSnapshot.created_at === latestSnapshot.created_at) return 0;
@ -181,7 +182,7 @@ const getRegularSnapshotInterval = async (client: Client, aid: number) => {
if (hoursDiff < 8) return 24;
const viewsDiff = latestSnapshot.views - oldSnapshot.views;
if (viewsDiff === 0) return 72;
const speedPerDay = viewsDiff / hoursDiff * 24;
const speedPerDay = viewsDiff / (hoursDiff + 0.001) * 24;
if (speedPerDay < 6) return 36;
if (speedPerDay < 120) return 24;
if (speedPerDay < 320) return 12;