diff --git a/app/queries/comments/community_wellness_query.rb b/app/queries/comments/community_wellness_query.rb index 921dc5270..da2d63d96 100644 --- a/app/queries/comments/community_wellness_query.rb +++ b/app/queries/comments/community_wellness_query.rb @@ -18,27 +18,7 @@ module Comments class CommunityWellnessQuery def self.call - if limit_query_rollout? - ActiveRecord::Base.connection.execute(limit_release_date_sql_query).to_a - else - ActiveRecord::Base.connection.execute(sql_query).to_a - end - end - - # We are rolling out the badge feature to consider comments posted May 1st, - # 2022 or later. The logic here consists in checking if 33 weeks have passed - # since May 1st, 2022. The method should return `true` if we need to - # restrict the comment dates, and it will return `false` if we are good to - # carry on without that restriction (i.e. it's been more than 33 weeks since - # the rollout). - # - # We can officially remove this query+logic on December 19, 2022 at which - # time we will continue to use the default query. - # - # Read more about this here: - # https://github.com/forem/forem/issues/17310#issuecomment-1118554640 - def self.limit_query_rollout? - Time.zone.parse("2022-05-01") > 33.weeks.ago + ActiveRecord::Base.connection.execute(sql_query).to_a end def self.sql_query @@ -85,58 +65,5 @@ module Comments GROUP BY user_id SQL end - - # This query is an exact copy of the `sql_query` but includes a filter that - # forces the weeks to start counting from May 1st, 2022. This allows us to - # rollout this badge one week at a time without human intervention. - # - # We can officially remove this query+logic on December 19, 2022 at which - # time we will continue to use the default query. - def self.limit_release_date_sql_query - <<~SQL - SELECT user_id, - /* A comma separated string of "weeks_ago" */ - array_to_string(array_agg(weeks_ago), ',') AS serialized_weeks_ago, - /* A comma separated string of comment counts. The first value in this string happens on the week that is the first value in serialized_weeks_ago */ - array_to_string(array_agg(number_of_comments_with_positive_reaction), ',') AS serialized_comment_counts - FROM - ( - SELECT user_id, - COUNT(user_id) AS number_of_comments_with_positive_reaction, - /* Get the number of weeks, since today for posts */ - trunc((extract(epoch FROM (current_timestamp- created_at))) / 604800) AS weeks_ago - FROM comments - INNER JOIN - ( - SELECT DISTINCT reactable_id - FROM reactions - WHERE reactable_type = 'Comment' - AND created_at > (now() - interval '231' day) - EXCEPT - SELECT DISTINCT reactable_id - FROM reactions - WHERE reactable_type = 'Comment' - AND created_at > (now() - interval '231' day) - AND category IN ('thumbsdown', 'vomit')) AS negative_reactions - ON comments.id = negative_reactions.reactable_id - INNER JOIN - ( - SELECT count(id) AS number_of_comments, - user_id AS comment_counts_user_id - FROM comments - /* This interval filters week 1 (what we care about) */ - WHERE created_at >= (now() - interval '14' day) - AND created_at <= (now() - interval '7' day) - GROUP BY user_id) AS comment_counts - ON comments.user_id = comment_counts_user_id - AND comment_counts.number_of_comments > 1 - /* Don’t select anything older than 231 days ago, or 33 weeks ago */ - WHERE created_at > (now() - interval '231' day) - /* We will only awarded from this date forward (feature release date) */ - AND created_at > '2022-05-01' - GROUP BY user_id, weeks_ago) AS user_comment_counts_by_week - GROUP BY user_id - SQL - end end end diff --git a/spec/queries/comments/community_wellness_query_spec.rb b/spec/queries/comments/community_wellness_query_spec.rb index 1739b7096..8f9960fed 100644 --- a/spec/queries/comments/community_wellness_query_spec.rb +++ b/spec/queries/comments/community_wellness_query_spec.rb @@ -23,49 +23,8 @@ RSpec.describe Comments::CommunityWellnessQuery, type: :query do create_comment_time_ago(user4.id, 239.days.ago, commentable: articles.sample) end - # This context spec will likely start to fail when we are ready to remove the - # rollout limitation from the query+logic. Read more about this here: - # - # - https://github.com/forem/forem/issues/17310#issuecomment-1118554640 - # - https://github.com/forem/forem/blob/main/app/queries/comments/community_wellness_query.rb#L28 - context "when the current date is less than (precedes) December 19, 2022" do - it "uses the date-limited query" do - expect(described_class.limit_query_rollout?).to be true - end - - it "returns a max number of weeks that matches the diff from rollout date" do - user6 = create(:user) - - # Add 2 comments per week (including week 0) for `user6` - 34.times do |i| - num_days_ago = (2 + (i * 7)).days.ago - create_comment_time_ago(user6.id, num_days_ago, commentable: articles.sample) - create_comment_time_ago(user6.id, num_days_ago, commentable: articles.sample) - end - - # Fetch and de-serialize the results for `user6` - result = described_class.call - index6 = result.index { |hash| hash["user_id"] == user6.id } - weeks_ago_array = result[index6]["serialized_weeks_ago"].split(",") - comment_counts_array = result[index6]["serialized_comment_counts"].split(",") - - post_rollout_comments = user6.comments.where("created_at > ?", "2022-05-01").order(created_at: :asc) - oldest_comment_date_post_rollout = post_rollout_comments.first.created_at - - # If `1.34` weeks have passed we should get no more than 2 weeks in the - # result arrays because of the `limit_release_date_sql_query`. That's what - # is being calculated here and stored in `expected_weeks` - expected_weeks = ((Time.current - oldest_comment_date_post_rollout) / 7.days).ceil - - expect(weeks_ago_array.count).to eq(expected_weeks) - expect(comment_counts_array.count).to eq(expected_weeks) - end - end - context "when multiple users match criteria" do before do - allow(described_class).to receive(:limit_query_rollout?).and_return(false) - # User 1 - week 0 create_comment_time_ago(user1.id, 5.days.ago, commentable: articles.sample) create_comment_time_ago(user1.id, 6.days.ago, commentable: articles.sample) @@ -132,8 +91,6 @@ RSpec.describe Comments::CommunityWellnessQuery, type: :query do context "when users match criteria but mod reaction reduces their comment counts" do before do - allow(described_class).to receive(:limit_query_rollout?).and_return(false) - # User 1 - week 0 create_comment_time_ago(user1.id, 5.days.ago, commentable: articles.sample) create_comment_time_ago(user1.id, 6.days.ago, commentable: articles.sample) diff --git a/spec/services/badges/award_community_wellness_spec.rb b/spec/services/badges/award_community_wellness_spec.rb index a646da8a9..4dba157f1 100644 --- a/spec/services/badges/award_community_wellness_spec.rb +++ b/spec/services/badges/award_community_wellness_spec.rb @@ -9,8 +9,6 @@ RSpec.describe Badges::AwardCommunityWellness, type: :service do let!(:articles) { create_list(:article, 4) } before do - allow(Comments::CommunityWellnessQuery).to receive(:limit_query_rollout?).and_return(false) - reward_weeks.each do |week| create( :badge,