Skip to content

Commit

Permalink
feat(next/api): update sync range logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Mar 28, 2024
1 parent 2c3a411 commit 4df68c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions next/api/src/interfaces/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type SyncTicketSearchDocumentJobData =
type: 'syncByRange';
start?: string;
end?: string;
exclude?: string[];
limit?: number;
delay?: number;
};
Expand Down
30 changes: 19 additions & 11 deletions next/api/src/service/search-ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,38 @@ export class SearchTicketService {
private async processSyncByRangeJob(
data: Extract<SyncTicketSearchDocumentJobData, { type: 'syncByRange' }>
) {
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,
Expand Down Expand Up @@ -300,7 +308,7 @@ export class SearchTicketService {
.toJSON();

const res = await this.esClient.search({
index: [this.indexName, this.indexName + '-tmp'],
index: this.indexName,
body,
});

Expand Down

0 comments on commit 4df68c8

Please sign in to comment.