Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only run stats once a day #1721

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}