Repository navigation
fix(api): stable ordering, bounded limit and documented pagination on v2 (#52) #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
database/migrations/2026_10_26_180000_add_search_sort_indexes.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Support\Facades\DB; | ||
| use MongoDB\Driver\Exception\CommandException; | ||
|
|
||
| return new class () extends Migration { | ||
| /** | ||
| * /v2/incidents/search sorts by (dateTime desc, _id desc). These indexes | ||
| * give that order without a blocking in-memory sort (walked backwards): | ||
| * the first one for the default fire-only search, the second for all=1. | ||
| */ | ||
| private function indexes(): array | ||
| { | ||
| return [ | ||
| [['isFire' => 1, 'dateTime' => 1, '_id' => 1], 'fire_datetime_id_idx'], | ||
| [['dateTime' => 1, '_id' => 1], 'datetime_id_idx'], | ||
| ]; | ||
| } | ||
|
|
||
| public function up(): void | ||
| { | ||
| $collection = DB::connection('mongodb')->getMongoDB()->selectCollection('data'); | ||
|
|
||
| foreach ($this->indexes() as [$keys, $name]) { | ||
| try { | ||
| $collection->createIndex($keys, ['name' => $name]); | ||
| } catch (CommandException $e) { | ||
| // 85 = IndexOptionsConflict, 86 = IndexKeySpecsConflict: an | ||
| // equivalent index already exists under another name. | ||
| if (in_array($e->getCode(), [85, 86], true)) { | ||
| continue; | ||
| } | ||
|
|
||
| throw $e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| $collection = DB::connection('mongodb')->getMongoDB()->selectCollection('data'); | ||
|
|
||
| foreach ($this->indexes() as [, $name]) { | ||
| try { | ||
| $collection->dropIndex($name); | ||
| } catch (CommandException $e) { | ||
| // 27 = IndexNotFound. | ||
| if ($e->getCode() === 27) { | ||
| continue; | ||
| } | ||
|
|
||
| throw $e; | ||
| } | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| title: Paginator | ||
| type: object | ||
| description: >- | ||
| Pagination metadata of the `limit`/`page` incident endpoints. Results are | ||
| ordered deterministically, so walking pages 1..totalPages returns every | ||
| item exactly once (as long as the data does not change in between). | ||
| properties: | ||
| currentPage: | ||
| type: integer | ||
| example: 1 | ||
| totalPages: | ||
| type: integer | ||
| description: Last page number (1 when there are no results). | ||
| example: 3 | ||
| totalItems: | ||
| type: integer | ||
| example: 128 | ||
| perPage: | ||
| type: integer | ||
| description: Effective page size after clamping `limit`. | ||
| example: 50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| title: PhotoPageMeta | ||
| type: object | ||
| description: Pagination metadata of the `per_page`/`page` photo endpoints. | ||
| properties: | ||
| total: | ||
| type: integer | ||
| example: 42 | ||
| page: | ||
| type: integer | ||
| example: 1 | ||
| per_page: | ||
| type: integer | ||
| description: Effective page size after clamping `per_page`. | ||
| example: 20 | ||
| last_page: | ||
| type: integer | ||
| description: Last page number (1 when there are no results). | ||
| example: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| name: page | ||
| in: query | ||
| required: false | ||
| description: >- | ||
| Page number, starting at 1. Values that are not a positive integer are | ||
| treated as 1. A page past the last one returns an empty `data` array. | ||
| schema: | ||
| type: integer | ||
| minimum: 1 | ||
| default: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For searches without a date restriction, this adds a blocking sort over the entire matching incident archive before returning even the first 50 records. The indexes defined in
database/migrations/2026_05_25_120000_create_mongodb_indexes.phpcoverdateTimeand(isFire, dateTime), but neither includes the new_idsort key; MongoDB therefore cannot obtain this complete ordering from those indexes (MongoDB documentation). Unlike the previous unsorted, limited query, each page now requires fetching and sorting all matches, creating a substantial latency and database-load regression on large archives. Add matching compound indexes for the default fire-only search and theall=1search alongside this ordering change.Useful? React with 👍 / 👎.