-
Notifications
You must be signed in to change notification settings - Fork 3
fix: Fix Question Service seed-batch bugs and edge cases #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes synchronization issues in the LeetCode question seeding process by improving cursor management and database operations. It removes the "done" state tracking in favor of more robust pagination handling and optimizes the database schema for better performance.
- Replaces "done" state with total-based cursor management to prevent re-fetching completed data
- Changes from
$set
to$setOnInsert
to prevent overwriting existing question data - Optimizes database indexes by adding unique constraint on titleSlug and improving compound indexes
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
backend-services/leetcode-backend-service/src/leetcode/seedBatch.ts | Removes done state tracking, improves cursor pagination logic, and changes upsert strategy to preserve existing data |
backend-services/leetcode-backend-service/src/db/model/question.ts | Adds unique index on titleSlug and optimizes compound indexes for better query performance |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
// setOnInsert to avoid overwriting existing entries | ||
$setOnInsert: { |
Copilot
AI
Oct 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using $setOnInsert
instead of $set
means that existing questions will never be updated with new data from LeetCode. This could leave stale data in the database if question details change on LeetCode's side.
// setOnInsert to avoid overwriting existing entries | |
$setOnInsert: { | |
// set to update existing entries with latest data | |
$set: { |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the intended outcome, not to update the database - to prevent issue where update may overwrite update made manually.
backend-services/leetcode-backend-service/src/db/model/question.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
backend-services/leetcode-backend-service/src/leetcode/seedBatch.ts
Outdated
Show resolved
Hide resolved
}, | ||
}, |
Copilot
AI
Oct 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updatedAt
field was removed from $setOnInsert
but upsert: true
still allows updates. This could lead to inconsistent timestamp behavior for existing records. Consider either adding updatedAt
back or clarifying the intended behavior for record updates.
}, | |
}, | |
updatedAt: new Date(), | |
}, | |
$set: { | |
updatedAt: new Date(), | |
}, |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our case, an additional
$set: {
updatedAt: new Date(),
},
is not required since we only want to insert new records and don't want to update existing ones. Moreover, since the schema already has { timestamps: true }
, MongoDB will take care of setting updatedAt
when a new document is inserted.
d7a163c
to
ceaa880
Compare
ceaa880
to
c970774
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
const cursor = | ||
(await SeedCursor.findById(id)) ?? | ||
new SeedCursor({ _id: id, nextSkip: 0, pageSize: 200, done: false }); | ||
new SeedCursor({ _id: id, nextSkip: 0, pageSize: 200 }); |
Copilot
AI
Oct 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pageSize is hardcoded as a magic number. Consider defining this as a named constant at the module level for better maintainability.
Copilot uses AI. Check for mistakes.
if (nextSkip + pageSize > total) { | ||
// Prevent future refetching of previously fetched items | ||
cursor.nextSkip = total; | ||
} else { | ||
cursor.nextSkip = nextSkip + pageSize; | ||
} |
Copilot
AI
Oct 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cursor advancement logic is duplicated from lines 67. Consider extracting this into a helper function to avoid code duplication.
Copilot uses AI. Check for mistakes.
} | ||
|
||
if (attempt < retries) { | ||
await delay(250 * 2 ** attempt); |
Copilot
AI
Oct 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The retry delay calculation uses magic numbers. Consider defining base delay (250ms) as a named constant for better maintainability.
Copilot uses AI. Check for mistakes.
Description:
This PR fixes the following issues with the question syncing process:
leetcode-service
instead ofquestion-service
.Changes Made
leetcode-service.questions
DB