Skip to content

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

Merged
merged 5 commits into from
Jul 1, 2025
Merged

Conversation

grunch
Copy link
Member

@grunch grunch commented Jul 1, 2025

  • 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()

Summary by CodeRabbit

  • New Features

    • Improved database performance by automatically creating optimized indexes on startup.
    • Added new utility functions for streamlined community lookup and currency support checks.
  • Refactor

    • Centralized and simplified community-related logic in order commands for better reliability and maintainability.
  • Bug Fixes

    • Enhanced error handling and validation for missing or banned communities during order operations.
    • Corrected order counting logic to ensure accurate pending order checks.
  • Chores

    • Updated internal interfaces to allow optional community IDs when creating orders.

- 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.
Copy link
Contributor

coderabbitai bot commented Jul 1, 2025

Walkthrough

The 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

File(s) Change Summary
app.ts, models/indexes.ts Added database index creation step during startup via new createIndexes function.
bot/modules/orders/commands.ts Refactored order commands to use centralized community lookup and currency support helpers.
util/communityHelper.ts New module: provides getCommunityInfo and isCurrencySupported utility functions.
bot/ordersActions.ts Made community_id optional in order arguments; updated fee calculation to handle undefined values.

Suggested reviewers

  • Catrya

Poem

In fields of code, a rabbit hops,
Building indexes—no time for stops!
Helpers for communities, swift and neat,
Orders refined, the logic complete.
With every hop, the queries fly,
🐇 Database dreams reach for the sky!

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-07-01T12_47_22_254Z-debug-0.log


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8cd40a3 and 8512830.

📒 Files selected for processing (2)
  • models/indexes.ts (1 hunks)
  • util/communityHelper.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • util/communityHelper.ts
  • models/indexes.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch performance-optimizations

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 use getCommunityInfo.

-import { Community, Order } from '../../../models';
+import { Order } from '../../../models';
🧹 Nitpick comments (9)
models/indexes.ts (6)

7-27: Consider using proper MongoDB types instead of any.

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 to undefined 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

📥 Commits

Reviewing files that changed from the base of the PR and between bee0835 and 75252c1.

📒 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 and status 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 deprecated count 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

@grunch
Copy link
Member Author

grunch commented Jul 1, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jul 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@grunch grunch merged commit 231a7e6 into main Jul 1, 2025
7 checks passed
@grunch grunch deleted the performance-optimizations branch July 1, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant