From 4df68c81d01912cfd88002158acc1f81744076d8 Mon Sep 17 00:00:00 2001 From: sdjdd Date: Thu, 28 Mar 2024 18:11:45 +0800 Subject: [PATCH] feat(next/api): update sync range logic --- next/api/src/interfaces/ticket.ts | 1 + next/api/src/service/search-ticket.ts | 30 +++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/next/api/src/interfaces/ticket.ts b/next/api/src/interfaces/ticket.ts index aab64eeb0..a3d843efa 100644 --- a/next/api/src/interfaces/ticket.ts +++ b/next/api/src/interfaces/ticket.ts @@ -43,6 +43,7 @@ export type SyncTicketSearchDocumentJobData = type: 'syncByRange'; start?: string; end?: string; + exclude?: string[]; limit?: number; delay?: number; }; diff --git a/next/api/src/service/search-ticket.ts b/next/api/src/service/search-ticket.ts index 0cca2323d..0128bd435 100644 --- a/next/api/src/service/search-ticket.ts +++ b/next/api/src/service/search-ticket.ts @@ -151,30 +151,38 @@ export class SearchTicketService { private async processSyncByRangeJob( data: Extract ) { - const { start, end, limit = 100, delay = 1000 } = data; + const { start, end, exclude, limit = 100, delay = 1000 } = data; + const query = Ticket.queryBuilder(); if (start) { - query.where('createdAt', '>', new Date(start)); + query.where('createdAt', '>=', new Date(start)); } if (end) { - query.where('createdAt', '<', new Date(end)); + query.where('createdAt', '<=', new Date(end)); + } + if (exclude?.length) { + query.where('objectId', 'not-in', exclude); } + const tickets = await query.orderBy('createdAt').limit(limit).find({ useMasterKey: true }); if (tickets.length === 0) { console.log('[SearchTicketService] Sync job is done'); return; } await this.syncTickets(tickets); - const lastCreatedAt = tickets[tickets.length - 1].createdAt.toISOString(); - console.log( - `[SearchTicketService] Sync ${ - tickets.length - } tickets, (${tickets[0].createdAt.toISOString()},${lastCreatedAt})` - ); + + const lastCreatedAt = tickets[tickets.length - 1].createdAt; + + // prettier-ignore + console.log(`[SearchTicketService] Sync ${tickets.length} tickets, (${tickets[0].createdAt.toISOString()},${lastCreatedAt.toISOString()})`); + await this.syncQueue?.add( { ...data, - start: lastCreatedAt, + start: lastCreatedAt.toISOString(), + exclude: tickets + .filter((ticket) => ticket.createdAt.getTime() === lastCreatedAt.getTime()) + .map((ticket) => ticket.id), }, { delay, @@ -300,7 +308,7 @@ export class SearchTicketService { .toJSON(); const res = await this.esClient.search({ - index: [this.indexName, this.indexName + '-tmp'], + index: this.indexName, body, });