Skip to content

Commit

Permalink
feature #98 Total count JobExecution matched by query
Browse files Browse the repository at this point in the history
  • Loading branch information
guich25 committed Jan 11, 2024
1 parent f6f6826 commit e5d3940
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
80 changes: 55 additions & 25 deletions src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -147,34 +148,10 @@ public function list(string $jobName): iterable

public function query(Query $query): iterable
{
$queryParameters = [];
$queryTypes = [];

$qb = $this->connection->createQueryBuilder();
$qb->select('*')
->from($this->table);

$names = $query->jobs();
if (\count($names) > 0) {
$qb->andWhere($qb->expr()->in('job_name', ':jobNames'));
$queryParameters['jobNames'] = $names;
$queryTypes['jobNames'] = Connection::PARAM_STR_ARRAY;
}

$ids = $query->ids();
if (\count($ids) > 0) {
$qb->andWhere($qb->expr()->in('id', ':ids'));
$queryParameters['ids'] = $ids;
$queryTypes['ids'] = Connection::PARAM_STR_ARRAY;
}

$statuses = $query->statuses();
if (\count($statuses) > 0) {
$qb->andWhere($qb->expr()->in('status', ':statuses'));
$queryParameters['statuses'] = $statuses;
$queryTypes['statuses'] = Connection::PARAM_INT_ARRAY;
}

switch ($query->sort()) {
case Query::SORT_BY_START_ASC:
$qb->orderBy('start_time', 'asc');
Expand All @@ -193,7 +170,18 @@ public function query(Query $query): iterable
$qb->setMaxResults($query->limit());
$qb->setFirstResult($query->offset());

yield from $this->queryList($qb->getSQL(), $queryParameters, $queryTypes);
yield from $this->queryList($qb->getSQL(), ...$this->addWheres($query, $qb));
}

public function count(Query $query): int
{
$qb = $this->connection->createQueryBuilder();
$qb->select('count(*)')
->from($this->table);

[$parameters, $types] = $this->addWheres($query, $qb);

return $this->connection->executeQuery($qb->getSQL(), $parameters, $types)->fetchOne();

Check failure on line 184 in src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Method Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage::count() should return int but returns mixed.
}

private function getSchema(): Schema
Expand Down Expand Up @@ -303,6 +291,16 @@ private function queryList(string $query, array $parameters, array $types): Gene
$statement->free();
}

/**
* @phpstan-param array<string, mixed> $parameters
* @phpstan-param array<string, int|string> $types
*/
private function queryCount(string $query, array $parameters, array $types): false|int

Check failure on line 298 in src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Method Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage::queryCount() is unused.
{
/** @var Result $statement */
return $this->connection->executeQuery($query, $parameters, $types)->fetchOne();

Check failure on line 301 in src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Method Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage::queryCount() should return int|false but returns mixed.

Check failure on line 301 in src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Variable $statement in PHPDoc tag @var does not exist.
}

/**
* @phpstan-return array<string, mixed>
*/
Expand All @@ -325,4 +323,36 @@ private function getNormalizer(): JobExecutionRowNormalizer

return $this->normalizer;
}

/**
* @return array<array<string|int>>
*/
private function addWheres(Query $query, QueryBuilder $qb): array
{
$queryParameters = [];
$queryTypes = [];

$names = $query->jobs();
if (count($names) > 0) {
$qb->andWhere($qb->expr()->in('job_name', ':jobNames'));
$queryParameters['jobNames'] = $names;
$queryTypes['jobNames'] = Connection::PARAM_STR_ARRAY;
}

$ids = $query->ids();
if (count($ids) > 0) {
$qb->andWhere($qb->expr()->in('id', ':ids'));
$queryParameters['ids'] = $ids;
$queryTypes['ids'] = Connection::PARAM_STR_ARRAY;
}

$statuses = $query->statuses();
if (count($statuses) > 0) {
$qb->andWhere($qb->expr()->in('status', ':statuses'));
$queryParameters['statuses'] = $statuses;
$queryTypes['statuses'] = Connection::PARAM_INT_ARRAY;
}

return [$queryParameters, $queryTypes];

Check failure on line 356 in src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Method Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage::addWheres() should return array<array<int|string>> but returns array<int, array<string, array<int|string>|int>>.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,14 @@ public function testList(): void
/**
* @dataProvider queries
*/
public function testQuery(QueryBuilder $queryBuilder, array $expectedCouples): void
public function testQuery(QueryBuilder $queryBuilder, array $expectedCouples, int $expectedCount): void
{
$storage = $this->createStorage();
$storage->setup();
$this->loadFixtures($storage);

self::assertExecutions($expectedCouples, $storage->query($queryBuilder->getQuery()));
self::assertSame($expectedCount, $storage->count($queryBuilder->getQuery()));
}

public function queries(): Generator
Expand All @@ -294,6 +295,7 @@ public function queries(): Generator
['import', '789'],
['import', '987'],
],
4
];
yield 'Filter ids' => [
(new QueryBuilder())
Expand All @@ -302,20 +304,23 @@ public function queries(): Generator
['export', '123'],
['import', '987'],
],
2
];
yield 'Filter job names' => [
(new QueryBuilder())
->jobs(['export']),
[
['export', '123'],
],
1,
];
yield 'Filter statuses' => [
(new QueryBuilder())
->statuses([BatchStatus::FAILED]),
[
['import', '456'],
],
1,
];
yield 'Order by start ASC' => [
(new QueryBuilder())
Expand All @@ -326,6 +331,7 @@ public function queries(): Generator
['export', '123'],
['import', '456'],
],
4,
];
yield 'Order by start DESC' => [
(new QueryBuilder())
Expand All @@ -336,6 +342,7 @@ public function queries(): Generator
['import', '789'],
['import', '987'],
],
4,
];
yield 'Order by end ASC' => [
(new QueryBuilder())
Expand All @@ -346,6 +353,7 @@ public function queries(): Generator
['export', '123'],
['import', '456'],
],
4,
];
yield 'Order by end DESC' => [
(new QueryBuilder())
Expand All @@ -356,6 +364,7 @@ public function queries(): Generator
['import', '987'],
['import', '789'],
],
4,
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/batch/src/Storage/FilesystemJobExecutionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public function query(Query $query): iterable
return \array_slice($candidates, $query->offset(), $query->limit());
}

public function count(Query $query): int
{
return count($this->query($query));

Check failure on line 144 in src/batch/src/Storage/FilesystemJobExecutionStorage.php

View workflow job for this annotation

GitHub Actions / PhpStan

Parameter #1 $value of function count expects array|Countable, iterable<Yokai\Batch\JobExecution> given.
}

private function buildFilePath(string $jobName, string $executionId): string
{
return \implode(DIRECTORY_SEPARATOR, [$this->directory, $jobName, $executionId]) .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ interface QueryableJobExecutionStorageInterface extends ListableJobExecutionStor
* @return iterable|JobExecution[]
*/
public function query(Query $query): iterable;

/**
* Execute query against stored job executions, and return count result.
*/
public function count(Query $query): int;
}
11 changes: 10 additions & 1 deletion src/batch/tests/Storage/FilesystemJobExecutionStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ public function list(): \Generator
/**
* @dataProvider query
*/
public function testQueryWithProvider(QueryBuilder $query, array $expectedCouples): void
public function testQueryWithProvider(QueryBuilder $query, array $expectedCouples, int $expectedCount): void
{
$storage = $this->createStorage(
__DIR__ . '/fixtures/filesystem-job-execution',
new JsonJobExecutionSerializer()
);

self::assertExecutions($expectedCouples, $storage->query($query->getQuery()));
self::assertEquals($expectedCount, $storage->count($query->getQuery()));
}

public function query(): \Generator
Expand All @@ -163,6 +164,7 @@ public function query(): \Generator
['list', '20210915'],
['list', '20210920'],
],
5
];
yield 'Filter ids' => [
(new QueryBuilder())
Expand All @@ -171,6 +173,7 @@ public function query(): \Generator
['export', '20210920'],
['list', '20210920'],
],
2
];
yield 'Filter job names' => [
(new QueryBuilder())
Expand All @@ -180,13 +183,15 @@ public function query(): \Generator
['list', '20210915'],
['list', '20210920'],
],
3
];
yield 'Filter statuses' => [
(new QueryBuilder())
->statuses([BatchStatus::FAILED]),
[
['list', '20210910'],
],
1
];
yield 'Order by start ASC' => [
(new QueryBuilder())
Expand All @@ -198,6 +203,7 @@ public function query(): \Generator
['list', '20210920'],
['export', '20210922'],
],
5
];
yield 'Order by start DESC' => [
(new QueryBuilder())
Expand All @@ -209,6 +215,7 @@ public function query(): \Generator
['list', '20210915'],
['list', '20210910'],
],
5
];
yield 'Order by end ASC' => [
(new QueryBuilder())
Expand All @@ -220,6 +227,7 @@ public function query(): \Generator
['list', '20210920'],
['export', '20210922'],
],
5
];
yield 'Order by end DESC' => [
(new QueryBuilder())
Expand All @@ -231,6 +239,7 @@ public function query(): \Generator
['list', '20210915'],
['list', '20210910'],
],
5
];
}

Expand Down

0 comments on commit e5d3940

Please sign in to comment.