Skip to content
Open
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
53 changes: 29 additions & 24 deletions app/Actions/UpdateCommitHotScores.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Dto\ThreadEngagement;
use App\Enums\CommitStatus;
use App\Models\Commit;
use App\Queries\CommitBuilder;
use App\Services\TheAlgorithm;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -82,21 +83,17 @@ private function rescore(array $rootIds): void
return;
}

$stargazersByThread = $this->usersByThread($this->stargazers($commits->modelKeys()));
$quotersByThread = $this->usersByThread($this->quoters($commits->modelKeys()));
$repliersByThread = $this->usersByThread($this->repliers($commits->modelKeys()));
$repliersAnsweredByAuthorByThread = $this->usersByThread($this->repliersAnsweredByAuthor($commits->modelKeys()));
$stargazersByThread = $this->countsByThread($this->stargazers($commits->modelKeys()));
$quotersByThread = $this->countsByThread($this->quoters($commits->modelKeys()));
$repliersByThread = $this->countsByThread($this->repliers($commits->modelKeys()));
$repliersAnsweredByAuthorByThread = $this->countsByThread($this->repliersAnsweredByAuthor($commits->modelKeys()));

$scores = $commits->flatMap(function (Commit $commit) use ($stargazersByThread, $quotersByThread, $repliersByThread, $repliersAnsweredByAuthorByThread): array {
$author = $commit->author_id === null ? [] : [$commit->author_id => true];

return [$commit->id, $this->theAlgorithm->score($commit->created_at, new ThreadEngagement(
stargazers: count(array_diff_key($stargazersByThread[$commit->id] ?? [], $author)),
quoters: count(array_diff_key($quotersByThread[$commit->id] ?? [], $author)),
repliers: count(array_diff_key($repliersByThread[$commit->id] ?? [], $author)),
repliersAnsweredByAuthor: count(array_diff_key($repliersAnsweredByAuthorByThread[$commit->id] ?? [], $author)),
))];
});
$scores = $commits->flatMap(fn (Commit $commit): array => [$commit->id, $this->theAlgorithm->score($commit->created_at, new ThreadEngagement(
stargazers: $stargazersByThread[$commit->id] ?? 0,
quoters: $quotersByThread[$commit->id] ?? 0,
repliers: $repliersByThread[$commit->id] ?? 0,
repliersAnsweredByAuthor: $repliersAnsweredByAuthorByThread[$commit->id] ?? 0,
))]);

DB::update(
'update commits set hot_score = cast(case id '.str_repeat('when ? then ? ', $commits->count()).'end as double precision)'
Expand Down Expand Up @@ -184,18 +181,26 @@ private function visibleCommits(string $alias): Builder
}

/**
* @return array<string, array<string, true>>
* Each query selects distinct thread/profile pairs. Count them in the database, excluding anonymous engagement
* and the thread's own author, so only one row per engaged thread is transferred to PHP.
*
* @return array<string, int>
*/
private function usersByThread(Builder $query): array
private function countsByThread(Builder $query): array
{
$usersByThread = [];

foreach ($query->get() as $row) {
if (is_string($row->thread_id) && is_string($row->profile_id)) {
$usersByThread[$row->thread_id][$row->profile_id] = true;
}
}
/** @var array<string, int> $counts */
$counts = Commit::query()
->joinSub($query, 'engagement', 'engagement.thread_id', '=', 'commits.id')
->whereNotNull('engagement.profile_id')
->where(fn (CommitBuilder $query): CommitBuilder => $query
->whereNull('commits.author_id')
->orWhereColumn('engagement.profile_id', '!=', 'commits.author_id'))
->select('commits.id')
->selectRaw('count(*) as engagement_count')
->groupBy('commits.id')
->pluck('engagement_count', 'id')
->all();

return $usersByThread;
return $counts;
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Controllers/CommitStarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
$this->actingAs($user)->postJson(route('commit-star.store', $commit))->assertExactJson(['starred' => true, 'stars_count' => 2]);
$this->deleteJson(route('commit-star.destroy', $commit))->assertExactJson(['starred' => false, 'stars_count' => 1]);

$counts = array_values(array_filter(array_column(DB::getQueryLog(), 'query'), fn (string $query): bool => str_contains($query, 'count(*)')));
$counts = array_values(array_filter(array_column(DB::getQueryLog(), 'query'), fn (string $query): bool => str_starts_with($query, 'select count(*) as "aggregate" from "stars"')));

expect($counts)->toBe(array_fill(0, 2, 'select count(*) as "aggregate" from "stars" where "stars"."commit_id" = ?'));

Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Actions/UpdateCommitHotScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Actions\UpdateCommitHotScores;
use App\Dto\ThreadEngagement;
use App\Models\Commit;
use App\Models\Organization;
use App\Models\User;
use App\Services\TheAlgorithm;

Expand Down Expand Up @@ -118,6 +119,52 @@
->toEqualWithDelta(resolve(TheAlgorithm::class)->score($commit->created_at, new ThreadEngagement(stargazers: 0, quoters: 0, repliers: 2, repliersAnsweredByAuthor: 1)), 1e-9);
});

it('excludes each threads own author without excluding their engagement in another thread', function (): void {
$author = User::factory()->create();
$own = Commit::factory()->for($author)->create();
$other = Commit::factory()->create();

foreach ([$own, $other] as $commit) {
$commit->stars()->attach($author);
Commit::factory()->for($author)->for($commit, 'parent')->create();
Commit::factory()->for($author)->for($commit, 'quotedCommit')->create();
}

resolve(UpdateCommitHotScores::class)->handle([$own->id, $other->id]);

expect($own->fresh()->hot_score)->toEqualWithDelta(resolve(TheAlgorithm::class)->score($own->created_at, ThreadEngagement::none()), 1e-9)
->and($other->fresh()->hot_score)->toEqualWithDelta(resolve(TheAlgorithm::class)->score($other->created_at, new ThreadEngagement(stargazers: 1, quoters: 1, repliers: 1, repliersAnsweredByAuthor: 0)), 1e-9);
});

it('counts engagement in an authorless thread but not replies or quotes without an author', function (): void {
$commit = Commit::factory()->create(['author_id' => null]);
$commit->stars()->attach($commit->user);
Commit::factory()->for($commit->user)->for($commit, 'parent')->create();
Commit::factory()->for($commit->user)->for($commit, 'quotedCommit')->create();
Commit::factory()->for($commit, 'parent')->create(['author_id' => null]);
Commit::factory()->for($commit, 'quotedCommit')->create(['author_id' => null]);

resolve(UpdateCommitHotScores::class)->handle([$commit->id]);

expect($commit->fresh()->hot_score)
->toEqualWithDelta(resolve(TheAlgorithm::class)->score($commit->created_at, new ThreadEngagement(stargazers: 1, quoters: 1, repliers: 1, repliersAnsweredByAuthor: 0)), 1e-9);
});

it('excludes the organization author while counting the member who posted its thread', function (): void {
$organization = Organization::factory()->create();
$commit = Commit::factory()->create(['author_id' => $organization->id]);
$commit->stars()->attach($commit->user);
$reply = Commit::factory()->for($commit->user)->for($commit, 'parent')->create();
Commit::factory()->for($reply, 'parent')->create(['author_id' => $organization->id]);
Commit::factory()->for($commit->user)->for($commit, 'quotedCommit')->create();
Commit::factory()->for($commit, 'quotedCommit')->create(['author_id' => $organization->id]);

resolve(UpdateCommitHotScores::class)->handle([$commit->id]);

expect($commit->fresh()->hot_score)
->toEqualWithDelta(resolve(TheAlgorithm::class)->score($commit->created_at, new ThreadEngagement(stargazers: 1, quoters: 1, repliers: 1, repliersAnsweredByAuthor: 1)), 1e-9);
});

it('leaves deleted threads as they are', function (): void {
$commit = Commit::factory()->create(['hot_score' => 12.5]);
$commit->delete();
Expand Down