-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #699 from meilisearch/feat/add-batches-endpoints
Add batches endpoints
- Loading branch information
Showing
8 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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; | ||
|
||
use Meilisearch\Endpoints\Delegates\TasksQueryTrait; | ||
|
||
class BatchesQuery | ||
{ | ||
use TasksQueryTrait; | ||
|
||
private ?int $from = null; | ||
|
||
/** | ||
* @var non-negative-int|null | ||
*/ | ||
private ?int $limit = null; | ||
|
||
private ?bool $reverse = null; | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setFrom(int $from): self | ||
{ | ||
$this->from = $from; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setLimit(int $limit): self | ||
{ | ||
$this->limit = $limit; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setReverse(bool $reverse): self | ||
{ | ||
$this->reverse = $reverse; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_filter( | ||
array_merge( | ||
$this->baseArray(), | ||
[ | ||
'from' => $this->from, | ||
'limit' => $this->limit, | ||
'reverse' => (null !== $this->reverse ? ($this->reverse ? 'true' : 'false') : null), | ||
] | ||
), | ||
static function ($item) { | ||
return null !== $item; | ||
} | ||
); | ||
} | ||
} |
This file contains 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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Contracts; | ||
|
||
class BatchesResults extends Data | ||
{ | ||
/** | ||
* @var non-negative-int | ||
*/ | ||
private int $next; | ||
|
||
/** | ||
* @var non-negative-int | ||
*/ | ||
private int $limit; | ||
|
||
/** | ||
* @var non-negative-int | ||
*/ | ||
private int $from; | ||
|
||
/** | ||
* @var non-negative-int | ||
*/ | ||
private int $total; | ||
|
||
public function __construct(array $params) | ||
{ | ||
parent::__construct($params['results']); | ||
|
||
$this->from = $params['from'] ?? 0; | ||
$this->limit = $params['limit'] ?? 0; | ||
$this->next = $params['next'] ?? 0; | ||
$this->total = $params['total'] ?? 0; | ||
} | ||
|
||
/** | ||
* @return array<int, array> | ||
*/ | ||
public function getResults(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* @return non-negative-int | ||
*/ | ||
public function getNext(): int | ||
{ | ||
return $this->next; | ||
} | ||
|
||
/** | ||
* @return non-negative-int | ||
*/ | ||
public function getLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
/** | ||
* @return non-negative-int | ||
*/ | ||
public function getFrom(): int | ||
{ | ||
return $this->from; | ||
} | ||
|
||
/** | ||
* @return non-negative-int | ||
*/ | ||
public function getTotal(): int | ||
{ | ||
return $this->total; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'results' => $this->data, | ||
'next' => $this->next, | ||
'limit' => $this->limit, | ||
'from' => $this->from, | ||
'total' => $this->total, | ||
]; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->data); | ||
} | ||
} |
This file contains 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 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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Endpoints; | ||
|
||
use Meilisearch\Contracts\Endpoint; | ||
|
||
class Batches extends Endpoint | ||
{ | ||
protected const PATH = '/batches'; | ||
|
||
public function get($batchUid): array | ||
{ | ||
return $this->http->get(self::PATH.'/'.$batchUid); | ||
} | ||
|
||
public function all(array $query = []): array | ||
{ | ||
return $this->http->get(self::PATH.'/', $query); | ||
} | ||
} |
This file contains 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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Endpoints\Delegates; | ||
|
||
use Meilisearch\Contracts\BatchesQuery; | ||
use Meilisearch\Contracts\BatchesResults; | ||
use Meilisearch\Endpoints\Batches; | ||
|
||
trait HandlesBatches | ||
{ | ||
protected Batches $batches; | ||
|
||
public function getBatch($uid): array | ||
{ | ||
return $this->batches->get($uid); | ||
} | ||
|
||
public function getBatches(?BatchesQuery $options = null): BatchesResults | ||
{ | ||
$query = null !== $options ? $options->toArray() : []; | ||
|
||
$response = $this->batches->all($query); | ||
|
||
return new BatchesResults($response); | ||
} | ||
} |
This file contains 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\Endpoints; | ||
|
||
use Meilisearch\Contracts\BatchesQuery; | ||
use Meilisearch\Contracts\TasksQuery; | ||
use Tests\TestCase; | ||
|
||
final class BatchesTest extends TestCase | ||
{ | ||
private string $indexName; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->indexName = $this->safeIndexName(); | ||
$this->createEmptyIndex($this->indexName); | ||
} | ||
|
||
public function testGetAllBatches(): void | ||
{ | ||
$response = $this->client->getBatches(); | ||
self::assertGreaterThan(0, $response->getTotal()); | ||
} | ||
|
||
public function testGetAllBatchesWithIndexUidFilters(): void | ||
{ | ||
$response = $this->client->getBatches((new BatchesQuery())->setIndexUids([$this->indexName])); | ||
foreach ($response->getResults() as $result) { | ||
self::assertArrayHasKey($this->indexName, $result['stats']['indexUids']); | ||
} | ||
} | ||
|
||
public function testGetAllBatchesWithTasksFilters(): void | ||
{ | ||
$tasks = $this->client->getTasks(new TasksQuery())->getResults(); | ||
$response = $this->client->getBatches((new BatchesQuery())->setUids([$tasks[0]['uid']])); | ||
self::assertGreaterThan(0, $response->getTotal()); | ||
} | ||
|
||
public function testGetAllBatchesInReverseOrder(): void | ||
{ | ||
$startDate = new \DateTimeImmutable('now'); | ||
|
||
$batches = $this->client->getBatches((new BatchesQuery()) | ||
->setAfterEnqueuedAt($startDate) | ||
); | ||
$reversedBatches = $this->client->getBatches((new BatchesQuery()) | ||
->setAfterEnqueuedAt($startDate) | ||
->setReverse(true) | ||
); | ||
self::assertSame($batches->getResults(), array_reverse($reversedBatches->getResults())); | ||
} | ||
|
||
public function testGetOneBatch(): void | ||
{ | ||
$batches = $this->client->getBatches(); | ||
$response = $this->client->getBatch($batches->getResults()[0]['uid']); | ||
|
||
self::assertSame($batches->getResults()[0]['uid'], $response['uid']); | ||
self::assertArrayHasKey('details', $response); | ||
self::assertArrayHasKey('totalNbTasks', $response['stats']); | ||
self::assertArrayHasKey('status', $response['stats']); | ||
self::assertArrayHasKey('types', $response['stats']); | ||
self::assertArrayHasKey('indexUids', $response['stats']); | ||
self::assertArrayHasKey('duration', $response); | ||
self::assertArrayHasKey('startedAt', $response); | ||
self::assertArrayHasKey('finishedAt', $response); | ||
} | ||
} |
This file contains 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