Skip to content

Commit e8c9285

Browse files
committed
Fix sorting and pagination in get method so it works correctly in horizon batches list.
1 parent 68e6191 commit e8c9285

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/Repositories/RedisBatchRepository.php

+21-25
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,44 @@ public function __construct( BatchFactory $factory )
2727
$this->factory = $factory;
2828
}
2929

30-
public function get( $limit = 50, $before = null ): array
30+
public function get($limit = 50, $before = null): array
3131
{
32-
if ( !Redis::exists( 'batches_list' ) ) {
32+
if (!Redis::exists('batches_list')) {
3333
return [];
3434
}
3535

36-
$totalBatches = Redis::llen( 'batches_list' );
36+
$totalBatches = Redis::llen('batches_list');
3737

38-
// Determine the starting index
39-
$startIndex = 0;
40-
if ( $before !== null ) {
41-
$rangeSize = 100;
42-
$allBatchIds = Redis::lrange( 'batches_list', 0, $rangeSize - 1 );
43-
$beforeIndex = array_search( $before, $allBatchIds );
38+
// If $before is specified, find its index
39+
$beforeIndex = $before ? array_search($before, Redis::lrange('batches_list', 0, -1)) : null;
4440

45-
if ( $beforeIndex !== false ) {
46-
$startIndex = $beforeIndex + 1;
47-
}
48-
// Handle cases where 'before' is not found or other scenarios
41+
// Determine the range to fetch
42+
if ($beforeIndex !== false && $beforeIndex !== null) {
43+
$rangeEnd = $beforeIndex - 1;
44+
$startIndex = max($rangeEnd - $limit + 1, 0);
45+
} else {
46+
$startIndex = max($totalBatches - $limit, 0);
47+
$rangeEnd = $totalBatches - 1;
4948
}
5049

51-
// Calculate the range to fetch
52-
$rangeEnd = min( $startIndex + $limit - 1, $totalBatches - 1 );
53-
5450
// Fetch only the required batch IDs
55-
$batchIds = Redis::lrange( 'batches_list', $startIndex, $rangeEnd );
51+
$batchIds = Redis::lrange('batches_list', $startIndex, $rangeEnd);
5652

5753
// Use Redis pipeline to bulk fetch batch data
58-
$responses = Redis::pipeline( function ( $pipe ) use ( $batchIds ) {
59-
foreach ( $batchIds as $batchId ) {
60-
$pipe->get( "batch:$batchId" );
54+
$responses = Redis::pipeline(function ($pipe) use ($batchIds) {
55+
foreach ($batchIds as $batchId) {
56+
$pipe->get("batch:$batchId");
6157
}
62-
} );
58+
});
6359

6460
// Filter, decode, and sort raw batch data
65-
$batchesRaw = array_map( fn( $response ) => json_decode( $response, true ), array_filter( $responses ) );
66-
uasort( $batchesRaw, function ( $a, $b ) {
61+
$batchesRaw = array_map(fn($response) => json_decode($response, true), array_filter($responses));
62+
uasort($batchesRaw, function ($a, $b) {
6763
return $b['created_at'] <=> $a['created_at'];
68-
} );
64+
});
6965

7066
// Map sorted data to Batch objects and convert to a sequential array
71-
$batches = array_values( array_map( fn( $data ) => $this->toBatch( $data ), $batchesRaw ) );
67+
$batches = array_values(array_map(fn($data) => $this->toBatch($data), $batchesRaw));
7268

7369
return $batches;
7470
}

0 commit comments

Comments
 (0)