Skip to content

Commit

Permalink
Merge pull request #9419 from weseek/imprv/shorten-thread-deletion-ex…
Browse files Browse the repository at this point in the history
…pired-at

imprv(ai): Shorten thread deletion expiredAt
  • Loading branch information
mergify[bot] authored Nov 18, 2024
2 parents 25a1bec + 43930d1 commit 8e2fa75
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
7 changes: 3 additions & 4 deletions apps/app/src/features/openai/server/models/thread-relation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { addDays } from 'date-fns';
import type mongoose from 'mongoose';
import { type Model, type Document, Schema } from 'mongoose';

import { getOrCreateModel } from '~/server/util/mongoose-utils';

const DAYS_UNTIL_EXPIRATION = 30;
const DAYS_UNTIL_EXPIRATION = 3;

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './normalize-thread-relation-expired-at';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './normalize-thread-relation-expired-at';
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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 } },
);
};
4 changes: 2 additions & 2 deletions apps/app/src/server/service/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ Guideline as a RAG:
ns: 'crowi',
key: 'app:openaiThreadDeletionCronMaxMinutesUntilRequest',
type: ValueType.NUMBER,
default: 60,
default: 30,
},
OPENAI_THREAD_DELETION_BARCH_SIZE: {
ns: 'crowi',
Expand All @@ -843,7 +843,7 @@ Guideline as a RAG:
ns: 'crowi',
key: 'app:openaiVectorStoreFileDeletionCronMaxMinutesUntilRequest',
type: ValueType.NUMBER,
default: 60,
default: 30,
},
OPENAI_VECTOR_STORE_FILE_DELETION_BARCH_SIZE: {
ns: 'crowi',
Expand Down
2 changes: 2 additions & 0 deletions apps/app/src/server/service/normalize-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizeExpiredAtForThreadRelations } from '~/features/openai/server/services/normalize-data';
import loggerFactory from '~/utils/logger';

import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid';
Expand All @@ -8,6 +9,7 @@ const logger = loggerFactory('growi:service:NormalizeData');
export const normalizeData = async(): Promise<void> => {
await renameDuplicateRootPages();
await convertRevisionPageIdToObjectId();
await normalizeExpiredAtForThreadRelations();

logger.info('normalizeData has been executed');
return;
Expand Down

0 comments on commit 8e2fa75

Please sign in to comment.