From 80b8f1c543dfa5eb2a86f36875e0d3eb11d5231c Mon Sep 17 00:00:00 2001 From: hayaIshida <49195006+hayaIshida@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:04:02 +0100 Subject: [PATCH] perf: count thread engagement in PostgreSQL --- app/Actions/UpdateCommitHotScores.php | 53 ++++++++++--------- .../Controllers/CommitStarControllerTest.php | 2 +- .../Actions/UpdateCommitHotScoresTest.php | 47 ++++++++++++++++ 3 files changed, 77 insertions(+), 25 deletions(-) diff --git a/app/Actions/UpdateCommitHotScores.php b/app/Actions/UpdateCommitHotScores.php index 8fc8f3792b..35a158a6af 100644 --- a/app/Actions/UpdateCommitHotScores.php +++ b/app/Actions/UpdateCommitHotScores.php @@ -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; @@ -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)' @@ -184,18 +181,26 @@ private function visibleCommits(string $alias): Builder } /** - * @return array> + * 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 */ - 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 $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; } } diff --git a/tests/Feature/Controllers/CommitStarControllerTest.php b/tests/Feature/Controllers/CommitStarControllerTest.php index 7196cd9ab6..c402126f74 100644 --- a/tests/Feature/Controllers/CommitStarControllerTest.php +++ b/tests/Feature/Controllers/CommitStarControllerTest.php @@ -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" = ?')); diff --git a/tests/Unit/Actions/UpdateCommitHotScoresTest.php b/tests/Unit/Actions/UpdateCommitHotScoresTest.php index 455420157f..a3de12add2 100644 --- a/tests/Unit/Actions/UpdateCommitHotScoresTest.php +++ b/tests/Unit/Actions/UpdateCommitHotScoresTest.php @@ -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; @@ -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();