Skip to content

Conversation

Copy link

Copilot AI commented Dec 3, 2025

Technical Summary

Comprehensive analysis of the Connect-ID codebase identifying 14 performance issues, organized by priority with actionable fixes.

High Priority (5 issues):

  • N+1 queries in send_bulk_notification(), RetrieveMessageView, RetrieveNotificationView
  • Sequential external API calls in resend_notifications_for_undelivered_messages()
  • Synchronous blocking calls (Twilio, OpenChatStudio, Connect API) in request handlers

Medium Priority (5 issues):

  • Missing DB indexes on ConnectUser.phone_number+is_active, Message.received+direction, Notification.received
  • Loop with individual DB ops in AddCredential
  • Synchronous geocoding and S3 operations in request path

Low Priority (4 issues):

  • Client instantiation per request (Twilio, S3)
  • Python sorting instead of DB-level ORDER BY
  • O(n²) sorting algorithm in notification response

Example N+1 pattern identified:

# Current: N+1 queries
for user in users:
    fcm_device = FCMDevice.objects.filter(user=user, active=True).first()  # Query per user

# Recommended: Single prefetch
users = ConnectUser.objects.filter(...).prefetch_related(
    Prefetch('fcmdevice_set', queryset=FCMDevice.objects.filter(active=True), to_attr='active_devices')
)

Added PERFORMANCE.md with detailed analysis, code samples, and priority matrix.

Logging and monitoring

N/A - documentation only.

Safety Assurance

Safety story

  • Documentation-only change; no code modifications

  • All 207 existing tests pass

  • I am confident that this change will not break current and/or previous versions of CommCare apps

Automated test coverage

N/A - no code changes.

QA Plan

N/A - documentation only.

Labels & Review

  • The set of people pinged as reviewers is appropriate for the level of risk of the change

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • sutton-henson.com
    • Triggering command: /usr/bin/python python -m pytest --tb=short -q (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Investigate performance of code in codebase</issue_title>
<issue_description>Look over the whole codebase and compile a list of performance issues that we need to address. Triage the list and provide clear actionable steps.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Investigate performance issues in the codebase Performance Analysis: Document codebase performance issues with prioritized recommendations Dec 3, 2025
Copilot AI requested a review from Charl1996 December 3, 2025 13:17
```python
for channel in channels:
# ...
channel_messages = Message.objects.filter( # Separate query for each channel!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number of channels is not much so this is probably not that important


# Send one aggregated notification per user
for username, messages in user_messages.items():
# Send summary notification instead of individual ones
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suggesting a change in feature behaviour, so can't implement this


**Impact:** For bulk notifications to N users, this results in N+1 database queries instead of 2 optimized queries.

**Recommendation:**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in #215

```python
def get(self, request, *args, **kwargs):
user = request.user
channels = Channel.objects.filter(connect_user=user).select_related("server")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in #215

@coderabbitai coderabbitai bot mentioned this pull request Jan 1, 2026
2 tasks
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.

Investigate performance of code in codebase

3 participants