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
This commit is contained in:
Ben Halpern 2020-08-11 15:31:57 -04:00 committed by GitHub
parent d337f5ee41
commit 93e442a3e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 9 deletions

View file

@ -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

View file

@ -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