-
Notifications
You must be signed in to change notification settings - Fork 120
Optimize order creation performance #675
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
Conversation
- Add image caching system to eliminate file I/O operations during order creation - Implement database indexes for frequently queried collections (users, orders, communities) - Consolidate community lookup queries to reduce database calls - Add graceful handling of existing database indexes - Replace deprecated Order.count() with Order.countDocuments() These optimizations significantly reduce order creation response times from 6-9 seconds to under 1 second.
WalkthroughThe changes introduce centralized helper functions for community lookup and currency validation, refactor order command logic to use these helpers, and add a new module for managing MongoDB indexes. Additionally, the order creation logic is updated to accept an optional community ID, and new indexes are created during application startup for improved database performance. Changes
Suggested reviewers
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm error Exit handler never called! 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
✨ Finishing Touches
🧪 Generate Unit Tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
bot/modules/orders/commands.ts (1)
2-2
: Remove unused import.The
Community
import is no longer used after refactoring to usegetCommunityInfo
.-import { Community, Order } from '../../../models'; +import { Order } from '../../../models';
🧹 Nitpick comments (9)
models/indexes.ts (6)
7-27
: Consider using proper MongoDB types instead ofany
.Using
any
type for the collection parameter reduces type safety. Consider importing and using the proper MongoDB Collection type from mongoose.+import { Collection } from 'mongoose'; + -const checkIndexExists = async (collection: any, indexSpec: any): Promise<boolean> => { +const checkIndexExists = async (collection: Collection, indexSpec: Record<string, number>): Promise<boolean> => {
51-58
: Use optional chaining for safer error handling.The static analysis correctly identifies that optional chaining would be safer here.
- if (error.message && error.message.includes('already exists')) { + if (error?.message?.includes('already exists')) {
7-7
: Improve type safety for better maintainability.Consider using more specific types instead of
any
for better type safety and IDE support.-const checkIndexExists = async (collection: any, indexSpec: any): Promise<boolean> => { +const checkIndexExists = async ( + collection: mongoose.Collection, + indexSpec: Record<string, number> +): Promise<boolean> => {
12-20
: Consider potential edge cases in index comparison logic.The JSON.stringify approach for comparing index specifications might not handle all edge cases, such as different key ordering or complex index options. While it works for the current use cases, consider using a more robust comparison method.
Alternative approach using lodash isEqual or custom comparison:
- // Convert our index spec to a comparable format - const targetKeys = JSON.stringify(indexSpec); - - // Check if any existing index matches our specification - for (const existingIndex of existingIndexes) { - const existingKeys = JSON.stringify(existingIndex.key); - if (existingKeys === targetKeys) { - return true; - } - } + // Check if any existing index matches our specification + for (const existingIndex of existingIndexes) { + if (deepEqual(existingIndex.key, indexSpec)) { + return true; + } + }
32-37
: Improve type safety for function parameters.Similar to the previous function, consider using more specific types for better type safety.
-const createIndexSafely = async ( - collection: any, - indexSpec: any, - options: any, - collectionName: string -): Promise<void> => { +const createIndexSafely = async ( + collection: mongoose.Collection, + indexSpec: Record<string, number>, + options: mongoose.IndexOptions & { name: string }, + collectionName: string +): Promise<void> => {
53-53
: Use optional chaining for cleaner code.The static analysis tool correctly identifies that optional chaining would be cleaner and safer here.
- if (error.message && error.message.includes('already exists')) { + if (error.message?.includes('already exists')) {bot/ordersActions.ts (1)
74-74
: Consider handling undefined community_id in getFee function instead of using empty string fallback.Instead of passing
community_id || ''
at every call site, it would be cleaner to handle undefined community_id within the getFee function itself.Consider updating the getFee function to accept an optional community_id parameter and handle the undefined case internally, then update the calls:
- const fee = await getFee(amount, community_id || ''); + const fee = await getFee(amount, community_id);and
- await getFee(amount, community_id || '', true) : + await getFee(amount, community_id, true) :Also applies to: 108-108
util/communityHelper.ts (2)
24-24
: Remove unnecessary initialization of communityId.The static analysis correctly identifies that initializing
communityId
toundefined
is redundant.- let communityId: string | undefined = undefined; + let communityId: string | undefined;
50-50
: Consider using strict equality for ID comparison.Using loose equality (
==
) for ID comparison could lead to unexpected behavior. If this is intentional for comparing MongoDB ObjectIds with strings, consider adding a comment explaining the rationale.- isBanned = community.banned_users.some((buser: any) => buser.id == user._id); + isBanned = community.banned_users.some((buser: any) => String(buser.id) === String(user._id));
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
app.ts
(2 hunks)bot/modules/orders/commands.ts
(4 hunks)bot/ordersActions.ts
(3 hunks)models/indexes.ts
(1 hunks)util/communityHelper.ts
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
`app.ts`: The main entry point of the application is app.ts, which handles MongoDB connection and bot initialization.
app.ts
: The main entry point of the application is app.ts, which handles MongoDB connection and bot initialization.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
app.ts
`**/*.{ts,tsx}`: Use custom context types that extend Telegraf's base context, such as MainContext and CommunityContext, for context enhancement.
**/*.{ts,tsx}
: Use custom context types that extend Telegraf's base context, such as MainContext and CommunityContext, for context enhancement.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
app.ts
models/indexes.ts
bot/ordersActions.ts
bot/modules/orders/commands.ts
util/communityHelper.ts
`models/**`: MongoDB schemas for User, Order, Community, Dispute, and PendingPay...
models/**
: MongoDB schemas for User, Order, Community, Dispute, and PendingPayment should be defined in the models directory.
Database models should include User, Order, Community, Dispute, and PendingPayment, each with appropriate schema definitions.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
models/indexes.ts
`bot/modules/*/{commands,actions,messages,scenes,index}.ts`: Each feature module...
bot/modules/*/{commands,actions,messages,scenes,index}.ts
: Each feature module in the bot should follow this structure: commands.ts (command handlers), actions.ts (action button handlers), messages.ts (message templates), scenes.ts (multi-step conversation flows), and index.ts (module configuration and exports).
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
bot/modules/orders/commands.ts
`bot/modules/**`: Feature-specific logic should be organized into modules within the bot/modules directory.
bot/modules/**
: Feature-specific logic should be organized into modules within the bot/modules directory.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
bot/modules/orders/commands.ts
`util/**`: Shared utilities and helpers should be placed in the util directory.
util/**
: Shared utilities and helpers should be placed in the util directory.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
util/communityHelper.ts
🧠 Learnings (5)
📓 Common learnings
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to models/** : MongoDB schemas for User, Order, Community, Dispute, and PendingPayment should be defined in the models directory.
Learnt from: Catrya
PR: lnp2pBot/bot#650
File: bot/modules/nostr/events.ts:43-54
Timestamp: 2025-05-02T19:37:57.466Z
Learning: In the lnp2pBot codebase, communities can have at most 2 order channels - it's not possible for community.order_channels.length to be greater than 2.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to models/** : Database models should include User, Order, Community, Dispute, and PendingPayment, each with appropriate schema definitions.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to jobs/** : Background jobs should handle pending payment retries, order expiration and cleanup, community earnings calculation, and node health monitoring.
app.ts (5)
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to app.ts : The main entry point of the application is app.ts, which handles MongoDB connection and bot initialization.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to bot/start.ts : Bot initialization, command registration, and scheduled jobs should be handled in bot/start.ts.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.440Z
Learning: Use the following key dependencies: telegraf, mongoose, lightning, node-schedule, and @grammyjs/i18n.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to bot/modules/*/{commands,actions,messages,scenes,index}.ts : Each feature module in the bot should follow this structure: commands.ts (command handlers), actions.ts (action button handlers), messages.ts (message templates), scenes.ts (multi-step conversation flows), and index.ts (module configuration and exports).
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to **/*.{ts,tsx} : Use custom context types that extend Telegraf's base context, such as MainContext and CommunityContext, for context enhancement.
models/indexes.ts (2)
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to models/** : MongoDB schemas for User, Order, Community, Dispute, and PendingPayment should be defined in the models directory.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to bot/modules/*/{commands,actions,messages,scenes,index}.ts : Each feature module in the bot should follow this structure: commands.ts (command handlers), actions.ts (action button handlers), messages.ts (message templates), scenes.ts (multi-step conversation flows), and index.ts (module configuration and exports).
bot/ordersActions.ts (4)
Learnt from: webwarrior-ws
PR: lnp2pBot/bot#665
File: bot/commands.ts:801-803
Timestamp: 2025-06-23T08:18:45.934Z
Learning: In the IOrder interface from bot/commands.ts, the secret property is typed such that it cannot be undefined, only null or string. Therefore, checking only for null is sufficient and checking for undefined is unnecessary.
Learnt from: webwarrior-ws
PR: lnp2pBot/bot#665
File: bot/commands.ts:801-803
Timestamp: 2025-06-23T08:18:45.934Z
Learning: In the IOrder interface from models/order.ts, the secret property is typed as `string | null`, which means it can never be undefined in TypeScript strict mode. Therefore, checking only for null is sufficient and checking for undefined is unnecessary.
Learnt from: webwarrior-ws
PR: lnp2pBot/bot#665
File: bot/commands.ts:267-270
Timestamp: 2025-06-23T08:19:23.336Z
Learning: In the IOrder interface, the hash property is typed as `string | null` (not including undefined), so checking specifically for null with `if (order.hash === null)` is the correct and most precise approach rather than using a falsy check like `if (!order.hash)`.
Learnt from: Catrya
PR: lnp2pBot/bot#650
File: bot/modules/nostr/events.ts:43-54
Timestamp: 2025-05-02T19:37:57.466Z
Learning: In the lnp2pBot codebase, communities can have at most 2 order channels - it's not possible for community.order_channels.length to be greater than 2.
bot/modules/orders/commands.ts (4)
Learnt from: webwarrior-ws
PR: lnp2pBot/bot#665
File: bot/commands.ts:801-803
Timestamp: 2025-06-23T08:18:45.934Z
Learning: In the IOrder interface from bot/commands.ts, the secret property is typed such that it cannot be undefined, only null or string. Therefore, checking only for null is sufficient and checking for undefined is unnecessary.
Learnt from: Catrya
PR: lnp2pBot/bot#650
File: bot/modules/nostr/events.ts:43-54
Timestamp: 2025-05-02T19:37:57.466Z
Learning: In the lnp2pBot codebase, communities can have at most 2 order channels - it's not possible for community.order_channels.length to be greater than 2.
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to bot/modules/*/{commands,actions,messages,scenes,index}.ts : Each feature module in the bot should follow this structure: commands.ts (command handlers), actions.ts (action button handlers), messages.ts (message templates), scenes.ts (multi-step conversation flows), and index.ts (module configuration and exports).
Learnt from: CR
PR: lnp2pBot/bot#0
File: CLAUDE.md:0-0
Timestamp: 2025-06-30T20:32:02.439Z
Learning: Applies to models/** : MongoDB schemas for User, Order, Community, Dispute, and PendingPayment should be defined in the models directory.
🧬 Code Graph Analysis (4)
app.ts (1)
models/indexes.ts (1)
createIndexes
(65-140)
bot/ordersActions.ts (2)
bot/modules/community/scenes.ts (2)
fee
(308-345)fee
(439-441)util/index.ts (1)
getFee
(597-597)
bot/modules/orders/commands.ts (4)
util/communityHelper.ts (2)
getCommunityInfo
(17-66)isCurrencySupported
(71-74)bot/modules/orders/messages.ts (1)
deletedCommunityMessage
(77-83)models/user.ts (1)
UserDocument
(8-27)models/index.ts (1)
Order
(11-11)
util/communityHelper.ts (4)
models/community.ts (1)
ICommunity
(37-53)models/user.ts (1)
UserDocument
(8-27)models/index.ts (1)
Community
(13-13)logger.ts (1)
logger
(53-53)
🪛 Biome (1.9.4)
models/indexes.ts
[error] 53-53: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🪛 GitHub Actions: Auto Check Lint
bot/modules/orders/commands.ts
[error] 2-2: ESLint: 'Community' is defined but never used. (no-unused-vars)
🪛 GitHub Check: Lint
util/communityHelper.ts
[failure] 24-24:
It's not necessary to initialize 'communityId: string | undefined' to undefined
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Analyze (javascript)
- GitHub Check: ci_to_main
🔇 Additional comments (9)
models/indexes.ts (5)
65-140
: Well-structured index creation with appropriate error handling.The index creation logic is well-implemented:
- Indexes are created in the background to avoid blocking
- Appropriate compound indexes for query optimization
- Case-insensitive collation for community group lookups
- Graceful error handling prevents app startup failure
73-81
: LGTM! Well-designed compound index for order queries.The compound index on
creator_id
andstatus
is well-designed for the query patterns mentioned in the PR objectives, particularly for efficiently finding pending orders by creator.
98-107
: Excellent use of case-insensitive collation for community lookups.The collation setting with strength 2 provides case-insensitive matching for community group names, which is appropriate for user-friendly community lookups.
124-133
: Good use of unique constraint on telegram ID.The unique index on
tg_id
prevents duplicate users and optimizes user lookups, which aligns with the application's user management requirements.
136-140
: Robust error handling prevents startup failures.The comprehensive error handling that logs errors without throwing exceptions is well-designed to prevent application startup failures while still providing visibility into index creation issues.
app.ts (1)
10-10
: Appropriate integration of index creation during startup.The index creation is correctly placed after MongoDB connection establishment and before other initialization steps, ensuring optimal query performance from the start.
Also applies to: 31-33
util/communityHelper.ts (1)
17-66
: Excellent consolidation of community lookup logic.The
getCommunityInfo
function effectively consolidates multiple database queries into a single, reusable utility. The logic handles both group and private chat contexts appropriately, and the automatic cleanup of invalid default community IDs is a nice touch.bot/modules/orders/commands.ts (2)
194-196
: Correct migration from deprecated Order.count to Order.countDocuments.Good job updating to use
countDocuments
instead of the deprecatedcount
method, and fixing the query to properly filter by status.
48-80
: Excellent refactoring to consolidate community lookup logic.The refactoring successfully:
- Reduces multiple database queries to a single optimized lookup
- Centralizes ban checking logic
- Improves code maintainability and readability
- Properly handles edge cases (missing communities, deleted defaults)
This aligns perfectly with the PR's performance optimization goals.
Also applies to: 113-143
@coderabbitai review |
✅ Actions performedReview triggered.
|
Summary by CodeRabbit
New Features
Refactor
Bug Fixes
Chores