From 93e442a3e9264197d930e39dffad1b4ad9c4dd40 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 11 Aug 2020 15:31:57 -0400 Subject: [PATCH] Create option for calculating score without external black box (#9735) * Create option for calculating score without external black box * Create option for calculating score without external black box * Fix article not present error * Modify tests * Fix tests * Fix stubbing in tests * Fix function caller contingency --- app/black_box/black_box.rb | 23 +++++++++++++++---- spec/black_box/black_box_spec.rb | 39 ++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/app/black_box/black_box.rb b/app/black_box/black_box.rb index 480bac817..5433a91d5 100644 --- a/app/black_box/black_box.rb +++ b/app/black_box/black_box.rb @@ -16,10 +16,7 @@ class BlackBox reaction_points = (reaction_points * 0.8).to_i # watercooler posts shouldn't get as much love in feed end - article_hotness = function_caller.call( - "blackbox-production-articleHotness", - { article: article, user: article.user }.to_json, - ).to_i + article_hotness = last_mile_hotness_calc(article, function_caller) ( article_hotness + reaction_points + recency_bonus + super_recent_bonus + @@ -38,6 +35,8 @@ class BlackBox def calculate_spaminess(story, function_caller = FunctionCaller) # accepts comment or article as story return 100 unless story.user + return 0 if ENV["AWS_SDK_KEY"].blank? # Skip this if we don't have a private spam score for now + return 0 if ENV["AWS_SDK_KEY"] == "foobarbaz" # Also skip if placeholder function_caller.call("blackbox-production-spamScore", { story: story, user: story.user }.to_json).to_i @@ -50,5 +49,21 @@ class BlackBox code_bonus = body_markdown.include?("`") ? 1 : 0 size_bonus + code_bonus end + + def last_mile_hotness_calc(article, function_caller) + if ENV["AWS_SDK_KEY"].present? && ENV["AWS_SDK_KEY"] != "foobarbaz" + function_caller.call( + "blackbox-production-articleHotness", + { article: article, user: article.user }.to_json, + ).to_i + else + # Simple calculation that takes in published at time and scores + # Same order of magnitude calculation as existing private function + # Gives credit to new articles and articles which score well from users and mods + article.published_at.to_i / 10_000 + + (article.score * 5000) + + (article.comment_score * 5000) + end + end end end diff --git a/spec/black_box/black_box_spec.rb b/spec/black_box/black_box_spec.rb index b0f44cf46..b355edf00 100644 --- a/spec/black_box/black_box_spec.rb +++ b/spec/black_box/black_box_spec.rb @@ -6,10 +6,20 @@ RSpec.describe BlackBox, type: :black_box do describe "#article_hotness_score" do let!(:article) { build_stubbed(:article, published_at: Time.current) } - it "calls function caller" do + it "calls function caller if AWS_SDK_KEY present" do + ENV["AWS_SDK_KEY"] = "valid_key" allow(function_caller).to receive(:call).and_return(5) described_class.article_hotness_score(article, function_caller) expect(function_caller).to have_received(:call).once + ENV["AWS_SDK_KEY"] = nil + end + + it "does not call function caller if AWS_SDK_KEY is placeholder" do + ENV["AWS_SDK_KEY"] = "foobarbaz" + allow(function_caller).to receive(:call).and_return(5) + described_class.article_hotness_score(article, function_caller) + expect(function_caller).not_to have_received(:call) + ENV["AWS_SDK_KEY"] = nil end it "doesn't fail when function caller returns nil" do @@ -24,7 +34,7 @@ RSpec.describe BlackBox, type: :black_box do # + score (99) # + value from the function caller (5) score = described_class.article_hotness_score(article, function_caller) - expect(score).to eq(3048) + expect(score).to eq(657_758) end it "returns the lower correct value if article tagged with watercooler" do @@ -35,7 +45,7 @@ RSpec.describe BlackBox, type: :black_box do # + score (99) # + value from the function caller (5) score = described_class.article_hotness_score(article, function_caller) - expect(score).to be < 3040 # lower because watercooler tag + expect(score).to be < 657_758 # lower because watercooler tag end end @@ -69,15 +79,34 @@ RSpec.describe BlackBox, type: :black_box do expect(function_caller).not_to have_received(:call) end - it "calls the function_caller" do + it "calls the function_caller if AWS_SDK_KEY present" do + ENV["AWS_SDK_KEY"] = "valid_key" described_class.calculate_spaminess(comment, function_caller) expect(function_caller).to have_received(:call).with("blackbox-production-spamScore", { story: comment, user: user }.to_json).once + ENV["AWS_SDK_KEY"] = nil end - it "returns the value that the caller returns" do + it "does not call the function_caller if AWS_SDK_KEY is placeholder" do + ENV["AWS_SDK_KEY"] = "foobarbaz" + described_class.calculate_spaminess(comment, function_caller) + expect(function_caller).not_to have_received(:call).with("blackbox-production-spamScore", + { story: comment, user: user }.to_json) + ENV["AWS_SDK_KEY"] = nil + end + + it "returns the function_caller spaminess if AWS_SDK_KEY present" do + ENV["AWS_SDK_KEY"] = "valid_key" spaminess = described_class.calculate_spaminess(comment, function_caller) expect(spaminess).to eq(1) + ENV["AWS_SDK_KEY"] = nil + end + + it "returns the default retrun value if AWS_SDK_KEY is placeholder" do + ENV["AWS_SDK_KEY"] = "foobarbaz" + spaminess = described_class.calculate_spaminess(comment, function_caller) + expect(spaminess).to eq(0) + ENV["AWS_SDK_KEY"] = nil end end end