Skip to content
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

Improve video controllers caching and retrieving #5709

Merged
merged 7 commits into from
Jan 27, 2020
Merged

Improve video controllers caching and retrieving #5709

merged 7 commits into from
Jan 27, 2020

Conversation

rhymes
Copy link
Contributor

@rhymes rhymes commented Jan 24, 2020

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

This PR has been a little bit of a ride, through it I found out we have some inefficiencies in https://dev.to/videos (and likely other pages) where we fetch the same page (made by 24 items) 3 times (once in the server and twice in the frontend).

This PR though addresses another set of things:

  • /api/videos fetches only the fields it needs to render the JSON, doesn't do a N + 1 on the user, has flexible pagination and sets the correct edge caching, removing server side caching

  • /videos fetches only the fields needed, removes the SQL queries that were issued in the partial resulting in N + 1 and sets the correct edge caching

I did not address the issues with multiple fetchings in /videos as it's outside the scope of this PR and it's probably a much bigger refactoring.

The PR is essentially done but it incorporates #5673 so I'm tagging this as WIP until that one is merged and then rebase and force push this one

@rhymes rhymes added the blocked blocked by internal or external issues label Jan 24, 2020
@rhymes rhymes requested review from a team and removed request for a team January 24, 2020 17:44
@rhymes
Copy link
Contributor Author

rhymes commented Jan 24, 2020

Writing down some of my discoveries, I'll open a separate issues once I get to the bottom of these:

if you notice insertNext insertVideos and insertArticles (though in a way I'm not understanding) in app/assets/javascripts/initializers/initScrolling.js.erb check if the item has already been inserted:

https://github.com/thepracticaldev/dev.to/blob/3412af977f26a69cb271095808b43b40db229ef2/app/assets/javascripts/initializers/initScrolling.js.erb#L36-L42

https://github.com/thepracticaldev/dev.to/blob/3412af977f26a69cb271095808b43b40db229ef2/app/assets/javascripts/initializers/initScrolling.js.erb#L130-L136

also the fetchers fetch from page 0 and then page 1 which are the same page

Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

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

🔥

app/controllers/api/v0/videos_controller.rb Show resolved Hide resolved
num = [per_page, 1000].min

@video_articles = Article.with_video.
includes([:user]).
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@@ -30,7 +30,7 @@
<span class="video-timestamp"><%= video_article.video_duration_in_minutes %></span>
</div>
<p><strong><%= video_article.title %></strong></p>
<p class="video-username"><%= User.find(video_article.user_id).name %></p>
Copy link
Contributor

Choose a reason for hiding this comment

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

NICE find!

@rhymes rhymes changed the title [WIP] Improve video controllers caching and retrieving Improve video controllers caching and retrieving Jan 26, 2020
@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Jan 26, 2020
@rhymes rhymes requested a review from mstruve January 26, 2020 21:16
@rhymes rhymes removed the blocked blocked by internal or external issues label Jan 26, 2020
Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

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

🔥

@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Jan 27, 2020
Copy link
Contributor

@joshpuetz joshpuetz left a comment

Choose a reason for hiding this comment

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

Very nice!

where("score > ?", -4).
page = params[:page]
per_page = (params[:per_page] || 24).to_i
num = [per_page, 1000].min
Copy link
Contributor

Choose a reason for hiding this comment

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

This is lovely refactoring!

@@ -138,6 +138,8 @@ class Article < ApplicationRecord

scope :feed, -> { published.select(:id, :published_at, :processed_html, :user_id, :organization_id, :title, :path) }

scope :with_video, -> { published.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).where("score > ?", -4) }
Copy link
Contributor

Choose a reason for hiding this comment

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

😍

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@vaidehijoshi vaidehijoshi left a comment

Choose a reason for hiding this comment

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

great work, and great catch on fixing some of the funny stuff that was happening here! ✨

@@ -138,6 +138,8 @@ class Article < ApplicationRecord

scope :feed, -> { published.select(:id, :published_at, :processed_html, :user_id, :organization_id, :title, :path) }

scope :with_video, -> { published.where.not(video: [nil, ""], video_thumbnail_url: [nil, ""]).where("score > ?", -4) }
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -1,21 +1,25 @@
module Api
module V0
class VideosController < ApiController
caches_action :index,
Copy link
Contributor

Choose a reason for hiding this comment

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

Lol, I had never seen caches_action before and had to look up where it was coming from 🙈

TIL about actionpack-action_caching and the fact that we use caches_action in podcast_episodes_controller and tags_controller. I'm assuming we can move away from that and just use set_cache_control_headers in place of it?

Copy link
Contributor Author

@rhymes rhymes Jan 27, 2020

Choose a reason for hiding this comment

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

@vaidehijoshi this is exactly what i've been doing with these latest PRs :) only a few endpoints left and we can remove that gem :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Rad!! Thanks for doing this 🙌

@benhalpern benhalpern merged commit 397ef31 into forem:master Jan 27, 2020
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: reviewed-approved bot applied label for PR's where reviewer approves changes labels Jan 27, 2020
@rhymes rhymes deleted the rhymes/improve-videos-controllers branch January 28, 2020 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants