Skip to content

Commit 4805001

Browse files
authored
perf(users): further optimize /api/v2/users list endpoint for large datasets DEV-1236 (#6450)
### 📣 Summary Fix remaining performance issues on the `/api/v2/users` endpoint when handling very large datasets not covered by #6447 ### 📖 Description Although PR #6447 significantly improved the /api/v2/users list performance, slow responses were still observed with very large datasets. This update introduces additional optimizations to handle those edge cases efficiently.
1 parent be3de51 commit 4805001

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

kpi/views/v2/user.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def get_queryset(self, *args, **kwargs):
8888
self.queryset = (
8989
self.queryset.exclude(pk=settings.ANONYMOUS_USER_ID)
9090
.select_related('extra_details')
91-
.annotate(assets_count=Count('assets', distinct=True))
9291
.order_by('id')
9392
)
9493

@@ -106,6 +105,28 @@ def list(self, request, *args, **kwargs):
106105

107106
return super().list(request, *args, **kwargs)
108107

108+
def paginate_queryset(self, queryset):
109+
if not (page := super().paginate_queryset(queryset)):
110+
return None
111+
112+
user_ids = (user.pk for user in page)
113+
counts_map = {
114+
asset['owner_id']: asset['assets_count']
115+
for asset in (
116+
Asset.objects.filter(owner_id__in=user_ids)
117+
.values('owner_id')
118+
.annotate(assets_count=Count('id'))
119+
)
120+
}
121+
122+
def _page_generator():
123+
# Inject and yield on-the-fly assets count
124+
for user in page:
125+
user.assets_count = counts_map.get(user.pk, 0)
126+
yield user
127+
128+
return _page_generator()
129+
109130
@action(detail=True, methods=['GET'],
110131
url_path=r'migrate(?:/(?P<task_id>[\d\w\-]+))?')
111132
def migrate(self, request, task_id: str = None, **kwargs):

0 commit comments

Comments
 (0)