Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
2b45fecd
Unverified
Commit
2b45fecd
authored
1 year ago
by
Claire
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix multiple N+1s in ConversationsController (#25134)
parent
675672fe
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/controllers/api/v1/conversations_controller.rb
+15
-2
15 additions, 2 deletions
app/controllers/api/v1/conversations_controller.rb
app/models/account_conversation.rb
+28
-10
28 additions, 10 deletions
app/models/account_conversation.rb
with
43 additions
and
12 deletions
app/controllers/api/v1/conversations_controller.rb
+
15
−
2
View file @
2b45fecd
...
...
@@ -11,7 +11,7 @@ class Api::V1::ConversationsController < Api::BaseController
def
index
@conversations
=
paginated_conversations
render
json:
@conversations
,
each_serializer:
REST
::
ConversationSerializer
render
json:
@conversations
,
each_serializer:
REST
::
ConversationSerializer
,
relationships:
StatusRelationshipsPresenter
.
new
(
@conversations
.
map
(
&
:last_status
),
current_user
&
.
account_id
)
end
def
read
...
...
@@ -32,7 +32,20 @@ class Api::V1::ConversationsController < Api::BaseController
def
paginated_conversations
AccountConversation
.
where
(
account:
current_account
)
.
to_a_paginated_by_id
(
limit_param
(
LIMIT
),
params_slice
(
:max_id
,
:since_id
,
:min_id
))
.
includes
(
account: :account_stat
,
last_status:
[
:media_attachments
,
:preview_cards
,
:status_stat
,
:tags
,
{
active_mentions:
[
account: :account_stat
],
account: :account_stat
,
},
]
)
.
to_a_paginated_by_id
(
limit_param
(
LIMIT
),
**
params_slice
(
:max_id
,
:since_id
,
:min_id
))
end
def
insert_pagination_headers
...
...
This diff is collapsed.
Click to expand it.
app/models/account_conversation.rb
+
28
−
10
View file @
2b45fecd
...
...
@@ -17,6 +17,8 @@
class
AccountConversation
<
ApplicationRecord
include
Redisable
attr_writer
:participant_accounts
before_validation
:set_last_status
after_commit
:push_to_streaming_api
...
...
@@ -26,24 +28,40 @@ class AccountConversation < ApplicationRecord
def
participant_account_ids
=
(
arr
)
self
[
:participant_account_ids
]
=
arr
.
sort
@participant_accounts
=
nil
end
def
participant_accounts
if
participant_account_ids
.
empty?
[
account
]
else
participants
=
Account
.
where
(
id:
participant_account_ids
)
participants
.
empty?
?
[
account
]
:
participants
@participant_accounts
||=
begin
if
participant_account_ids
.
empty?
[
account
]
else
participants
=
Account
.
where
(
id:
participant_account_ids
).
to_a
participants
.
empty?
?
[
account
]
:
participants
end
end
end
class
<<
self
def
to_a_paginated_by_id
(
limit
,
options
=
{})
if
options
[
:min_id
]
paginate_by_min_id
(
limit
,
options
[
:min_id
],
options
[
:max_id
]).
reverse
else
paginate_by_max_id
(
limit
,
options
[
:max_id
],
options
[
:since_id
]).
to_a
def
to_a_paginated_by_id
(
limit
,
min_id:
nil
,
max_id:
nil
,
since_id:
nil
,
preload_participants:
true
)
array
=
begin
if
min_id
paginate_by_min_id
(
limit
,
min_id
,
max_id
).
reverse
else
paginate_by_max_id
(
limit
,
max_id
,
since_id
).
to_a
end
end
if
preload_participants
participant_ids
=
array
.
flat_map
(
&
:participant_account_ids
)
accounts_by_id
=
Account
.
where
(
id:
participant_ids
).
index_by
(
&
:id
)
array
.
each
do
|
conversation
|
conversation
.
participant_accounts
=
conversation
.
participant_account_ids
.
filter_map
{
|
id
|
accounts_by_id
[
id
]
}
end
end
array
end
def
paginate_by_min_id
(
limit
,
min_id
=
nil
,
max_id
=
nil
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment