Skip to content

Commit 8254856

Browse files
authored
Merge pull request #699 from meilisearch/feat/add-batches-endpoints
Add batches endpoints
2 parents 3e0a026 + 1e46dae commit 8254856

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Meilisearch;
66

7+
use Meilisearch\Endpoints\Batches;
8+
use Meilisearch\Endpoints\Delegates\HandlesBatches;
79
use Meilisearch\Endpoints\Delegates\HandlesDumps;
810
use Meilisearch\Endpoints\Delegates\HandlesIndex;
911
use Meilisearch\Endpoints\Delegates\HandlesKeys;
@@ -34,6 +36,7 @@ class Client
3436
use HandlesSnapshots;
3537
use HandlesSystem;
3638
use HandlesMultiSearch;
39+
use HandlesBatches;
3740

3841
/**
3942
* @param array<int, string> $clientAgents
@@ -52,6 +55,7 @@ public function __construct(
5255
$this->version = new Version($this->http);
5356
$this->stats = new Stats($this->http);
5457
$this->tasks = new Tasks($this->http);
58+
$this->batches = new Batches($this->http);
5559
$this->keys = new Keys($this->http);
5660
$this->dumps = new Dumps($this->http);
5761
$this->snapshots = new Snapshots($this->http);

src/Contracts/BatchesQuery.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
use Meilisearch\Endpoints\Delegates\TasksQueryTrait;
8+
9+
class BatchesQuery
10+
{
11+
use TasksQueryTrait;
12+
13+
private ?int $from = null;
14+
15+
/**
16+
* @var non-negative-int|null
17+
*/
18+
private ?int $limit = null;
19+
20+
private ?bool $reverse = null;
21+
22+
/**
23+
* @return $this
24+
*/
25+
public function setFrom(int $from): self
26+
{
27+
$this->from = $from;
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* @return $this
34+
*/
35+
public function setLimit(int $limit): self
36+
{
37+
$this->limit = $limit;
38+
39+
return $this;
40+
}
41+
42+
/**
43+
* @return $this
44+
*/
45+
public function setReverse(bool $reverse): self
46+
{
47+
$this->reverse = $reverse;
48+
49+
return $this;
50+
}
51+
52+
public function toArray(): array
53+
{
54+
return array_filter(
55+
array_merge(
56+
$this->baseArray(),
57+
[
58+
'from' => $this->from,
59+
'limit' => $this->limit,
60+
'reverse' => (null !== $this->reverse ? ($this->reverse ? 'true' : 'false') : null),
61+
]
62+
),
63+
static function ($item) {
64+
return null !== $item;
65+
}
66+
);
67+
}
68+
}

src/Contracts/BatchesResults.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
class BatchesResults extends Data
8+
{
9+
/**
10+
* @var non-negative-int
11+
*/
12+
private int $next;
13+
14+
/**
15+
* @var non-negative-int
16+
*/
17+
private int $limit;
18+
19+
/**
20+
* @var non-negative-int
21+
*/
22+
private int $from;
23+
24+
/**
25+
* @var non-negative-int
26+
*/
27+
private int $total;
28+
29+
public function __construct(array $params)
30+
{
31+
parent::__construct($params['results']);
32+
33+
$this->from = $params['from'] ?? 0;
34+
$this->limit = $params['limit'] ?? 0;
35+
$this->next = $params['next'] ?? 0;
36+
$this->total = $params['total'] ?? 0;
37+
}
38+
39+
/**
40+
* @return array<int, array>
41+
*/
42+
public function getResults(): array
43+
{
44+
return $this->data;
45+
}
46+
47+
/**
48+
* @return non-negative-int
49+
*/
50+
public function getNext(): int
51+
{
52+
return $this->next;
53+
}
54+
55+
/**
56+
* @return non-negative-int
57+
*/
58+
public function getLimit(): int
59+
{
60+
return $this->limit;
61+
}
62+
63+
/**
64+
* @return non-negative-int
65+
*/
66+
public function getFrom(): int
67+
{
68+
return $this->from;
69+
}
70+
71+
/**
72+
* @return non-negative-int
73+
*/
74+
public function getTotal(): int
75+
{
76+
return $this->total;
77+
}
78+
79+
public function toArray(): array
80+
{
81+
return [
82+
'results' => $this->data,
83+
'next' => $this->next,
84+
'limit' => $this->limit,
85+
'from' => $this->from,
86+
'total' => $this->total,
87+
];
88+
}
89+
90+
public function count(): int
91+
{
92+
return \count($this->data);
93+
}
94+
}

src/Contracts/TasksQuery.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class TasksQuery
2222
*/
2323
private ?array $canceledBy = null;
2424

25+
private ?int $batchUid = null;
26+
2527
private ?bool $reverse = null;
2628

2729
/**
@@ -59,6 +61,13 @@ public function setLimit(int $limit): self
5961
/**
6062
* @return $this
6163
*/
64+
public function setBatchUid(int $batchUid): self
65+
{
66+
$this->batchUid = $batchUid;
67+
68+
return $this;
69+
}
70+
6271
public function setReverse(bool $reverse): self
6372
{
6473
$this->reverse = $reverse;
@@ -75,6 +84,7 @@ public function toArray(): array
7584
'from' => $this->from,
7685
'limit' => $this->limit,
7786
'canceledBy' => $this->formatArray($this->canceledBy),
87+
'batchUid' => $this->batchUid,
7888
'reverse' => (null !== $this->reverse ? ($this->reverse ? 'true' : 'false') : null),
7989
]
8090
),

src/Endpoints/Batches.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Endpoints;
6+
7+
use Meilisearch\Contracts\Endpoint;
8+
9+
class Batches extends Endpoint
10+
{
11+
protected const PATH = '/batches';
12+
13+
public function get($batchUid): array
14+
{
15+
return $this->http->get(self::PATH.'/'.$batchUid);
16+
}
17+
18+
public function all(array $query = []): array
19+
{
20+
return $this->http->get(self::PATH.'/', $query);
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Endpoints\Delegates;
6+
7+
use Meilisearch\Contracts\BatchesQuery;
8+
use Meilisearch\Contracts\BatchesResults;
9+
use Meilisearch\Endpoints\Batches;
10+
11+
trait HandlesBatches
12+
{
13+
protected Batches $batches;
14+
15+
public function getBatch($uid): array
16+
{
17+
return $this->batches->get($uid);
18+
}
19+
20+
public function getBatches(?BatchesQuery $options = null): BatchesResults
21+
{
22+
$query = null !== $options ? $options->toArray() : [];
23+
24+
$response = $this->batches->all($query);
25+
26+
return new BatchesResults($response);
27+
}
28+
}

tests/Endpoints/BatchesTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Endpoints;
6+
7+
use Meilisearch\Contracts\BatchesQuery;
8+
use Meilisearch\Contracts\TasksQuery;
9+
use Tests\TestCase;
10+
11+
final class BatchesTest extends TestCase
12+
{
13+
private string $indexName;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->indexName = $this->safeIndexName();
19+
$this->createEmptyIndex($this->indexName);
20+
}
21+
22+
public function testGetAllBatches(): void
23+
{
24+
$response = $this->client->getBatches();
25+
self::assertGreaterThan(0, $response->getTotal());
26+
}
27+
28+
public function testGetAllBatchesWithIndexUidFilters(): void
29+
{
30+
$response = $this->client->getBatches((new BatchesQuery())->setIndexUids([$this->indexName]));
31+
foreach ($response->getResults() as $result) {
32+
self::assertArrayHasKey($this->indexName, $result['stats']['indexUids']);
33+
}
34+
}
35+
36+
public function testGetAllBatchesWithTasksFilters(): void
37+
{
38+
$tasks = $this->client->getTasks(new TasksQuery())->getResults();
39+
$response = $this->client->getBatches((new BatchesQuery())->setUids([$tasks[0]['uid']]));
40+
self::assertGreaterThan(0, $response->getTotal());
41+
}
42+
43+
public function testGetAllBatchesInReverseOrder(): void
44+
{
45+
$startDate = new \DateTimeImmutable('now');
46+
47+
$batches = $this->client->getBatches((new BatchesQuery())
48+
->setAfterEnqueuedAt($startDate)
49+
);
50+
$reversedBatches = $this->client->getBatches((new BatchesQuery())
51+
->setAfterEnqueuedAt($startDate)
52+
->setReverse(true)
53+
);
54+
self::assertSame($batches->getResults(), array_reverse($reversedBatches->getResults()));
55+
}
56+
57+
public function testGetOneBatch(): void
58+
{
59+
$batches = $this->client->getBatches();
60+
$response = $this->client->getBatch($batches->getResults()[0]['uid']);
61+
62+
self::assertSame($batches->getResults()[0]['uid'], $response['uid']);
63+
self::assertArrayHasKey('details', $response);
64+
self::assertArrayHasKey('totalNbTasks', $response['stats']);
65+
self::assertArrayHasKey('status', $response['stats']);
66+
self::assertArrayHasKey('types', $response['stats']);
67+
self::assertArrayHasKey('indexUids', $response['stats']);
68+
self::assertArrayHasKey('duration', $response);
69+
self::assertArrayHasKey('startedAt', $response);
70+
self::assertArrayHasKey('finishedAt', $response);
71+
}
72+
}

tests/Endpoints/TasksTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ public function testGetAllTasksClientWithPagination(): void
7676
self::assertSame([], $response->getResults());
7777
}
7878

79+
public function getAllTasksClientWithBatchFilter(): void
80+
{
81+
[$promise, $response] = $this->seedIndex();
82+
$task = $this->client->getTask($promise['taskUid']);
83+
84+
$response = $this->client->getTasks((new TasksQuery())
85+
->setBatchUid($task['uid'])
86+
);
87+
88+
self::assertGreaterThan(0, $response->getTotal());
89+
}
90+
7991
public function testGetOneTaskIndex(): void
8092
{
8193
[$promise, $response] = $this->seedIndex();

0 commit comments

Comments
 (0)