Skip to content

Commit

Permalink
Merge pull request #699 from meilisearch/feat/add-batches-endpoints
Browse files Browse the repository at this point in the history
Add batches endpoints
  • Loading branch information
Strift authored Dec 6, 2024
2 parents 3e0a026 + 1e46dae commit 8254856
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Meilisearch;

use Meilisearch\Endpoints\Batches;
use Meilisearch\Endpoints\Delegates\HandlesBatches;
use Meilisearch\Endpoints\Delegates\HandlesDumps;
use Meilisearch\Endpoints\Delegates\HandlesIndex;
use Meilisearch\Endpoints\Delegates\HandlesKeys;
Expand Down Expand Up @@ -34,6 +36,7 @@ class Client
use HandlesSnapshots;
use HandlesSystem;
use HandlesMultiSearch;
use HandlesBatches;

/**
* @param array<int, string> $clientAgents
Expand All @@ -52,6 +55,7 @@ public function __construct(
$this->version = new Version($this->http);
$this->stats = new Stats($this->http);
$this->tasks = new Tasks($this->http);
$this->batches = new Batches($this->http);
$this->keys = new Keys($this->http);
$this->dumps = new Dumps($this->http);
$this->snapshots = new Snapshots($this->http);
Expand Down
68 changes: 68 additions & 0 deletions src/Contracts/BatchesQuery.php
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;
}
);
}
}
94 changes: 94 additions & 0 deletions src/Contracts/BatchesResults.php
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);
}
}
10 changes: 10 additions & 0 deletions src/Contracts/TasksQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TasksQuery
*/
private ?array $canceledBy = null;

private ?int $batchUid = null;

private ?bool $reverse = null;

/**
Expand Down Expand Up @@ -59,6 +61,13 @@ public function setLimit(int $limit): self
/**
* @return $this
*/
public function setBatchUid(int $batchUid): self
{
$this->batchUid = $batchUid;

return $this;
}

public function setReverse(bool $reverse): self
{
$this->reverse = $reverse;
Expand All @@ -75,6 +84,7 @@ public function toArray(): array
'from' => $this->from,
'limit' => $this->limit,
'canceledBy' => $this->formatArray($this->canceledBy),
'batchUid' => $this->batchUid,
'reverse' => (null !== $this->reverse ? ($this->reverse ? 'true' : 'false') : null),
]
),
Expand Down
22 changes: 22 additions & 0 deletions src/Endpoints/Batches.php
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);
}
}
28 changes: 28 additions & 0 deletions src/Endpoints/Delegates/HandlesBatches.php
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);
}
}
72 changes: 72 additions & 0 deletions tests/Endpoints/BatchesTest.php
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);
}
}
12 changes: 12 additions & 0 deletions tests/Endpoints/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public function testGetAllTasksClientWithPagination(): void
self::assertSame([], $response->getResults());
}

public function getAllTasksClientWithBatchFilter(): void
{
[$promise, $response] = $this->seedIndex();
$task = $this->client->getTask($promise['taskUid']);

$response = $this->client->getTasks((new TasksQuery())
->setBatchUid($task['uid'])
);

self::assertGreaterThan(0, $response->getTotal());
}

public function testGetOneTaskIndex(): void
{
[$promise, $response] = $this->seedIndex();
Expand Down

0 comments on commit 8254856

Please sign in to comment.