-
Notifications
You must be signed in to change notification settings - Fork 105
Add typed Stats object
#823
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| final class IndexStats | ||
| { | ||
| /** | ||
| * @param non-negative-int $numberOfDocuments | ||
| * @param non-negative-int $rawDocumentDbSize | ||
| * @param non-negative-int $avgDocumentSize | ||
| * @param non-negative-int $numberOfEmbeddings | ||
| * @param non-negative-int $numberOfEmbeddedDocuments | ||
| * @param array<non-empty-string, non-negative-int> $fieldDistribution | ||
| */ | ||
| public function __construct( | ||
| private readonly int $numberOfDocuments, | ||
| private readonly int $rawDocumentDbSize, | ||
| private readonly int $avgDocumentSize, | ||
| private readonly bool $isIndexing, | ||
| private readonly int $numberOfEmbeddings, | ||
| private readonly int $numberOfEmbeddedDocuments, | ||
| private readonly array $fieldDistribution, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getNumberOfDocuments(): int | ||
| { | ||
| return $this->numberOfDocuments; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getRawDocumentDbSize(): int | ||
| { | ||
| return $this->rawDocumentDbSize; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getAvgDocumentSize(): int | ||
| { | ||
| return $this->avgDocumentSize; | ||
| } | ||
|
|
||
| public function isIndexing(): bool | ||
| { | ||
| return $this->isIndexing; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getNumberOfEmbeddings(): int | ||
| { | ||
| return $this->numberOfEmbeddings; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getNumberOfEmbeddedDocuments(): int | ||
| { | ||
| return $this->numberOfEmbeddedDocuments; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<non-empty-string, non-negative-int> | ||
| */ | ||
| public function getFieldDistribution(): array | ||
| { | ||
| return $this->fieldDistribution; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{ | ||
| * numberOfDocuments: non-negative-int, | ||
| * rawDocumentDbSize: non-negative-int, | ||
| * avgDocumentSize: non-negative-int, | ||
| * isIndexing: bool, | ||
| * numberOfEmbeddings: non-negative-int, | ||
| * numberOfEmbeddedDocuments: non-negative-int, | ||
| * fieldDistribution: array<non-empty-string, non-negative-int> | ||
| * } $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| return new self( | ||
| $data['numberOfDocuments'], | ||
| $data['rawDocumentDbSize'], | ||
| $data['avgDocumentSize'], | ||
| $data['isIndexing'], | ||
| $data['numberOfEmbeddings'], | ||
| $data['numberOfEmbeddedDocuments'], | ||
| $data['fieldDistribution'], | ||
| ); | ||
| } | ||
| } |
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,68 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| final class Stats | ||
| { | ||
| /** | ||
| * @param non-negative-int $databaseSize | ||
| * @param non-negative-int $usedDatabaseSize | ||
| * @param array<non-empty-string, IndexStats> $indexes | ||
| */ | ||
| public function __construct( | ||
| private readonly int $databaseSize, | ||
| private readonly int $usedDatabaseSize, | ||
| private readonly ?\DateTimeImmutable $lastUpdate, | ||
| private readonly array $indexes, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getDatabaseSize(): int | ||
| { | ||
| return $this->databaseSize; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-negative-int | ||
| */ | ||
| public function getUsedDatabaseSize(): int | ||
| { | ||
| return $this->usedDatabaseSize; | ||
| } | ||
|
|
||
| public function getLastUpdate(): ?\DateTimeImmutable | ||
| { | ||
| return $this->lastUpdate; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<non-empty-string, IndexStats> | ||
| */ | ||
| public function getIndexes(): array | ||
| { | ||
| return $this->indexes; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{ | ||
| * databaseSize: non-negative-int, | ||
| * usedDatabaseSize: non-negative-int, | ||
| * lastUpdate: non-empty-string|null, | ||
| * indexes: array<non-empty-string, mixed> | ||
| * } $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| return new self( | ||
| $data['databaseSize'], | ||
| $data['usedDatabaseSize'], | ||
| null !== $data['lastUpdate'] ? new \DateTimeImmutable($data['lastUpdate']) : null, | ||
| array_map(static fn (array $v) => IndexStats::fromArray($v), $data['indexes']), | ||
| ); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Contracts; | ||
|
|
||
| use Meilisearch\Contracts\IndexStats; | ||
| use Meilisearch\Contracts\Stats; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class StatsTest extends TestCase | ||
| { | ||
| public function testConstruct(): void | ||
| { | ||
| $stats = new Stats( | ||
| databaseSize: 1146880, | ||
| usedDatabaseSize: 925696, | ||
| lastUpdate: $date = new \DateTimeImmutable('2025-11-15 10:03:15.000000'), | ||
| indexes: $indexes = [ | ||
| 'stats_a902635d92481c0925e3a801bbc60c3e' => new IndexStats( | ||
| numberOfDocuments: 2, | ||
| rawDocumentDbSize: 4096, | ||
| avgDocumentSize: 2040, | ||
| isIndexing: false, | ||
| numberOfEmbeddings: 0, | ||
| numberOfEmbeddedDocuments: 0, | ||
| fieldDistribution: ['objectID' => 2, 'type' => 1], | ||
| ), | ||
| ], | ||
| ); | ||
|
|
||
| self::assertSame(1146880, $stats->getDatabaseSize()); | ||
| self::assertSame(925696, $stats->getUsedDatabaseSize()); | ||
| self::assertSame($date, $stats->getLastUpdate()); | ||
| self::assertSame($indexes, $stats->getIndexes()); | ||
| } | ||
|
|
||
| public function testFromArray(): void | ||
| { | ||
| $stats = Stats::fromArray([ | ||
| 'databaseSize' => 1146880, | ||
| 'usedDatabaseSize' => 925696, | ||
| 'lastUpdate' => '2025-11-15T10:03:15.000000000Z', | ||
| 'indexes' => [ | ||
| 'stats_a902635d92481c0925e3a801bbc60c3e' => [ | ||
| 'numberOfDocuments' => 2, | ||
| 'rawDocumentDbSize' => 4096, | ||
| 'avgDocumentSize' => 2040, | ||
| 'isIndexing' => false, | ||
| 'numberOfEmbeddings' => 0, | ||
| 'numberOfEmbeddedDocuments' => 0, | ||
| 'fieldDistribution' => ['objectID' => 2, 'type' => 1], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| self::assertSame(1146880, $stats->getDatabaseSize()); | ||
| self::assertSame(925696, $stats->getUsedDatabaseSize()); | ||
| self::assertEquals(new \DateTimeImmutable('2025-11-15 10:03:15.000000'), $stats->getLastUpdate()); | ||
| self::assertEquals([ | ||
| 'stats_a902635d92481c0925e3a801bbc60c3e' => new IndexStats( | ||
| numberOfDocuments: 2, | ||
| rawDocumentDbSize: 4096, | ||
| avgDocumentSize: 2040, | ||
| isIndexing: false, | ||
| numberOfEmbeddings: 0, | ||
| numberOfEmbeddedDocuments: 0, | ||
| fieldDistribution: ['objectID' => 2, 'type' => 1], | ||
| ), | ||
| ], $stats->getIndexes()); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.