Просмотр исходного кода

Merge pull request #9419 from weseek/imprv/shorten-thread-deletion-expired-at

imprv(ai): Shorten thread deletion expiredAt
mergify[bot] 1 год назад
Родитель
Сommit
8e2fa75ebe

+ 3 - 4
apps/app/src/features/openai/server/models/thread-relation.ts

@@ -1,14 +1,13 @@
+import { addDays } from 'date-fns';
 import type mongoose from 'mongoose';
 import type mongoose from 'mongoose';
 import { type Model, type Document, Schema } from 'mongoose';
 import { type Model, type Document, Schema } from 'mongoose';
 
 
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
 
 
-const DAYS_UNTIL_EXPIRATION = 30;
+const DAYS_UNTIL_EXPIRATION = 3;
 
 
 const generateExpirationDate = (): Date => {
 const generateExpirationDate = (): Date => {
-  const currentDate = new Date();
-  const expirationDate = new Date(currentDate.setDate(currentDate.getDate() + DAYS_UNTIL_EXPIRATION));
-  return expirationDate;
+  return addDays(new Date(), DAYS_UNTIL_EXPIRATION);
 };
 };
 
 
 interface ThreadRelation {
 interface ThreadRelation {

+ 1 - 0
apps/app/src/features/openai/server/services/normalize-data/index.ts

@@ -0,0 +1 @@
+export * from './normalize-thread-relation-expired-at';

+ 1 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/index.ts

@@ -0,0 +1 @@
+export * from './normalize-thread-relation-expired-at';

+ 70 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.integ.ts

@@ -0,0 +1,70 @@
+import { faker } from '@faker-js/faker';
+import { addDays, subDays } from 'date-fns';
+import { Types } from 'mongoose';
+
+import ThreadRelation from '../../../models/thread-relation';
+
+import { MAX_DAYS_UNTIL_EXPIRATION, normalizeExpiredAtForThreadRelations } from './normalize-thread-relation-expired-at';
+
+describe('normalizeExpiredAtForThreadRelations', () => {
+
+  it('should update expiredAt to 3 days from now for expired thread relations', async() => {
+    // arrange
+    const expiredDays = faker.number.int({ min: MAX_DAYS_UNTIL_EXPIRATION, max: 30 });
+    const expiredDate = addDays(new Date(), expiredDays);
+    const threadRelation = new ThreadRelation({
+      userId: new Types.ObjectId(),
+      threadId: 'test-thread',
+      expiredAt: expiredDate,
+    });
+    await threadRelation.save();
+
+    // act
+    await normalizeExpiredAtForThreadRelations();
+
+    // assert
+    const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id);
+    expect(updatedThreadRelation).not.toBeNull();
+    assert(updatedThreadRelation?.expiredAt != null);
+    expect(updatedThreadRelation.expiredAt < addDays(new Date(), MAX_DAYS_UNTIL_EXPIRATION)).toBeTruthy();
+  });
+
+  it('should not update expiredAt for non-expired thread relations', async() => {
+    // arrange
+    const nonExpiredDays = faker.number.int({ min: 0, max: MAX_DAYS_UNTIL_EXPIRATION });
+    const nonExpiredDate = addDays(new Date(), nonExpiredDays);
+    const threadRelation = new ThreadRelation({
+      userId: new Types.ObjectId(),
+      threadId: 'test-thread-2',
+      expiredAt: nonExpiredDate,
+    });
+    await threadRelation.save();
+
+    // act
+    await normalizeExpiredAtForThreadRelations();
+
+    // assert
+    const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id);
+    expect(updatedThreadRelation).not.toBeNull();
+    expect(updatedThreadRelation?.expiredAt).toEqual(nonExpiredDate);
+  });
+
+  it('should not update expiredAt is before today', async() => {
+    // arrange
+    const nonExpiredDate = subDays(new Date(), 1);
+    const threadRelation = new ThreadRelation({
+      userId: new Types.ObjectId(),
+      threadId: 'test-thread-3',
+      expiredAt: nonExpiredDate,
+    });
+    await threadRelation.save();
+
+    // act
+    await normalizeExpiredAtForThreadRelations();
+
+    // assert
+    const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id);
+    expect(updatedThreadRelation).not.toBeNull();
+    expect(updatedThreadRelation?.expiredAt).toEqual(nonExpiredDate);
+  });
+});

+ 14 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.ts

@@ -0,0 +1,14 @@
+import { addDays } from 'date-fns';
+
+import ThreadRelation from '../../../models/thread-relation';
+
+export const MAX_DAYS_UNTIL_EXPIRATION = 3;
+
+export const normalizeExpiredAtForThreadRelations = async(): Promise<void> => {
+  const maxDaysExpiredAt = addDays(new Date(), MAX_DAYS_UNTIL_EXPIRATION);
+
+  await ThreadRelation.updateMany(
+    { expiredAt: { $gt: maxDaysExpiredAt } },
+    { $set: { expiredAt: maxDaysExpiredAt } },
+  );
+};

+ 2 - 2
apps/app/src/server/service/config-loader.ts

@@ -819,7 +819,7 @@ Guideline as a RAG:
     ns: 'crowi',
     ns: 'crowi',
     key: 'app:openaiThreadDeletionCronMaxMinutesUntilRequest',
     key: 'app:openaiThreadDeletionCronMaxMinutesUntilRequest',
     type: ValueType.NUMBER,
     type: ValueType.NUMBER,
-    default: 60,
+    default: 30,
   },
   },
   OPENAI_THREAD_DELETION_BARCH_SIZE: {
   OPENAI_THREAD_DELETION_BARCH_SIZE: {
     ns: 'crowi',
     ns: 'crowi',
@@ -843,7 +843,7 @@ Guideline as a RAG:
     ns: 'crowi',
     ns: 'crowi',
     key: 'app:openaiVectorStoreFileDeletionCronMaxMinutesUntilRequest',
     key: 'app:openaiVectorStoreFileDeletionCronMaxMinutesUntilRequest',
     type: ValueType.NUMBER,
     type: ValueType.NUMBER,
-    default: 60,
+    default: 30,
   },
   },
   OPENAI_VECTOR_STORE_FILE_DELETION_BARCH_SIZE: {
   OPENAI_VECTOR_STORE_FILE_DELETION_BARCH_SIZE: {
     ns: 'crowi',
     ns: 'crowi',

+ 2 - 0
apps/app/src/server/service/normalize-data/index.ts

@@ -1,3 +1,4 @@
+import { normalizeExpiredAtForThreadRelations } from '~/features/openai/server/services/normalize-data';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid';
 import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid';
@@ -8,6 +9,7 @@ const logger = loggerFactory('growi:service:NormalizeData');
 export const normalizeData = async(): Promise<void> => {
 export const normalizeData = async(): Promise<void> => {
   await renameDuplicateRootPages();
   await renameDuplicateRootPages();
   await convertRevisionPageIdToObjectId();
   await convertRevisionPageIdToObjectId();
+  await normalizeExpiredAtForThreadRelations();
 
 
   logger.info('normalizeData has been executed');
   logger.info('normalizeData has been executed');
   return;
   return;