* Ensuring we don't track views of author or unpublished Prior to this commit, I was surprised to learn that we: 1) Tracked an author's view of their article. 2) Tracked views of an unpublished article. This came up from a Forem creator asking if they could reset the view counter. Or trigger the reset on publication. I think a general business logic policy of don't track views for the author and don't track views for unpublished articles is a reasonable default. Were we to pursue the clear views on publication, we'd need to consider something that went from unpublished -> published -> unpublished -> published. Without a more explicit state machine, triggering a busineiss logic behavior seems a bit unexpected. In other words, I wrote an article. There are 20 views when I realize that I need to unpublished it. I make the changes in the unpublished state, and re-publish. I'd assume that those 20 views would still be "recorded" and counted towards my article's view counts. * Adjusting condition structure Prior to this commit, the `if` clause was rather far to the right. This helps make the if clause more pronounced.
36 lines
1.3 KiB
Ruby
36 lines
1.3 KiB
Ruby
module Articles
|
|
class UpdatePageViewsWorker
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options queue: :medium_priority,
|
|
lock: :until_executing,
|
|
on_conflict: :replace,
|
|
retry: false
|
|
|
|
# @see Articles::PageViewUpdater
|
|
def perform(create_params)
|
|
article = Article.find_by(id: create_params["article_id"])
|
|
return unless article
|
|
return unless article.published?
|
|
return if create_params[:user_id] && article.user_id == create_params[:user_id]
|
|
|
|
PageView.create!(create_params)
|
|
|
|
updated_count = article.page_views.sum(:counts_for_number_of_views)
|
|
if updated_count > article.page_views_count
|
|
article.update_column(:page_views_count, updated_count)
|
|
end
|
|
|
|
# PageViewsController#create called the method update_organic_page_views
|
|
# at the end. The relationship between the two was 12.5% chance (rand(8))
|
|
# and 1% chance (rand(100)), or roughly 12x more likely for page view
|
|
# updates vs. organic page view updates. We kept a similar relationship
|
|
# between the two workers, this one here is schedule after 2 minutes,
|
|
# organic page view updates after 25 minutes.
|
|
Articles::UpdateOrganicPageViewsWorker.perform_at(
|
|
25.minutes.from_now,
|
|
article.id,
|
|
)
|
|
end
|
|
end
|
|
end
|