Spaces:
Sleeping
Sleeping
feat: only run stats once a day (#1721)
Browse files
src/lib/jobs/refresh-assistants-counts.ts
CHANGED
@@ -3,12 +3,23 @@ import { acquireLock, refreshLock } from "$lib/migrations/lock";
|
|
3 |
import type { ObjectId } from "mongodb";
|
4 |
import { subDays } from "date-fns";
|
5 |
import { logger } from "$lib/server/logger";
|
6 |
-
|
7 |
const LOCK_KEY = "assistants.count";
|
8 |
|
9 |
let hasLock = false;
|
10 |
let lockId: ObjectId | null = null;
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
async function refreshAssistantsCountsHelper() {
|
13 |
if (!hasLock) {
|
14 |
return;
|
@@ -81,9 +92,15 @@ async function maintainLock() {
|
|
81 |
export function refreshAssistantsCounts() {
|
82 |
const ONE_HOUR_MS = 3_600_000;
|
83 |
|
84 |
-
maintainLock().then(() => {
|
85 |
-
|
|
|
|
|
86 |
|
87 |
-
setInterval(
|
|
|
|
|
|
|
|
|
88 |
});
|
89 |
}
|
|
|
3 |
import type { ObjectId } from "mongodb";
|
4 |
import { subDays } from "date-fns";
|
5 |
import { logger } from "$lib/server/logger";
|
6 |
+
import { collections } from "$lib/server/database";
|
7 |
const LOCK_KEY = "assistants.count";
|
8 |
|
9 |
let hasLock = false;
|
10 |
let lockId: ObjectId | null = null;
|
11 |
|
12 |
+
async function getLastComputationTime(): Promise<Date> {
|
13 |
+
const lastStats = await collections.assistantStats.findOne({}, { sort: { "date.at": -1 } });
|
14 |
+
return lastStats?.date?.at || new Date(0);
|
15 |
+
}
|
16 |
+
|
17 |
+
async function shouldComputeStats(): Promise<boolean> {
|
18 |
+
const lastComputationTime = await getLastComputationTime();
|
19 |
+
const oneDayAgo = new Date(Date.now() - 24 * 3_600_000);
|
20 |
+
return lastComputationTime < oneDayAgo;
|
21 |
+
}
|
22 |
+
|
23 |
async function refreshAssistantsCountsHelper() {
|
24 |
if (!hasLock) {
|
25 |
return;
|
|
|
92 |
export function refreshAssistantsCounts() {
|
93 |
const ONE_HOUR_MS = 3_600_000;
|
94 |
|
95 |
+
maintainLock().then(async () => {
|
96 |
+
if (await shouldComputeStats()) {
|
97 |
+
refreshAssistantsCountsHelper();
|
98 |
+
}
|
99 |
|
100 |
+
setInterval(async () => {
|
101 |
+
if (await shouldComputeStats()) {
|
102 |
+
refreshAssistantsCountsHelper();
|
103 |
+
}
|
104 |
+
}, 24 * ONE_HOUR_MS);
|
105 |
});
|
106 |
}
|
src/lib/jobs/refresh-conversation-stats.ts
CHANGED
@@ -4,6 +4,17 @@ import { logger } from "$lib/server/logger";
|
|
4 |
import type { ObjectId } from "mongodb";
|
5 |
import { acquireLock, refreshLock } from "$lib/migrations/lock";
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
export async function computeAllStats() {
|
8 |
for (const span of ["day", "week", "month"] as const) {
|
9 |
computeStats({ dateField: "updatedAt", type: "conversation", span }).catch((e) =>
|
@@ -246,9 +257,15 @@ async function maintainLock() {
|
|
246 |
export function refreshConversationStats() {
|
247 |
const ONE_HOUR_MS = 3_600_000;
|
248 |
|
249 |
-
maintainLock().then(() => {
|
250 |
-
|
|
|
|
|
251 |
|
252 |
-
setInterval(
|
|
|
|
|
|
|
|
|
253 |
});
|
254 |
}
|
|
|
4 |
import type { ObjectId } from "mongodb";
|
5 |
import { acquireLock, refreshLock } from "$lib/migrations/lock";
|
6 |
|
7 |
+
async function getLastComputationTime(): Promise<Date> {
|
8 |
+
const lastStats = await collections.conversationStats.findOne({}, { sort: { "date.at": -1 } });
|
9 |
+
return lastStats?.date?.at || new Date(0);
|
10 |
+
}
|
11 |
+
|
12 |
+
async function shouldComputeStats(): Promise<boolean> {
|
13 |
+
const lastComputationTime = await getLastComputationTime();
|
14 |
+
const oneDayAgo = new Date(Date.now() - 24 * 3_600_000);
|
15 |
+
return lastComputationTime < oneDayAgo;
|
16 |
+
}
|
17 |
+
|
18 |
export async function computeAllStats() {
|
19 |
for (const span of ["day", "week", "month"] as const) {
|
20 |
computeStats({ dateField: "updatedAt", type: "conversation", span }).catch((e) =>
|
|
|
257 |
export function refreshConversationStats() {
|
258 |
const ONE_HOUR_MS = 3_600_000;
|
259 |
|
260 |
+
maintainLock().then(async () => {
|
261 |
+
if (await shouldComputeStats()) {
|
262 |
+
computeAllStats();
|
263 |
+
}
|
264 |
|
265 |
+
setInterval(async () => {
|
266 |
+
if (await shouldComputeStats()) {
|
267 |
+
computeAllStats();
|
268 |
+
}
|
269 |
+
}, 24 * ONE_HOUR_MS);
|
270 |
});
|
271 |
}
|