Skip to content

Commit

Permalink
Add back sorting by createdAt desc
Browse files Browse the repository at this point in the history
  • Loading branch information
cyppe committed Jan 25, 2024
1 parent 28f53f7 commit 1709ca9
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Repositories/RedisBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,14 @@ public function get( $limit = 50, $before = null ): array
// Determine the starting index
$startIndex = 0;
if ( $before !== null ) {
// Fetch a range of batch IDs surrounding the 'before' value
// Adjust the range size as needed for efficiency
$rangeSize = 100;
$allBatchIds = Redis::lrange( 'batches_list', 0, $rangeSize - 1 );
$beforeIndex = array_search( $before, $allBatchIds );

if ( $beforeIndex !== false ) {
$startIndex = $beforeIndex + 1;
} else {
// TODO:
// Handle case where 'before' is not found in the initial range
// Consider fetching additional ranges or handling this scenario differently
}
// Handle cases where 'before' is not found or other scenarios
}

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

// Filter, unserialize, and map to Batch objects
$batches = array_map( function ( $response ) {
return $response ? $this->toBatch( json_decode( $response, true ) ) : null;
}, $responses );
// Filter, decode, and sort raw batch data
$batchesRaw = array_map( fn( $response ) => json_decode( $response, true ), array_filter( $responses ) );
uasort( $batchesRaw, function ( $a, $b ) {
return $b['created_at'] <=> $a['created_at'];
} );

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

return array_filter( $batches );
return $batches;
}

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

return $totalDeleted;
}
}
}

0 comments on commit 1709ca9

Please sign in to comment.