Skip to content

Commit

Permalink
Merge pull request #38 from yawik/feat-organization_filter
Browse files Browse the repository at this point in the history
feat: organization filter
  • Loading branch information
cbleek authored Sep 23, 2020
2 parents aa86dfe + 8416ea7 commit 48a1401
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/console.config.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
'routes' => [
'solr-index-job' => [
'options' => [
'route' => 'solr index job [--batch=]',
'route' => 'solr index job [--batch=] [--orgId=] [--drop]',
'defaults' => [
'controller' => 'Solr/Console',
'action' => 'activeJobIndex',
41 changes: 32 additions & 9 deletions src/Controller/ConsoleController.php
Original file line number Diff line number Diff line change
@@ -9,19 +9,18 @@

namespace Solr\Controller;

use Core\Console\ProgressBar;
use Jobs\Entity\StatusInterface;
use Jobs\Repository\Job as JobRepository;
use Laminas\Mvc\Console\Controller\AbstractConsoleController;
use SolrClient;
use Laminas\Mvc\Controller\AbstractActionController;

/**
* @author Anthonius Munthi <me@itstoni.com>
* @author Miroslav Fedeleš <miroslav.fedeles@gmail.com>
* @since 0.26
* @package Solr\Controller
*/
class ConsoleController extends AbstractActionController
class ConsoleController extends AbstractConsoleController
{

/**
@@ -61,27 +60,49 @@ public function __construct(SolrClient $solrClient, JobRepository $jobRepository
public function activeJobIndexAction()
{

$console = $this->getConsole();
$limit = $this->params('batch', false);
$skip = 0;
if ($limit) {
$file = getcwd() . '/var/cache/solr-index.dat';
$skip = file_exists($file) ? file_get_contents($file) : 0;
file_put_contents($file, ($skip + $limit));
}


$qb = $this->jobRepository->createQueryBuilder()
->hydrate(true)
->field('status.name')->in([StatusInterface::ACTIVE])
->field('isDraft')->equals(false)
->readOnly()
;

$orgId = $this->params('orgId');
if ($orgId) {
if (!preg_match('~^[a-f0-9]{24}$~', $orgId)) {
$console->writeLine("Invalid organization id value.");
exit(200);
}
if ($this->params('drop', false) && !$skip) {
$console->writeLine("Deleting all indexed jobs from organization: " . $orgId . PHP_EOL);

$this->solrClient->deleteByQuery('organizationId:"' . $orgId . '"');
$this->solrClient->commit(true, false);
}
$qb->field('organization')->equals($orgId);
if (!$skip) {
$console->writeLine("Filter: orgId: $orgId" . PHP_EOL);
}
}

if ($limit) {
$qb->limit($limit)->skip($skip);
}
$q = $qb->getQuery();
$jobs = $q->execute();

$count = $jobs->count(true);

$count = $jobs->count(true);
// check if there is any active job
if (0 === $count) {
if ($limit) {
@@ -90,15 +111,17 @@ public function activeJobIndexAction()
return 'There is no active job' . PHP_EOL;
}

if ($count > 2500 && !$limit) {
return 'There are to many active jobs, please use --batch';
}

if ($limit) {
$upper = ($skip + $limit);
$total = $jobs->count();
$upper = $upper > $total ? $total : $upper;
echo "Processing jobs $skip - $upper of $total", PHP_EOL;
$console->writeLine("Processing jobs $skip - $upper of $total", PHP_EOL);
} else {
$console->writeLine("Found $count jobs." . PHP_EOL);
}

if ($count > 2500 && !$limit) {
return 'There are to many active jobs, please use --batch';
}

$i = 1;
15 changes: 14 additions & 1 deletion test/SolrTest/Controller/ConsoleControllerTest.php
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
use SolrClient;
use Jobs\Entity\Job;
use Jobs\Repository\Job as JobRepository;
use Laminas\Console\Adapter\AdapterInterface;
use Solr\Controller\ConsoleController;
use stdClass;
use Laminas\Mvc\Controller\PluginManager;
@@ -127,6 +128,12 @@ protected function setUp():void
->method('__invoke')
->willReturn($this->progressBar);

$console = $this->getMockBuilder(AdapterInterface::class)
->disableOriginalConstructor()
->setMethods(['writeLine'])
->getMockForAbstractClass();
$console->expects($this->any())->method('writeLine')->willReturn($console);

$this->controller = new ConsoleController($this->client, $jobRepo, $this->progressBarFactory, $this->options);

$this->params = $this->getMockBuilder(\Laminas\Mvc\Controller\Plugin\Params::class)
@@ -137,6 +144,7 @@ protected function setUp():void
$plugins->setService('params', $this->params);

$this->controller->setPluginManager($plugins);
$this->controller->setConsole($console);
}

/**
@@ -147,7 +155,12 @@ public function testActiveJobIndexActionWithoutJobs()
{
$this->qb->expects($this->never())->method('limit');
$this->qb->expects($this->never())->method('skip');
$this->params->expects($this->once())->method('__invoke')->willReturn(null);
$this->params->expects($this->any())->method('__invoke')
->will($this->returnValueMap([
['batch', false, false],
['orgId', null, null]
]
));
$this->cursor->expects($this->once())
->method('count')
->willReturn(0);

0 comments on commit 48a1401

Please sign in to comment.