Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the trending algorithm tunable and add number of comments to inputs #472

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions app/Queries/Feeds/TrendingQuestionsFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,50 @@

use App\Models\Question;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Config;

final readonly class TrendingQuestionsFeed
{
/**
* The likes bias for the trending feed.
*/
private const int LIKES_BIAS = 1;

/**
* The comments bias for the trending feed.
*/
private const int COMMENTS_BIAS = 1;

/**
* The time bias for the trending feed.
*/
private const int TIME_BIAS = 86400;

/**
* The max days since posted for the trending feed.
*/
private const int MAX_DAYS_SINCE_POSTED = 7;

/**
* Get the query builder for the feed.
*
* @return Builder<Question>
*/
public function builder(): Builder
{
$likesBias = self::LIKES_BIAS;
$commentsBias = self::COMMENTS_BIAS;
$timeBias = self::TIME_BIAS;
$maxDaysSincePosted = self::MAX_DAYS_SINCE_POSTED;

return Question::query()
->withCount('likes')
->where('likes_count', '>', 1)
->orderByDesc('likes_count')
->where('is_reported', false)
->withCount('likes', 'children')
->orderByRaw(<<<SQL
(((likes_count * {$likesBias} + 1.0) * (children_count * {$commentsBias} + 1.0))
/ (unixepoch() - unixepoch(answer_created_at) + {$timeBias} + 1.0)) desc
SQL,
)->where('is_reported', false)
->where('is_ignored', false)
->where('answer_created_at', '>=', now()->subDays(7));
->where('answer_created_at', '>=', now()->subDays($maxDaysSincePosted));
}
}
83 changes: 60 additions & 23 deletions tests/Unit/Livewire/Home/TrendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\Like;
use App\Models\Question;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Livewire\Livewire;

test('renders trending questions', function () {
Expand Down Expand Up @@ -52,64 +54,99 @@
->assertDontSee($questionContent);
});

test('renders trending questions orderby trending score', function () {
// (likes * 0.8 + views * 0.2) / (minutes since answered + 1) = trending score
test('renders trending questions order by trending score', function () {
$this->travelTo($date = now());

// 0 likes, 0 comments, just posted
// (proves the algo works with zeroes)
Question::factory()
->hasLikes(5)
->create([
'content' => 'trending question 1',
'answer_created_at' => now()->subMinutes(10),
'answer_created_at' => $date,
'views' => 20,
]); // score = (5 * 0.8 + 20 * 0.2) / (10 + 1) = 0.72
]); // score = .00001157

// 0 likes, 0 comments, posted 10 minutes ago
Question::factory()
->hasLikes(5)
->create([
'content' => 'trending question 2',
'answer_created_at' => now()->subMinutes(20),
'answer_created_at' => $date->subMinutes(10),
'views' => 20,
]); // score = (5 * 0.8 + 20 * 0.2) / (20 + 1) = 0.38
]); // score = .00001149

// 1 like, 0 comments, posted 10 minutes ago
Question::factory()
->hasLikes(15)
->hasLikes(1)
->create([
'content' => 'trending question 3',
'answer_created_at' => now()->subMinutes(20),
'answer_created_at' => $date->subMinutes(10),
'views' => 100,
]); // score = (15 * 0.8 + 100 * 0.2) / (20 + 1) = 1.52
]); // score = .00002298

// 0 likes, 1 comment, posted 10 minutes ago (same score as above)
Question::factory()
->hasLikes(20)
->afterCreating(fn (Question $question) => Question::factory()->create([
'parent_id' => $question->id,
'content' => 'comment on question 4',
'answer_created_at' => $date->subMinutes(10),
]))
->create([
'content' => 'trending question 4',
'answer_created_at' => now()->subMinutes(20),
'answer_created_at' => $date->subMinutes(10),
'views' => 100,
]); // score = (20 * 0.8 + 100 * 0.2) / (20 + 1) = 1.71
]); // score = .00002298

// 1 like, 0 comments, posted 11 minutes ago (just below question 3)
Question::factory()
->hasLikes(50)
->hasLikes(1)
->create([
'content' => 'trending question 5',
'answer_created_at' => now()->subMinutes(30),
'answer_created_at' => $date->subMinutes(11),
'views' => 500,
]); // score = (50 * 0.8 + 500 * 0.2) / (30 + 1) = 4.51
]); // score = .00002297

// 1 like, 1 comment, posted 15 minutes ago
Question::factory()
->hasLikes(50)
->hasLikes(1)
->afterCreating(fn (Question $question) => Question::factory()->create([
'parent_id' => $question->id,
'content' => 'comment on question 6',
'answer_created_at' => $date->subMinutes(15),
]))
->create([
'content' => 'trending question 6',
'answer_created_at' => now()->subMinutes(30),
'answer_created_at' => $date->subMinutes(15),
'views' => 700,
]); // score = (50 * 0.8 + 700 * 0.2) / (30 + 1) = 5.36
]); // score = .0000459

// 20 likes, 0 comments, posted a day ago
Question::factory()
->hasLikes(20)
->create([
'content' => 'trending question 7',
'answer_created_at' => $date->subDay(),
'views' => 500,
]); // score = .0001215

// Prove we limit to max days since posted
Question::factory()
->hasLikes(50)
->create([
'content' => 'trending question 8',
'answer_created_at' => $date->subDays(8),
'views' => 500,
]); // score = .00006559 (would otherwise be trending...)

$component = Livewire::test(TrendingQuestions::class, ['perPage' => 10]);

$component = Livewire::test(TrendingQuestions::class);
$component->assertSeeInOrder([
'trending question 7',
'trending question 6',
'trending question 5',
'trending question 4',
'trending question 3',
'trending question 4',
'trending question 5',
'trending question 1',
'trending question 2',
]);
$component->assertDontSee('trending question 8');
});