Improve feed and add more randomness (#1570)

* Improve feed and add more randomness

* Fix tests

* Fix test score calc

* Modify home page test criteria
This commit is contained in:
Ben Halpern 2019-01-16 16:01:14 -05:00 committed by GitHub
parent 5f051d9512
commit b1f18f5bbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 14 deletions

View file

@ -45,6 +45,12 @@ function insertNewArticles(user){
if (containsOrganizationID) {
articlePoints = articlePoints + 16
}
var rand = Math.random();
if (rand < 0.3) {
articlePoints = articlePoints + 1
} else if (rand < 0.6) {
articlePoints = articlePoints + 2
}
article['points'] = articlePoints
});
var sortedArticles = articlesJSON.sort(function(a, b) {
@ -82,6 +88,12 @@ function insertTopArticles(user){
if (containsOrganizationID) {
articlePoints = articlePoints + 1
}
var rand = Math.random();
if (rand < 0.3) {
articlePoints = articlePoints + 1
} else if (rand < 0.6) {
articlePoints = articlePoints + 2
}
article['points'] = articlePoints
});
var sortedArticles = articlesJSON.sort(function(a, b) {
@ -107,7 +119,6 @@ function algoliaFollowedArticles(){
var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>'
var client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey);
var index = client.initIndex('ordered_articles_<%= Rails.env %>');
var searchObj = {
hitsPerPage: 20,
page: "0",

View file

@ -4,11 +4,11 @@ class BlackBox
return (article.featured_number || 10000) / 10000 unless Rails.env.production?
reaction_points = article.score
super_super_recent_bonus = article.published_at > 1.hours.ago ? 18 : 0
super_recent_bonus = article.published_at > 3.hours.ago ? 11 : 0
recency_bonus = article.published_at > 11.hours.ago ? 70 : 0
today_bonus = article.published_at > 26.hours.ago ? 285 : 0
two_day_bonus = article.published_at > 48.hours.ago ? 120 : 0
super_super_recent_bonus = article.published_at > 1.hours.ago ? 28 : 0
super_recent_bonus = article.published_at > 8.hours.ago ? 31 : 0
recency_bonus = article.published_at > 12.hours.ago ? 70 : 0
today_bonus = article.published_at > 26.hours.ago ? 385 : 0
two_day_bonus = article.published_at > 48.hours.ago ? 320 : 0
FunctionCaller.new("blackbox-production-articleHotness",
{ article: article, user: article.user }.to_json).call +
reaction_points + recency_bonus + super_recent_bonus + super_super_recent_bonus + today_bonus + two_day_bonus

View file

@ -107,7 +107,7 @@ class StoriesController < ApplicationController
if ["week", "month", "year", "infinity"].include?(params[:timeframe])
@stories = @stories.where("published_at > ?", Timeframer.new(params[:timeframe]).datetime).
order("positive_reactions_count DESC")
order("score DESC")
@featured_story = @stories.where.not(main_image: nil).first&.decorate || Article.new
elsif params[:timeframe] == "latest"
@stories = @stories.order("published_at DESC").
@ -116,7 +116,7 @@ class StoriesController < ApplicationController
else
@default_home_feed = true
@stories = @stories.
where("reactions_count > ? OR featured = ?", 10, true).
where("score > ? OR featured = ?", 9, true).
order("hotness_score DESC")
if user_signed_in?
offset = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7].sample # random offset, weighted more towards zero
@ -124,10 +124,10 @@ class StoriesController < ApplicationController
end
@featured_story = @stories.where.not(main_image: nil).first&.decorate || Article.new
if user_signed_in?
@new_stories = Article.where("published_at > ? AND score > ?", 4.hours.ago, -30).
@new_stories = Article.where("published_at > ? AND score > ?", rand(2..6).hours.ago, -30).
where(published: true).
includes(:user).
limit(45).
limit(rand(15..60)).
order("published_at DESC").
limited_column_select.
decorate

View file

@ -84,7 +84,7 @@ class ArticleApiIndexService
def state_articles(state)
if state == "fresh"
Article.where(published: true).
where("positive_reactions_count < ? AND featured_number > ? AND score > ?", 3, 7.hours.ago.to_i, -2)
where("positive_reactions_count < ? AND featured_number > ? AND score > ?", 2, 7.hours.ago.to_i, -2)
elsif state == "rising"
Article.where(published: true).
where("positive_reactions_count > ? AND positive_reactions_count < ? AND featured_number > ?", 19, 33, 3.days.ago.to_i)

View file

@ -1,7 +1,7 @@
FactoryBot.define do
factory :article do
transient do
title { Faker::Book.title }
title { Faker::Book.title + rand(100).to_s }
published { true }
date { "01/01/2015" }
tags { Faker::Hipster.words(4).join(", ") }

View file

@ -1,8 +1,8 @@
require "rails_helper"
describe "User visits a homepage", type: :feature do
let!(:article) { create(:article, reactions_count: 12, featured: true) }
let!(:article2) { create(:article, reactions_count: 20) }
let!(:article) { create(:article, reactions_count: 12, score: 15, featured: true) }
let!(:article2) { create(:article, reactions_count: 20, score: 20, featured: true) }
let!(:bad_article) { create(:article, reactions_count: 0) }
context "when no options specified" do
@ -13,6 +13,8 @@ describe "User visits a homepage", type: :feature do
end
it "shows correct articles", js: true do
article.update_column(:score, 15)
article2.update_column(:score, 15)
expect(page).to have_selector(".single-article", count: 2)
expect(page).to have_text(article.title)
expect(page).to have_text(article2.title)