* caching added to nightly config

* routes for getting nightly config data
This commit is contained in:
Dávid Danyi 2017-03-28 17:34:07 +02:00
parent d7d810f815
commit 9cd08908dc
7 changed files with 143 additions and 11 deletions

View File

@ -12,6 +12,8 @@ return [
App\Action\CiExecutorAction::class => App\Action\CiExecutorFactory::class,
App\Action\JcatPackageAction::class => App\Action\JcatPackageFactory::class,
App\Action\CiStreamAction::class => App\Action\CiStreamFactory::class,
App\Action\NightlyByNodesAction::class => App\Action\NightlyByNodesFactory::class,
App\Action\NightlyByStreamsAction::class => App\Action\NightlyByStreamsFactory::class,
App\Action\HomePageAction::class => App\Action\HomePageFactory::class,
],
],
@ -72,5 +74,17 @@ return [
'middleware' => App\Action\CiStreamAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'api.nightly-by-nodes',
'path' => '/api/nightly-by-nodes',
'middleware' => App\Action\NightlyByNodesAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'api.nightly-by-streams',
'path' => '/api/nightly-by-streams',
'middleware' => App\Action\NightlyByStreamsAction::class,
'allowed_methods' => ['GET'],
],
],
];

View File

@ -0,0 +1,27 @@
<?php
namespace App\Action;
use App\Response\JsonCorsResponse;
use App\Service\CiExecutorService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class NightlyByNodesAction extends AbstractAction
{
/**
* @var CiExecutorService
*/
private $ciExecutorService;
public function __construct(CiExecutorService $ciExecutorService)
{
$this->ciExecutorService = $ciExecutorService;
}
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new JsonCorsResponse($this->ciExecutorService->getActiveNightlyConfigurationByNode());
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Action;
use App\Service\CiExecutorService;
use Interop\Container\ContainerInterface;
class NightlyByNodesFactory
{
public function __invoke(ContainerInterface $container)
{
$service = $container->get(CiExecutorService::class);
return new NightlyByNodesAction($service);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Action;
use App\Response\JsonCorsResponse;
use App\Service\CiExecutorService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class NightlyByStreamsAction extends AbstractAction
{
/**
* @var CiExecutorService
*/
private $ciExecutorService;
public function __construct(CiExecutorService $ciExecutorService)
{
$this->ciExecutorService = $ciExecutorService;
}
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new JsonCorsResponse($this->ciExecutorService->getActiveNightlyConfiguration());
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Action;
use App\Service\CiExecutorService;
use Interop\Container\ContainerInterface;
class NightlyByStreamsFactory
{
public function __invoke(ContainerInterface $container)
{
$service = $container->get(CiExecutorService::class);
return new NightlyByStreamsAction($service);
}
}

View File

@ -25,6 +25,7 @@ class CiExecutorService
const JCAT_CACHE_KEY = "jcat";
const PACKAGE_CACHE_KEY = "packages";
const STREAM_CACHE_KEY = "stream";
const NIGHTLY_CACHE_KEY = "nightly";
private $testTypeMap = [
'isDoRollbackBefore' => 'rollback_before',
@ -692,19 +693,45 @@ class CiExecutorService
throw new \Exception("Jenkins redirect url is not working, return code was: " . $response->getStatusCode());
}
public function getActiveNightlyConfiguration()
public function getActiveNightlyConfigurationByNode(bool $forceReload = false)
{
$activeNightlyByStream = $this->getActiveNightlyConfiguration($forceReload);
$configByNodes = [];
foreach ($activeNightlyByStream as $stream => $nodes) {
foreach($nodes as $node) {
if(!isset($configByNodes[$node])) { $configByNodes[$node] = []; }
array_push($configByNodes[$node], $stream);
}
}
return $configByNodes;
}
public function getActiveNightlyConfiguration(bool $forceReload = false)
{
$cache = $this->getCache(self::NIGHTLY_CACHE_KEY);
$result = $cache->getItem(self::NIGHTLY_CACHE_KEY, $success);
if (!$success || $forceReload) {
$nightlyConfigHtml = $this->getNightlyConfigHtml();
$streams = $this->getHtmlDropdownValues($nightlyConfigHtml, "STREAM");
sort($streams, SORT_NATURAL);
$stapler = $this->getStaplerTokens($nightlyConfigHtml, "STREAM");
$result = [];
$ownNodes = $this->nodeService->getNodeNames();
$nightly = [];
foreach($streams as $stream) {
$result[$stream] = $this->getNightlyNodesForStream($stream, $stapler['url'], $stapler['token']);
$streamNodes = $this->getNightlyNodesForStream($stream, $stapler['url'], $stapler['token']);
$nodeDiff = array_intersect($streamNodes, $ownNodes);
if($nodeDiff) {
$nightly[$stream] = array_values($nodeDiff);
}
return $result;
}
$cache->setItem(self::NIGHTLY_CACHE_KEY, serialize($nightly));
} else {
$nightly = unserialize($result);
}
return $nightly;
}
private function getHtmlDropdownValues(string $html, string $fieldName): array

View File

@ -133,6 +133,13 @@ class CiNodeService
return $this->nodes;
}
public function getNodeNames(): array
{
return array_map(function($node) {
return $node["name"];
}, $this->nodes);
}
public function getNode($node): ?array
{
return array_pop(array_filter($this->nodes, function ($item) use ($node) {