Skip to content

Commit 1709ca9

Browse files
committed
Add back sorting by createdAt desc
1 parent 28f53f7 commit 1709ca9

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/Repositories/RedisBatchRepository.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,14 @@ public function get( $limit = 50, $before = null ): array
3737
// Determine the starting index
3838
$startIndex = 0;
3939
if ( $before !== null ) {
40-
// Fetch a range of batch IDs surrounding the 'before' value
41-
// Adjust the range size as needed for efficiency
4240
$rangeSize = 100;
4341
$allBatchIds = Redis::lrange( 'batches_list', 0, $rangeSize - 1 );
4442
$beforeIndex = array_search( $before, $allBatchIds );
4543

4644
if ( $beforeIndex !== false ) {
4745
$startIndex = $beforeIndex + 1;
48-
} else {
49-
// TODO:
50-
// Handle case where 'before' is not found in the initial range
51-
// Consider fetching additional ranges or handling this scenario differently
5246
}
47+
// Handle cases where 'before' is not found or other scenarios
5348
}
5449

5550
// Calculate the range to fetch
@@ -65,12 +60,16 @@ public function get( $limit = 50, $before = null ): array
6560
}
6661
} );
6762

68-
// Filter, unserialize, and map to Batch objects
69-
$batches = array_map( function ( $response ) {
70-
return $response ? $this->toBatch( json_decode( $response, true ) ) : null;
71-
}, $responses );
63+
// Filter, decode, and sort raw batch data
64+
$batchesRaw = array_map( fn( $response ) => json_decode( $response, true ), array_filter( $responses ) );
65+
uasort( $batchesRaw, function ( $a, $b ) {
66+
return $b['created_at'] <=> $a['created_at'];
67+
} );
68+
69+
// Map sorted data to Batch objects and convert to a sequential array
70+
$batches = array_values( array_map( fn( $data ) => $this->toBatch( $data ), $batchesRaw ) );
7271

73-
return array_filter( $batches );
72+
return $batches;
7473
}
7574

7675
public function find( string $batchId )
@@ -327,4 +326,4 @@ protected function pruneBatches( DateTimeInterface $before, $isFinished = null,
327326

328327
return $totalDeleted;
329328
}
330-
}
329+
}

0 commit comments

Comments
 (0)