Skip to content

Commit

Permalink
feat: only run stats once a day (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored Feb 17, 2025
1 parent 91f7e95 commit b983360
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
25 changes: 21 additions & 4 deletions src/lib/jobs/refresh-assistants-counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ import { acquireLock, refreshLock } from "$lib/migrations/lock";
import type { ObjectId } from "mongodb";
import { subDays } from "date-fns";
import { logger } from "$lib/server/logger";

import { collections } from "$lib/server/database";
const LOCK_KEY = "assistants.count";

let hasLock = false;
let lockId: ObjectId | null = null;

async function getLastComputationTime(): Promise<Date> {
const lastStats = await collections.assistantStats.findOne({}, { sort: { "date.at": -1 } });
return lastStats?.date?.at || new Date(0);
}

async function shouldComputeStats(): Promise<boolean> {
const lastComputationTime = await getLastComputationTime();
const oneDayAgo = new Date(Date.now() - 24 * 3_600_000);
return lastComputationTime < oneDayAgo;
}

async function refreshAssistantsCountsHelper() {
if (!hasLock) {
return;
Expand Down Expand Up @@ -81,9 +92,15 @@ async function maintainLock() {
export function refreshAssistantsCounts() {
const ONE_HOUR_MS = 3_600_000;

maintainLock().then(() => {
refreshAssistantsCountsHelper();
maintainLock().then(async () => {
if (await shouldComputeStats()) {
refreshAssistantsCountsHelper();
}

setInterval(refreshAssistantsCountsHelper, ONE_HOUR_MS);
setInterval(async () => {
if (await shouldComputeStats()) {
refreshAssistantsCountsHelper();
}
}, 24 * ONE_HOUR_MS);
});
}
23 changes: 20 additions & 3 deletions src/lib/jobs/refresh-conversation-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import { logger } from "$lib/server/logger";
import type { ObjectId } from "mongodb";
import { acquireLock, refreshLock } from "$lib/migrations/lock";

async function getLastComputationTime(): Promise<Date> {
const lastStats = await collections.conversationStats.findOne({}, { sort: { "date.at": -1 } });
return lastStats?.date?.at || new Date(0);
}

async function shouldComputeStats(): Promise<boolean> {
const lastComputationTime = await getLastComputationTime();
const oneDayAgo = new Date(Date.now() - 24 * 3_600_000);
return lastComputationTime < oneDayAgo;
}

export async function computeAllStats() {
for (const span of ["day", "week", "month"] as const) {
computeStats({ dateField: "updatedAt", type: "conversation", span }).catch((e) =>
Expand Down Expand Up @@ -246,9 +257,15 @@ async function maintainLock() {
export function refreshConversationStats() {
const ONE_HOUR_MS = 3_600_000;

maintainLock().then(() => {
computeAllStats();
maintainLock().then(async () => {
if (await shouldComputeStats()) {
computeAllStats();
}

setInterval(computeAllStats, 12 * ONE_HOUR_MS);
setInterval(async () => {
if (await shouldComputeStats()) {
computeAllStats();
}
}, 24 * ONE_HOUR_MS);
});
}

0 comments on commit b983360

Please sign in to comment.