optimizations

This commit is contained in:
2026-03-28 19:15:39 +01:00
parent 0b25d9570a
commit cab4fbd83e
509 changed files with 1016804 additions and 1605 deletions

View File

@@ -7,6 +7,7 @@ namespace App\Services\EarlyGrowth;
use App\Models\Artwork;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* SpotlightEngine
@@ -68,6 +69,7 @@ final class SpotlightEngine implements SpotlightEngineInterface
private function selectSpotlight(int $limit): Collection
{
$seed = (int) now()->format('Ymd');
$randomExpr = $this->dailyRandomExpression('artworks.id', $seed);
// Artworks published > 7 days ago with meaningful ranking score
return Artwork::query()
@@ -82,7 +84,7 @@ final class SpotlightEngine implements SpotlightEngineInterface
->select('artworks.*')
->where('artworks.published_at', '<=', now()->subDays(7))
// Blend ranking quality with daily-seeded randomness so spotlight varies
->orderByRaw("COALESCE(_ast.ranking_score, 0) * 0.6 + RAND({$seed}) * 0.4 DESC")
->orderByRaw("COALESCE(_ast.ranking_score, 0) * 0.6 + {$randomExpr} * 0.4 DESC")
->limit($limit * 3)
->get()
->sortByDesc(fn ($a) => optional($a->artworkStats)->ranking_score ?? 0)
@@ -96,6 +98,7 @@ final class SpotlightEngine implements SpotlightEngineInterface
private function selectCurated(int $limit, int $olderThanDays): Collection
{
$seed = (int) now()->format('Ymd');
$randomExpr = $this->dailyRandomExpression('artworks.id', $seed);
return Artwork::query()
->public()
@@ -108,9 +111,18 @@ final class SpotlightEngine implements SpotlightEngineInterface
->leftJoin('artwork_stats as _ast2', '_ast2.artwork_id', '=', 'artworks.id')
->select('artworks.*')
->where('artworks.published_at', '<=', now()->subDays($olderThanDays))
->orderByRaw("COALESCE(_ast2.ranking_score, 0) * 0.7 + RAND({$seed}) * 0.3 DESC")
->orderByRaw("COALESCE(_ast2.ranking_score, 0) * 0.7 + {$randomExpr} * 0.3 DESC")
->limit($limit)
->get()
->values();
}
private function dailyRandomExpression(string $idColumn, int $seed): string
{
if (DB::connection()->getDriverName() === 'sqlite') {
return "(ABS((({$idColumn} * 1103515245) + {$seed}) % 2147483647) / 2147483647.0)";
}
return "RAND({$seed} + {$idColumn})";
}
}