Skip to content

Commit

Permalink
Merge pull request #11145 from nanaya/recent-ended-at
Browse files Browse the repository at this point in the history
Sort recent scores based on end time instead of update
  • Loading branch information
peppy authored Apr 11, 2024
2 parents a1e27dd + 3706926 commit 9aa058e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ private function getExtra($page, array $options, int $perPage = 10, int $offset
$includes = ScoreTransformer::USER_PROFILE_INCLUDES;
$query = $this->user->soloScores()
->recent($this->mode, $options['includeFails'] ?? false)
->reorderBy('unix_updated_at', 'desc')
->reorderBy('ended_at', 'desc')
->with(ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
$userRelationColumn = 'user';
break;
Expand Down
12 changes: 10 additions & 2 deletions app/Models/Solo/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Models\ScoreToken;
use App\Models\Traits;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Builder;
use LaravelRedis;
Expand Down Expand Up @@ -181,14 +182,21 @@ public function scopeIndexable(Builder $query): Builder
*/
public function scopeRecent(Builder $query, string $ruleset, bool $includeFails): Builder
{
$minTime = CarbonImmutable::now()->subDays(1);

return $query
// ensure correct index is used
->from(\DB::raw("{$this->getTable()} FORCE INDEX (user_ruleset_index)"))
->default()
->forRuleset($ruleset)
->includeFails($includeFails)
// 1 day (24 * 3600)
->where('unix_updated_at', '>', time() - 86_400);
// unix_updated_at may be updated arbitrarily, so also filter by `ended_at` to ensure
// only recent scores are returned.
->where('ended_at', '>', $minTime)
// we still want to filter by `unix_updated_at` to make the query efficient (is in the PK).
->where('unix_updated_at', '>', $minTime->getTimestamp())
// ensure correct partition in production
->where('preserve', '>=', 0);
}

public function scopeVisibleUsers(Builder $query): Builder
Expand Down

0 comments on commit 9aa058e

Please sign in to comment.