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 ebf7b40
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
71 changes: 47 additions & 24 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,33 +148,11 @@ 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;
}
[$queryParameters, $queryTypes] = $this->addWheres($query, $qb);

switch ($query->sort()) {
case Query::SORT_BY_START_ASC:
Expand All @@ -196,6 +175,18 @@ public function query(Query $query): iterable
yield from $this->queryList($qb->getSQL(), $queryParameters, $queryTypes);
}

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

/** @var int $result */
$result = $this->connection->executeQuery($qb->getSQL(), ...$this->addWheres($query, $qb))->fetchOne();

return $result;
}

private function getSchema(): Schema
{
$schema = new Schema();
Expand Down Expand Up @@ -287,7 +278,7 @@ private function fetchRow(string $jobName, string $id): array

/**
* @phpstan-param array<string, mixed> $parameters
* @phpstan-param array<string, int|string> $types
* @phpstan-param array<string, array<int|string>|int> $types
*
* @phpstan-return Generator<JobExecution>
*/
Expand Down Expand Up @@ -325,4 +316,36 @@ private function getNormalizer(): JobExecutionRowNormalizer

return $this->normalizer;
}

/**
* @return array<int, array<string, array<int|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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public function testQuery(QueryBuilder $queryBuilder, array $expectedCouples): v
$this->loadFixtures($storage);

self::assertExecutions($expectedCouples, $storage->query($queryBuilder->getQuery()));
self::assertSame(\count($expectedCouples[0]), $storage->count($queryBuilder->getQuery()));
}

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

public function count(Query $query): int
{
/** @var JobExecution[] $result */
$result = $this->query($query);

return \count($result);
}

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function testQueryWithProvider(QueryBuilder $query, array $expectedCouple
);

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

public function query(): \Generator
Expand Down

0 comments on commit ebf7b40

Please sign in to comment.