From 1f73a3ba6d5df69a18faaf7d3cc53442914eec91 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 6 Jun 2019 15:44:55 -0400 Subject: [PATCH] FIX: round the calculated heat values Views heats like 12135 will become 12000, like ratios like 1.666666666667 will become 1.67. --- app/services/heat_settings_updater.rb | 15 +++++++++++++-- spec/services/heat_settings_updater_spec.rb | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/services/heat_settings_updater.rb b/app/services/heat_settings_updater.rb index 7d2ece2c053..56666b71f47 100644 --- a/app/services/heat_settings_updater.rb +++ b/app/services/heat_settings_updater.rb @@ -73,8 +73,19 @@ class HeatSettingsUpdater elsif SiteSetting.get(name) == 0 || (new_value.to_f / SiteSetting.get(name) - 1.0).abs >= 0.05 - if SiteSetting.get(name) != new_value - SiteSetting.set_and_log(name, new_value) + rounded_new_value = if new_value.is_a?(Integer) + if new_value > 9 + digits = new_value.digits.reverse + (digits[0] * 10 + digits[1]) * 10.pow(digits[2..-1].size) + else + new_value + end + else + new_value.round(2) + end + + if SiteSetting.get(name) != rounded_new_value + SiteSetting.set_and_log(name, rounded_new_value) end end end diff --git a/spec/services/heat_settings_updater_spec.rb b/spec/services/heat_settings_updater_spec.rb index db32634fcaa..4f86390e50e 100644 --- a/spec/services/heat_settings_updater_spec.rb +++ b/spec/services/heat_settings_updater_spec.rb @@ -75,14 +75,14 @@ describe HeatSettingsUpdater do context 'increased activity' do let!(:hottest_topic1) { Fabricate(:topic, views: 10_100, posts_count: 100, like_count: 230) } - let!(:hottest_topic2) { Fabricate(:topic, views: 10_000, posts_count: 100, like_count: 220) } - let!(:warm_topic1) { Fabricate(:topic, views: 4020, posts_count: 100, like_count: 126) } - let!(:warm_topic2) { Fabricate(:topic, views: 4010, posts_count: 100, like_count: 116) } - let!(:warm_topic3) { Fabricate(:topic, views: 4000, posts_count: 100, like_count: 106) } - let!(:lukewarm_topic1) { Fabricate(:topic, views: 2040, posts_count: 100, like_count: 84) } - let!(:lukewarm_topic2) { Fabricate(:topic, views: 2030, posts_count: 100, like_count: 74) } - let!(:lukewarm_topic3) { Fabricate(:topic, views: 2020, posts_count: 100, like_count: 64) } - let!(:lukewarm_topic4) { Fabricate(:topic, views: 2000, posts_count: 100, like_count: 54) } + let!(:hottest_topic2) { Fabricate(:topic, views: 10_012, posts_count: 100, like_count: 220) } + let!(:warm_topic1) { Fabricate(:topic, views: 4020, posts_count: 99, like_count: 126) } + let!(:warm_topic2) { Fabricate(:topic, views: 4010, posts_count: 99, like_count: 116) } + let!(:warm_topic3) { Fabricate(:topic, views: 4005, posts_count: 99, like_count: 106) } + let!(:lukewarm_topic1) { Fabricate(:topic, views: 2040, posts_count: 99, like_count: 84) } + let!(:lukewarm_topic2) { Fabricate(:topic, views: 2030, posts_count: 99, like_count: 74) } + let!(:lukewarm_topic3) { Fabricate(:topic, views: 2020, posts_count: 99, like_count: 64) } + let!(:lukewarm_topic4) { Fabricate(:topic, views: 2002, posts_count: 99, like_count: 54) } let!(:cold_topic) { Fabricate(:topic, views: 100, posts_count: 100, like_count: 1) } it 'changes settings when difference is significant' do @@ -93,8 +93,8 @@ describe HeatSettingsUpdater do expect(SiteSetting.topic_views_heat_medium).to eq(4000) expect(SiteSetting.topic_views_heat_low).to eq(2000) expect(SiteSetting.topic_post_like_heat_high).to eq(2.2) - expect(SiteSetting.topic_post_like_heat_medium).to eq(1.06) - expect(SiteSetting.topic_post_like_heat_low).to eq(0.54) + expect(SiteSetting.topic_post_like_heat_medium).to eq(1.07) + expect(SiteSetting.topic_post_like_heat_low).to eq(0.55) end it "doesn't change settings when automatic_topic_heat_values is false" do