-
Notifications
You must be signed in to change notification settings - Fork 71
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
提出物一覧ページでの提出物の並び順を修正 #3228
提出物一覧ページでの提出物の並び順を修正 #3228
Conversation
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.
@udaikue
お手すきでレビューお願いします🙏
scope :list, lambda { | ||
with_avatar | ||
.preload([:practice, :comments, { checks: { user: { avatar_attachment: :blob } } }]) | ||
.order(created_at: :desc) | ||
.preload(:practice, | ||
:comments, | ||
{ user: :company }, | ||
{ checks: { user: { avatar_attachment: :blob } } }) | ||
} | ||
scope :reorder_for_not_responded_products, -> { reorder(published_at: :desc, id: :desc) } | ||
scope :order_for_list, -> { order(created_at: :desc, id: :desc) } | ||
scope :order_for_not_wip_list, -> { order(published_at: :desc, id: :desc) } |
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.
https://github.com/fjordllc/bootcamp/pull/2280/files#r585987542
で、orderとorder以外のスコープは分離したいという意図を感じたので、以下のように変更しました
Product.list
、Product.not_responded_products
からorderは削除- orderは別個のスコープ
order_for_list
とorder_for_not_wip_list
として切り出し Product.list
、Product.not_responded_products
を使っていた既存のコードで、並び替えが必要そうな箇所ではorder_for_list
かorder_for_not_wip_list
を呼び出すようにする
一通りコード検索した限り、 Product.list
、 Product.not_responded_products
を使ってorderし忘れている箇所は無いと思います
一箇所だけorderを追加しなかった箇所はありますが、countしてるだけなので並び替えは不要かと思いました
Lines 25 to 29 in ae9fb5b
def not_responded_product_count | |
Rails.cache.fetch 'not_responded_product_count' do | |
Product.not_responded_products.count | |
end | |
end |
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.
今見返すとスコープが微妙な感じですね…
何か改善案があればお願いします🙇
list
というスコープ名が適切ではない(preloadしてるだけなので)。でも代案が思いつかないlist
したあとにorder_for_list
もしくはorder_for_not_wip_list
の呼び出しが推奨される、という暗黙のルールが存在してしまっている。とはいえlist
の中にorderを入れると、作成日順か提出日順かを自由に選べなくなる
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.
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.
preloadした結果をどういう名前で表せばいいのかよく分からなくなってしまったんですよね…
一旦これで行こうと思います🙇
.order(created_at: :desc) | ||
.preload(:practice, | ||
:comments, | ||
{ user: :company }, |
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.
bullet(SQLのN+1を検知するgem)が警告を吐いていたので { user: :company }
を追加しました
.includes( | ||
:practice, | ||
{ comments: { user: :avatar_attachment } }, | ||
{ user: [{ avatar_attachment: :blob }, :company] }, | ||
{ checks: { user: [:company] } } | ||
) | ||
.order(published_at: :desc) | ||
.list | ||
.order_for_list |
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.
全ての提出物タブでの提出物表示用に色々includesしていた箇所ですが、全部必要には見えなかったので、bullet.logを見つつ list
スコープで置換しました
新しくN+1の警告が増えている箇所はありません
この状態で全ての提出物タブ( http://localhost:3000/products )にアクセスすると、bullet.logにincludes追加の警告が一つだけ出ます
listスコープでの置換前のincludesでも同じ警告が出ている + productとreportに直接のrelationが無いので、どう修正したものか分からずに困ってます
パフォーマンス問題なので、露骨に影響が出なければ一旦放置でいいかなと思っています
2021-09-05 16:27:01[WARN] user: konaga
GET /api/reports/recents.json
USE eager loading detected
Report => [:checks]
Add to your query: .includes([:checks])
Call stack
/home/konaga/environment/fjord/bootcamp/app/views/api/reports/recents/index.json.jbuilder:6:in `block in _app_views_api_reports_recents_index_json_jbuilder___3742612758457132164_46982601130400'
/home/konaga/environment/fjord/bootcamp/app/views/api/reports/recents/index.json.jbuilder:1:in `_app_views_api_reports_recents_index_json_jbuilder___3742612758457132164_46982601130400'
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.
まずはscopeを使わずにN+1がでないような状態を目指してやってみてください〜
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.
わかりづらくてすみません、scopeは使う・使わないどっちでも良いので、もうちょっとN+1がなくなるように頑張ってみてください〜
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.
910fc38
右サイドメニューの直近の日報一覧でN+1が生じていたみたいです
提出物とは直接関係ありませんが、ついでなので潰しました
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.
@konaga-k ありがとうございます!!
@@ -0,0 +1,59 @@ | |||
# frozen_string_literal: true |
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.
提出物の未アサインタブはSystem Test自体が存在していなかったので、他のタブのSystem Testを参考にして新規作成しました
# id順で並べたときの最初と最後の提出物を、提出日順で見たときに最新と最古になるように入れ替える | ||
Product.update_all(created_at: 1.day.ago, published_at: 1.day.ago) # rubocop:disable Rails/SkipsModelValidations | ||
# 最古の提出物を画面上で判定するため、提出物を1ページ内に収める | ||
Product.unassigned.unchecked.not_wip.limit(Product.count - PAGINATES_PER).delete_all |
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.
提出物一覧の先頭と最後尾を確認することで並び順を検証するテストですが、
提出物に2ページ名以降が発生すると、画面1ページ目の最後尾と、未アサイン提出物全体の最後尾が一致しなくなるため、
1ページに収まるようにレコードを削除しています
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.
@konaga-k こんな技があるんですね〜👏
products = Product | ||
.not_responded_products | ||
.list | ||
.order_for_not_wip_list |
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.
Discordのmentorチャンネル投稿用のAPIでもlistスコープが使われています
WIPを含まない一覧は提出日の降順で並べるという方針を一貫させたかったため、画面同様に提出日の降順に並び替えました
(そもそも並び替えしなくてもいいかもしれない…)
to: 駒形さん
このAPIでのproductsの並び順を作成日降順 -> 提出日降順に変えています
問題あれば教えてください🙇
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.
@konaga-k 問題ございません〜
レビュー、ちょっとお時間いただきます🙏 |
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.
完璧にはチェックできていないかもしれませんが、良いと思います!丁寧に考えられていて参考になります〜
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.
@udaikue
レビューありがとうございます🙏
scope :list, lambda { | ||
with_avatar | ||
.preload([:practice, :comments, { checks: { user: { avatar_attachment: :blob } } }]) | ||
.order(created_at: :desc) | ||
.preload(:practice, | ||
:comments, | ||
{ user: :company }, | ||
{ checks: { user: { avatar_attachment: :blob } } }) | ||
} | ||
scope :reorder_for_not_responded_products, -> { reorder(published_at: :desc, id: :desc) } | ||
scope :order_for_list, -> { order(created_at: :desc, id: :desc) } | ||
scope :order_for_not_wip_list, -> { order(published_at: :desc, id: :desc) } |
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.
preloadした結果をどういう名前で表せばいいのかよく分からなくなってしまったんですよね…
一旦これで行こうと思います🙇
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.
@komagata
レビューお願いします🙏
.includes( | ||
:practice, | ||
{ comments: { user: :avatar_attachment } }, | ||
{ user: [{ avatar_attachment: :blob }, :company] }, | ||
{ checks: { user: [:company] } } | ||
) | ||
.order(published_at: :desc) | ||
.list | ||
.order_for_list |
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.
まずはscopeを使わずにN+1がでないような状態を目指してやってみてください〜
@@ -2,6 +2,8 @@ | |||
|
|||
require 'application_system_test_case' | |||
|
|||
PAGINATES_PER = 50 |
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.
できるだけsocpeが狭い方がいいのでProductsTestの中に入れちゃっていいと思います〜
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.
5a1bebf
すみません、定数を中に入れ忘れてました🙇
dd1cf1b
to
910fc38
Compare
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.
@komagata
修正しました〜
.includes( | ||
:practice, | ||
{ comments: { user: :avatar_attachment } }, | ||
{ user: [{ avatar_attachment: :blob }, :company] }, | ||
{ checks: { user: [:company] } } | ||
) | ||
.order(published_at: :desc) | ||
.list | ||
.order_for_list |
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.
910fc38
右サイドメニューの直近の日報一覧でN+1が生じていたみたいです
提出物とは直接関係ありませんが、ついでなので潰しました
ありがとうございます! |
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.
conflictの解消お願いします〜
@konaga-k マージコミットがいくつかありますが、これは避けられないものですかね?(bootcampではrebaseで取り込むのを基本にしているため) |
abb9dc0
to
9cd2dfd
Compare
@komagata |
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.
確認しました、OKですー🙆♂️
refs #3197
概要
提出物一覧ページの提出物の並び順を以下のように修正
現在の提出物の並び順
本PRでの提出物の並び順
変更意図は以下の通り
他に並び替えが必要であれば別issueで対応
UI