Skip to content

Commit 9aa058e

Browse files
authored
Merge pull request #11145 from nanaya/recent-ended-at
Sort recent scores based on end time instead of update
2 parents a1e27dd + 3706926 commit 9aa058e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

app/Http/Controllers/UsersController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ private function getExtra($page, array $options, int $perPage = 10, int $offset
844844
$includes = ScoreTransformer::USER_PROFILE_INCLUDES;
845845
$query = $this->user->soloScores()
846846
->recent($this->mode, $options['includeFails'] ?? false)
847-
->reorderBy('unix_updated_at', 'desc')
847+
->reorderBy('ended_at', 'desc')
848848
->with(ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
849849
$userRelationColumn = 'user';
850850
break;

app/Models/Solo/Score.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use App\Models\ScoreToken;
2020
use App\Models\Traits;
2121
use App\Models\User;
22+
use Carbon\CarbonImmutable;
2223
use Illuminate\Contracts\Filesystem\Filesystem;
2324
use Illuminate\Database\Eloquent\Builder;
2425
use LaravelRedis;
@@ -181,14 +182,21 @@ public function scopeIndexable(Builder $query): Builder
181182
*/
182183
public function scopeRecent(Builder $query, string $ruleset, bool $includeFails): Builder
183184
{
185+
$minTime = CarbonImmutable::now()->subDays(1);
186+
184187
return $query
185188
// ensure correct index is used
186189
->from(\DB::raw("{$this->getTable()} FORCE INDEX (user_ruleset_index)"))
187190
->default()
188191
->forRuleset($ruleset)
189192
->includeFails($includeFails)
190-
// 1 day (24 * 3600)
191-
->where('unix_updated_at', '>', time() - 86_400);
193+
// unix_updated_at may be updated arbitrarily, so also filter by `ended_at` to ensure
194+
// only recent scores are returned.
195+
->where('ended_at', '>', $minTime)
196+
// we still want to filter by `unix_updated_at` to make the query efficient (is in the PK).
197+
->where('unix_updated_at', '>', $minTime->getTimestamp())
198+
// ensure correct partition in production
199+
->where('preserve', '>=', 0);
192200
}
193201

194202
public function scopeVisibleUsers(Builder $query): Builder

0 commit comments

Comments
 (0)